eee-c / eee-code

source code for the couchdb based version of eeecooks.com

eee-code / eee.rb
100644 252 lines (196 sloc) 5.845 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require 'rubygems'
require 'sinatra'
require 'rest_client'
require 'json'
require 'pony'
require 'rss'
require 'helpers'
require 'pp'
 
$: << File.expand_path(File.dirname(__FILE__) + '/lib')
 
require 'float'
 
AMAZON_ASSOCIATE_ID = "eeecooks-20"
ROOT_URL = "http://www.eeecooks.com"
DEFAULT_QUERY = "type:Recipe"
 
configure :test do
  @@db = "http://localhost:5984/eee-test"
end
 
configure :development, :production do
  @@db = "http://localhost:5984/eee"
end
 
helpers do
  include Eee::Helpers
end
 
if ENV['RACK_ENV'] != 'test'
  before do
    content_type 'text/html', :charset => 'UTF-8'
  end
end
 
get '/' do
  url = "#{@@db}/_design/meals/_view/by_date?limit=13&descending=true"
  data = RestClient.get url
  @meal_view = JSON.parse(data)['rows']
 
  @meals = @meal_view.inject([]) do |memo, couch_rec|
    data = RestClient.get "#{@@db}/#{couch_rec['id']}"
    meal = JSON.parse(data)
    memo + [meal]
  end
 
  @older_meals = @meals.slice!(10,3) || []
 
  haml :index
end
 
get '/main.rss' do
  content_type "application/rss+xml"
 
  rss_for_date_view("meals") do |rss_item, meal|
    date = Date.parse(meal['date'])
    rss_item.link = ROOT_URL + date.strftime("/meals/%Y/%m/%d")
  end
end
 
get '/recipes.rss' do
  content_type "application/rss+xml"
 
  rss_for_date_view("recipes") do |rss_item, recipe|
    rss_item.link = ROOT_URL + "/recipes/#{url_from_permalink(recipe['_id'])}"
  end
end
 
# get '/benchmarking' do
# url = "#{@@db}/_design/meals/_view/by_date_short"
# data = RestClient.get url
# JSON.parse(data)
 
# ""
# end
 
get %r{/meals/(\d+)/(\d+)/(\d+)} do |year, month, day|
  data = RestClient.get "#{@@db}/#{year}-#{month}-#{day}"
  @meal = JSON.parse(data)
  etag(@meal['_rev'])
 
  url = "#{@@db}/_design/meals/_view/by_date_short"
  data = RestClient.get url
  @meals_by_date = JSON.parse(data)['rows']
 
  @recipes = @meal['menu'].map { |m| wiki_recipe(m) }.compact
 
  @url = request.url
 
  @title = @meal['title'] + ' (Meal)'
  haml :meal
end
 
get %r{/meals/(\d+)/(\d+)} do |year, month|
  url = "#{@@db}/_design/meals/_view/by_date?" +
    "startkey=%22#{year}-#{month}-00%22&" +
    "endkey=%22#{year}-#{month}-99%22"
  data = RestClient.get url
  @meals = JSON.parse(data)['rows'].map{|r| r['value']}
  @month = "#{year}-#{month}"
 
  url = "#{@@db}/_design/meals/_view/count_by_month?group=true"
  data = RestClient.get url
  @count_by_year = JSON.parse(data)['rows']
 
  @title = "Meals from #{year}-#{month}"
  haml :meal_by_month
end
 
get %r{/meals/(\d+)} do |year|
  url = "#{@@db}/_design/meals/_view/by_date?" +
    "startkey=%22#{year}-00-00%22&" +
    "endkey=%22#{year}-99-99%22"
  data = RestClient.get url
  @meals = JSON.parse(data)['rows'].map{|r| r['value']}
  @year = year
 
  url = "#{@@db}/_design/meals/_view/count_by_year?group=true"
  data = RestClient.get url
  @count_by_year = JSON.parse(data)['rows']
 
  @title = "Meals from #{year}"
  haml :meal_by_year
end
 
get %r{/mini/(.*)} do |date_str|
  url = "#{@@db}/_design/meals/_view/count_by_month?group=true"
  data = RestClient.get url
  @count_by_month = JSON.parse(data)['rows']
  @month = date_str == '' ? @count_by_month.last['key'] : date_str.sub(/\//, '-')
 
  url = "#{@@db}/_design/meals/_view/by_date_short?" +
    "startkey=%22#{@month}-00%22&" +
    "endkey=%22#{@month}-99%22"
  data = RestClient.get url
  @meals_by_date = JSON.parse(data)['rows'].inject({ }) do |memo, m|
    memo[m['value']['date']] = m['value']['title']
    memo
  end
 
  haml :mini_calendar, :layout => false
end
 
get '/recipes/search' do
  @query = params[:q] == '' ? DEFAULT_QUERY : params[:q]
  @sort = params[:q] == '' ? "%5Csort_date" : params[:sort]
 
  page = params[:page].to_i
  skip = (page < 2) ? 0 : ((page - 1) * 20)
 
  couchdb_url = "#{@@db}/_fti?limit=20" +
    "&q=#{Rack::Utils.escape(@query)}" +
    "&skip=#{skip}"
 
  if @sort =~ /\w/
    order = params[:order] =~ /desc/ ? "%5C" : ""
    couchdb_url += "&sort=#{order}#{@sort}"
  end
 
  begin
    data = RestClient.get couchdb_url
    @results = JSON.parse(data)
  rescue Exception
    #puts Rack::Utils.escape(@query)
    @query = ""
    @results = { 'total_rows' => 0, 'rows' => [] }
  end
 
  if @results['rows'].count == 0 && page > 1
    redirect("/recipes/search?q=#{@query}")
    return
  end
 
  @title = "Recipes"
  haml @results['total_rows'] == 0 ? :no_results : :search
end
 
get %r{/recipes/(\d+)/(\d+)/(\d+)/?(.*)} do |year, month, day, short_name|
  data = RestClient.get "#{@@db}/#{year}-#{month}-#{day}-#{short_name}"
  @recipe = JSON.parse(data)
  etag(@recipe['_rev'])
 
  url = "#{@@db}/_design/recipes/_view/by_date_short"
  data = RestClient.get url
  @recipes_by_date = JSON.parse(data)['rows']
 
  @url = request.url
 
  @title = @recipe['title'] + ' (Recipe)'
  haml :recipe
end
 
get '/images/:permalink/:image' do
  content_type 'image/jpeg'
  begin
    RestClient.get "#{@@db}/#{params[:permalink]}/#{params[:image]}"
  rescue
    404
  end
end
 
get '/feedback' do
  @subject = params[:subject]
  @url = params[:url]
  haml :feedback
end
 
post '/email' do
  message = <<"_EOM"
From: #{params[:name]}
Email: #{params[:email]}
 
#{params[:message]}
_EOM
 
  message << "\nURL: #{params[:url]}\n" if params[:url]
 
  Pony.mail(:to => "us _at_ eeecooks.com".gsub(/\s*_at_\s*/, '@'),
            :subject => params[:subject],
            :body => message)
 
  haml :email
end
 
get '/ingredients' do
  url = "#{@@db}/_design/recipes/_view/by_ingredients?group=true"
  data = RestClient.get url
  @ingredients = JSON.parse(data)['rows']
 
  @title = "Ingredient Index"
 
  haml :ingredients
end
 
get %r{^/([\-\w]+)$} do |doc_id|
  begin
    data = RestClient.get "#{@@db}/#{doc_id}"
    @doc = JSON.parse(data)
  rescue RestClient::ResourceNotFound
    pass
  end
  haml RedCloth.new(@doc['content']).to_html
end
 
not_found do
  haml :'404'
end
 
error do
  haml :'500'
end