Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 137 lines (109 sloc) 2.959 kB
f2ba94d @bryckbost Add root path for html request, scope json requests to /api
authored
1 # encoding: utf-8
f6ed877 @bryckbost Add sinatra app
authored
2 require 'bundler/setup'
3 Bundler.require
4 require 'sinatra/mongo'
2dc1ff6 @bryckbost Add index tank search. Feels a little slow though :\
authored
5 require 'sinatra/indextank'
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
6 require 'active_support/core_ext/string/inflections'
f6ed877 @bryckbost Add sinatra app
authored
7 MultiJson.engine = :yajl
8
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
9 if File.exist?('config/application.yml')
10 config = YAML.load_file('config/application.yml')
11 config.each{|k,v| ENV[k] = v }
12 end
13
9544715 @bryckbost It's MONGOHQ_URL, dummy.
authored
14 set :mongo, ENV['MONGOHQ_URL']
2dc1ff6 @bryckbost Add index tank search. Feels a little slow though :\
authored
15 set :indextank, ENV['INDEXTANK_API_URL']
f6ed877 @bryckbost Add sinatra app
authored
16
c079ce9 @bryckbost Add new relic rpm
authored
17 configure :production do
18 require 'newrelic_rpm'
19 end
20
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
21 helpers do
34208b4 @bryckbost Massive updates to the index page. Fast facts, twipsys, and more.
authored
22 def friendly_name(liquor)
23 if liquor && liquor["BRAND NAME"]
24 liquor["BRAND NAME"].downcase.titleize
25 else
26 ""
27 end
28 end
29
30 def price_per_milliliter(liquor)
31 liquor["PRICE"] / liquor["SIZE"]
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
32 end
f8281b0 @bryckbost Add pagination, limit to home page and page routes
authored
33
34 def per_page
35 150
36 end
37
38 def current_page
39 [params[:page].to_i, 1].max
40 end
41
42 def number_of_pages
43 liquor_count / per_page
44 end
45
46 def liquor_count
47 @liquor_count ||= mongo["liquors"].find().count
48 end
49
50 def previous_page
51 current_page - 1
52 end
53
54 def next_page
55 current_page + 1
56 end
610dda9 @bryckbost Some more style tweaks.
authored
57
58 def scope(options={})
59 mongo["liquors"].find(options, :sort => [['BRAND NAME', 1]])
60 end
34208b4 @bryckbost Massive updates to the index page. Fast facts, twipsys, and more.
authored
61
62 def most_expensive
63 mongo["liquors"].find({}, :sort => [['MINIMUM', -1]]).limit(1).first
64 end
65
66 def cheapest
67 mongo["liquors"].find({}, :sort => [['MINIMUM', 1]]).limit(1).first
68 end
69
70 def highest_proof
71 mongo["liquors"].find({}, :sort => [['PROOF', -1]]).limit(1).first
72 end
73
74 def weakest_proof
75 mongo["liquors"].find({}, :sort => [['PROOF', 1]]).limit(1).first
76 end
77
78 def best_value
79 mongo["liquors"].find({}, :sort => [['VALUE', -1]]).limit(1).first
80 end
81
82 def biggest_ripoff
83 mongo["liquors"].find({}, :sort => [['VALUE', 1]]).limit(1).first
84 end
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
85 end
86
87 # only set the random liquor for non /api endpoints
88 before /^\/((?!api).*)/ do
f8281b0 @bryckbost Add pagination, limit to home page and page routes
authored
89 @header_liquor = mongo["liquors"].find().skip(rand(liquor_count)).limit(1).first
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
90 end
91
f6ed877 @bryckbost Add sinatra app
authored
92 get '/' do
610dda9 @bryckbost Some more style tweaks.
authored
93 @liquors = scope.limit(per_page).to_a
f8281b0 @bryckbost Add pagination, limit to home page and page routes
authored
94 @paginate = true
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
95 erb :index
96 end
97
89a002f @bryckbost Add a liquor show page.
authored
98 get '/liquors/:id' do
99 @liquor = mongo["liquors"].find({"_id" => BSON::ObjectId(params[:id])}).first
100 erb :show
101 end
102
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
103 get '/page/:page' do
610dda9 @bryckbost Some more style tweaks.
authored
104 @liquors = scope.skip(per_page * params[:page].to_i).limit(per_page).to_a
f8281b0 @bryckbost Add pagination, limit to home page and page routes
authored
105 @paginate = true
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
106 erb :index
107 end
108
109 get '/search' do
31c5f56 @bryckbost index tank gem defaults to only 10 results returned. Stupid.
authored
110 results = indextank.indexes('idx').search(params[:q], :len => 500)
2dc1ff6 @bryckbost Add index tank search. Feels a little slow though :\
authored
111 liquors = results["results"].map{|r| BSON::ObjectId(r["docid"])}
112 @liquors = mongo["liquors"].find({"_id" => {"$in" => liquors}}).to_a
f2ba94d @bryckbost Add root path for html request, scope json requests to /api
authored
113 erb :index
114 end
115
5ddd8da @bryckbost Add a category route. Also change one of the headers to show the coun…
authored
116 # GET /vodka, /scotch, /whisky, /bourbon …
117 get '/:category' do
118 @category = params[:category]
119 @liquors = scope({"CATEGORY" => /#{@category}/i}).to_a
120 erb :index
121 end
122
f2ba94d @bryckbost Add root path for html request, scope json requests to /api
authored
123 get '/api/all' do
f6ed877 @bryckbost Add sinatra app
authored
124 content_type :json
125 MultiJson.encode mongo["liquors"].find().to_a
126 end
127
f2ba94d @bryckbost Add root path for html request, scope json requests to /api
authored
128 get '/api/search' do
f6ed877 @bryckbost Add sinatra app
authored
129 content_type :json
9cb726f @bryckbost Add a friendly name helper. Add page and search routes.
authored
130 MultiJson.encode mongo["liquors"].find({"BRAND NAME" => /#{params[:q]}/i}).to_a
f6ed877 @bryckbost Add sinatra app
authored
131 end
132
e647fd7 @bryckbost Add category endpoint to api
authored
133 get '/api/:category' do
134 content_type :json
135 MultiJson.encode mongo["liquors"].find({"CATEGORY" => /#{params[:category]}/i}).to_a
136 end
Something went wrong with that request. Please try again.