From 097ab935c616d7d19c31e1a6eba3cc07c4b7d8aa Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Wed, 19 Mar 2008 08:57:20 -0700 Subject: [PATCH] getting into view land --- lib/couch_rest.rb | 20 ++++++++++++------ lib/database.rb | 21 +++++++++++++++++- spec/couch_rest_spec.rb | 11 ++++++++-- spec/database_spec.rb | 47 +++++++++++++++++++++++++++++++++++------ 4 files changed, 83 insertions(+), 16 deletions(-) diff --git a/lib/couch_rest.rb b/lib/couch_rest.rb index f91cea4..1e450df 100644 --- a/lib/couch_rest.rb +++ b/lib/couch_rest.rb @@ -36,19 +36,25 @@ class CouchRest end class << self - def put uri, payload = nil - response = RestClient.put(uri, payload) - JSON.parse response + def put uri, doc = nil + payload = JSON.unparse doc if doc + JSON.parse(RestClient.put(uri, payload)) end def get uri - response = RestClient.get(uri) - JSON.parse response + JSON.parse(RestClient.get(uri)) + end + + def post uri, doc = nil + payload = JSON.unparse doc if doc + JSON.parse(RestClient.post(uri, payload)) end def delete uri - response = RestClient.delete(uri) - JSON.parse response + JSON.parse(RestClient.delete(uri)) end end + end + + diff --git a/lib/database.rb b/lib/database.rb index b2e7e76..482677d 100644 --- a/lib/database.rb +++ b/lib/database.rb @@ -4,10 +4,29 @@ class CouchRest def initialize host, name @name = name @host = host + @root = "#{host}/#{name}" end + def documents + view "_all_docs" + end + + def view name + CouchRest.get "#{@root}/#{name}" + end + + def save doc + if doc['_id'] + url = doc['_id'] + CouchRest.put "#{@root}/#{doc['_id']}", doc + else + CouchRest.post "#{@root}", doc + end + end + + def delete! - CouchRest.delete "#{host}/#{name}" + CouchRest.delete @root end end end \ No newline at end of file diff --git a/spec/couch_rest_spec.rb b/spec/couch_rest_spec.rb index a0e37da..f4a76b2 100644 --- a/spec/couch_rest_spec.rb +++ b/spec/couch_rest_spec.rb @@ -4,9 +4,16 @@ describe CouchRest do before(:each) do @cr = CouchRest.new("http://local.grabb.it:5984") - db = @cr.database('couchrest-test') + @db = @cr.database('couchrest-test') begin - db.delete! + @db.delete! + rescue RestClient::Request::RequestFailed + end + end + + after(:each) do + begin + @db.delete! rescue RestClient::Request::RequestFailed end end diff --git a/spec/database_spec.rb b/spec/database_spec.rb index 9c87e8f..9ddde7e 100644 --- a/spec/database_spec.rb +++ b/spec/database_spec.rb @@ -4,21 +4,56 @@ describe CouchRest::Database do before(:each) do @cr = CouchRest.new("http://local.grabb.it:5984") begin - @cr.create_db('couchrest-test') + @db = @cr.create_db('couchrest-test') rescue RestClient::Request::RequestFailed end end - describe "deleting one" do - before(:each) do - + + after(:each) do + begin + @db.delete! + rescue RestClient::Request::RequestFailed end + end + + describe "inserting documents without an id: POST" do + it "should start without the document" do + @db.documents.should_not include({'_id' => 'my-doc'}) + # this needs to be a loop over docs on content with the post + # or instead make it return something with a fancy <=> method + end + it "should create the document" do + r = @db.save({'lemons' => 'from texas', 'and' => 'spain'}) + @db.documents.should include(r) + end + end + + describe "with documents in it" do + before(:each) do + # @db.create_doc() + end + it "should list them" do + ds = @db.documents + ds['rows'].should be_an_instance_of(Array) + # ds[:total_rows].should be_greater_than 0 + # should I use a View class? + ds.should be_an_instance_of(CouchRest::View) + + # ds.rows = [] + # ds.rows.include?(...) + # ds.total_rows + end + end + + describe "deleting a database" do it "should start with the test database" do @cr.databases.should include('couchrest-test') end it "should delete the database" do db = @cr.database('couchrest-test') - r = db.delete! - r.should == "" + # r = + db.delete! + # r['ok'].should == true @cr.databases.should_not include('couchrest-test') end end