diff --git a/lib/database.rb b/lib/database.rb index fbca83f..57f6937 100644 --- a/lib/database.rb +++ b/lib/database.rb @@ -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 diff --git a/spec/database_spec.rb b/spec/database_spec.rb index eeefbac..7c6ebf5 100644 --- a/spec/database_spec.rb +++ b/spec/database_spec.rb @@ -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 @@ -38,6 +43,15 @@ describe CouchRest::Database do lambda{@db.save({'_id' => r['id']})}.should raise_error(RestClient::Request::RequestFailed) 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