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

View file

@ -19,11 +19,16 @@ describe CouchRest::Database do
describe "GET (document by id) when the doc exists" do describe "GET (document by id) when the doc exists" do
before(:each) do before(:each) do
@r = @db.save({'lemons' => 'from texas', 'and' => 'spain'}) @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 end
it "should get the document" do it "should get the document" do
doc = @db.get(@r['id']) doc = @db.get(@r['id'])
doc['lemons'].should == 'from texas' doc['lemons'].should == 'from texas'
end end
it "should work with a funky id" do
@db.get(@docid)['will-exist'].should == 'here'
end
end end
describe "POST (new document without an id)" do describe "POST (new document without an id)" do
@ -39,6 +44,15 @@ describe CouchRest::Database do
end end
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 describe "PUT (new document with id)" do
it "should start without the document" do it "should start without the document" do
# r = @db.save({'lemons' => 'from texas', 'and' => 'spain'}) # r = @db.save({'lemons' => 'from texas', 'and' => 'spain'})