added handling of view params like count etc

This commit is contained in:
Chris Anderson 2008-05-22 21:41:52 -07:00
parent ef639040c3
commit ce5bf9ffe8
2 changed files with 22 additions and 2 deletions

View file

@ -17,8 +17,16 @@ class CouchRest
JSON.parse(RestClient.post("#{@root}/_temp_view", JSON.unparse(funcs), {"Content-Type" => type})) JSON.parse(RestClient.post("#{@root}/_temp_view", JSON.unparse(funcs), {"Content-Type" => type}))
end end
def view name def view name, params = nil
CouchRest.get "#{@root}/_view/#{name}" url = "#{@root}/_view/#{name}"
if params
query = params.collect do |k,v|
v = JSON.unparse(v) if %w{key startkey endkey}.include?(k.to_s)
"#{k}=#{CGI.escape(v.to_s)}"
end.join("&")
url = "#{url}?#{query}"
end
CouchRest.get url
end end
def get id def get id

View file

@ -67,6 +67,18 @@ describe CouchRest::Database do
rs = @db.view('first/test') rs = @db.view('first/test')
rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1 rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1
end end
it "should work with a range" do
rs = @db.view('first/test',{:startkey => "b", :endkey => "z"})
rs['rows'].length.should == 2
end
it "should work with a key" do
rs = @db.view('first/test',{:key => "wild"})
rs['rows'].length.should == 1
end
it "should work with a count" do
rs = @db.view('first/test',{:count => 1})
rs['rows'].length.should == 1
end
end end
describe "GET (document by id) when the doc exists" do describe "GET (document by id) when the doc exists" do