Skip to content

Commit

Permalink
Hook up the Sinatra app to couchdb-lucene rest calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
eee-c committed May 1, 2009
1 parent 2301548 commit 85bdbc6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
14 changes: 12 additions & 2 deletions eee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@
end

get '/recipes/search' do
@query = params[:q]

page = params[:page].to_i
skip = (page < 2) ? 0 : ((page - 1) * 20) + 1
data = RestClient.get "#{@@db}/_fti?limit=20&skip=#{skip}&q=#{params[:q]}"

couchdb_url = "#{@@db}/_fti?limit=20" +
"&q=#{@query}" +
"&skip=#{skip}"

if params[:sort] =~ /\w/
couchdb_url += "&sort=#{params[:sort]}"
end

data = RestClient.get couchdb_url

@results = JSON.parse(data)
@query = params[:q]

if @results['rows'].size == 0 && page > 1
redirect("/recipes/search?q=#{@query}")
Expand Down
17 changes: 17 additions & 0 deletions spec/eee_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,22 @@

get "/recipes/search?q=title:eggs&page=foo"
end

it "should sort" do
RestClient.should_receive(:get).
with(/sort=title/).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')

get "/recipes/search?q=title:egg&sort=title"
end

it "should not sort when no sort field is supplied" do
RestClient.stub!(:get).
and_return('{"total_rows":30,"skip":0,"limit":20,"rows":[]}')

RestClient.should_not_receive(:get).with(/sort=/)

get "/recipes/search?q=title:egg&sort="
end
end
end
2 changes: 1 addition & 1 deletion spec/views/search.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
assigns[:query] = "foo"
render("/views/search.haml")
response.should have_selector("th > a",
:href => "/recipes/search?q=foo&sort=name",
:href => "/recipes/search?q=foo&sort=sort_title",
:content => "Name")
end
end
2 changes: 1 addition & 1 deletion views/search.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%table
%tr
%th
%a{:href => "/recipes/search?q=foo&sort=name", :id => "sort-by-name"} Name
%a{:href => "/recipes/search?q=foo&sort=sort_title", :id => "sort-by-name"} Name
%th= "Date"
- @results['rows'].each_with_index do |result, i|
%tr{:class => "row#{i % 2}"}
Expand Down

0 comments on commit 85bdbc6

Please sign in to comment.