diff --git a/lib/database.rb b/lib/database.rb index 5ac534e..9346a55 100644 --- a/lib/database.rb +++ b/lib/database.rb @@ -17,8 +17,16 @@ class CouchRest JSON.parse(RestClient.post("#{@root}/_temp_view", JSON.unparse(funcs), {"Content-Type" => type})) end - def view name - CouchRest.get "#{@root}/_view/#{name}" + def view name, params = nil + 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 def get id diff --git a/spec/database_spec.rb b/spec/database_spec.rb index f6058b5..f41709f 100644 --- a/spec/database_spec.rb +++ b/spec/database_spec.rb @@ -67,6 +67,18 @@ describe CouchRest::Database do rs = @db.view('first/test') rs['rows'].select{|r|r['key'] == 'wild' && r['value'] == 'and random'}.length.should == 1 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 describe "GET (document by id) when the doc exists" do