getting into view land

This commit is contained in:
Chris Anderson 2008-03-19 08:57:20 -07:00
parent d356e2356a
commit 097ab935c6
4 changed files with 83 additions and 16 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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