escape get and put ... still need delete

This commit is contained in:
Chris Anderson 2008-03-19 14:33:41 -07:00
parent 99f25bbbfc
commit 420168be70
2 changed files with 20 additions and 4 deletions

View file

@ -1,3 +1,5 @@
require 'cgi'
class CouchRest
class Database
attr_accessor :host, :name
@ -16,20 +18,20 @@ class CouchRest
end
def get id
CouchRest.get "#{@root}/#{id}"
slug = CGI.escape(id)
CouchRest.get "#{@root}/#{slug}"
end
# PUT or POST depending on precense of _id attribute
def save doc
if doc['_id']
url = doc['_id']
CouchRest.put "#{@root}/#{doc['_id']}", doc
slug = CGI.escape(doc['_id'])
CouchRest.put "#{@root}/#{slug}", doc
else
CouchRest.post "#{@root}", doc
end
end
def delete!
CouchRest.delete @root
end

View file

@ -19,11 +19,16 @@ describe CouchRest::Database do
describe "GET (document by id) when the doc exists" do
before(:each) do
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
@db.save({'_id' => @docid, 'will-exist' => 'here'})
end
it "should get the document" do
doc = @db.get(@r['id'])
doc['lemons'].should == 'from texas'
end
it "should work with a funky id" do
@db.get(@docid)['will-exist'].should == 'here'
end
end
describe "POST (new document without an id)" do
@ -39,6 +44,15 @@ describe CouchRest::Database do
end
end
describe "PUT (new document with url id)" do
it "should create the document" do
@docid = "http://example.com/stuff.cgi?things=and%20stuff"
@db.save({'_id' => @docid, 'will-exist' => 'here'})
lambda{@db.save({'_id' => @docid})}.should raise_error(RestClient::Request::RequestFailed)
@db.get(@docid)['will-exist'].should == 'here'
end
end
describe "PUT (new document with id)" do
it "should start without the document" do
# r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})