Commit c0f73265 authored by John E. Vincent's avatar John E. Vincent

wrapping up ephemeral specs

parent ed104af7
......@@ -22,7 +22,8 @@ class Noah::App
put '/e/*' do
raise("Data too large") if request.body.size > 512
d = request.body.read || nil
e = Noah::Ephemeral.new(:path => "/#{params[:splat][0]}", :data => d)
e = Noah::Ephemeral.find_or_create(:path => "/#{params[:splat][0]}")
e.data = d
if e.valid?
e.save
action = e.is_new? ? "create" : "update"
......
......@@ -21,6 +21,21 @@ module Noah
h = {:path => path, :data => data, :created_at => created_at, :updated_at => :updated_at}
super.merge(h)
end
class << self
def find_or_create(opts = {})
begin
find(opts).first.nil? ? (eph = create(opts)) : (eph = find(opts).first)
if eph.valid?
eph.save
end
eph
rescue Exception => e
e.message
end
end
end
protected
def save_hook
# called after any create,update,delete
......@@ -31,7 +46,7 @@ module Noah
def path_protected?(path_part)
# Check for protected paths in ephemeral nodes
end
end
end
......@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
s.add_development_dependency("warbler", ["= 1.2.1"])
else
s.add_dependency("yajl-ruby")
s.add_dependency("SystemTimer")
s.add_dependency("SystemTimer") if RUBY_VERSION =~ /1.8/
s.add_dependency("thin")
end
......
......@@ -65,8 +65,28 @@ describe "Using the Ephemeral API", :reset_redis => true do
response['data'].should == nil
end
it "existing ephemeral with data should work"
it "existing ephemeral without data should work"
it "existing ephemeral with data should work" do
Noah::Ephemeral.create(:path => '/new/ephemeral', :data => 'old_value')
get '/e/new/ephemeral'
last_response.should be_ok
last_response.body.should == 'old_value'
put '/e/new/ephemeral', 'new_value'
last_response.should be_ok
get '/e/new/ephemeral'
last_response.should be_ok
last_response.body.should == 'new_value'
end
it "existing ephemeral without data should work" do
Noah::Ephemeral.create(:path => '/a/random/key')
get '/e/a/random/key'
last_response.should be_ok
last_response.body.should == ""
put '/e/a/random/key', 'a new value'
last_response.should be_ok
get '/e/a/random/key'
last_response.should be_ok
last_response.body.should == 'a new value'
end
end
describe "DELETE" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment