-
Notifications
You must be signed in to change notification settings - Fork 3
/
eee.rb
172 lines (132 loc) · 3.71 KB
/
eee.rb
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
require 'rubygems'
require 'sinatra'
require 'rest_client'
require 'json'
require 'pony'
require 'rss'
require 'helpers'
require 'pp'
ROOT_URL = "http://www.eeecooks.com"
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
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/#{recipe['id']}"
end
end
get %r{/meals/(\d+)/(\d+)/(\d+)} do |year, month, day|
data = RestClient.get "#{@@db}/#{year}-#{month}-#{day}"
@meal = JSON.parse(data)
url = "#{@@db}/_design/meals/_view/by_date"
data = RestClient.get url
@meals_by_date = JSON.parse(data)['rows']
@recipes = @meal['menu'].map { |m| wiki_recipe(m) }.compact
@url = request.url
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']
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']
haml :meal_by_year
end
get '/recipes/search' do
@query = params[:q]
page = params[:page].to_i
skip = (page < 2) ? 0 : ((page - 1) * 20)
couchdb_url = "#{@@db}/_fti?limit=20" +
"&q=#{@query}" +
"&skip=#{skip}"
if params[:sort] =~ /\w/
order = params[:order] =~ /desc/ ? "%5C" : ""
couchdb_url += "&sort=#{order}#{params[:sort]}"
end
begin
data = RestClient.get couchdb_url
@results = JSON.parse(data)
rescue Exception
@query = ""
@results = { 'total_rows' => 0, 'rows' => [] }
end
if @results['rows'].count == 0 && page > 1
redirect("/recipes/search?q=#{@query}")
return
end
haml @results['total_rows'] == 0 ? :no_results : :search
end
get '/recipes/:permalink' do
data = RestClient.get "#{@@db}/#{params[:permalink]}"
@recipe = JSON.parse(data)
@url = request.url
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