Permalink
Newer
Older
100644 302 lines (221 sloc) 6 KB
Dec 23, 2008
2
require 'sinatra'
Dec 23, 2008
3
require 'activerecord'
4
require 'erb'
5
require 'yaml'
6
7
require 'openid'
Dec 23, 2008
8
9
require File.expand_path(File.dirname(__FILE__) + '/config/setup')
Dec 23, 2008
10
11
require 'lib/active_record_store/active_record_store'
Dec 26, 2008
12
require 'openid/extensions/sreg'
13
require 'lib/models/user'
Dec 26, 2008
14
require 'lib/models/email'
15
16
require 'pony'
17
require 'lib/session_helper'
Dec 31, 2008
18
require 'lib/authentication'
Dec 23, 2008
19
20
include ActiveRecord
21
include SessionHelper
Dec 31, 2008
23
include Sinatra::Authorization
24
25
enable :sessions
26
Jan 6, 2009
27
before do
Jan 8, 2009
28
#require_administrative_privileges
Jan 6, 2009
29
end
Dec 31, 2008
30
31
helpers do
32
def partial(page, options={})
33
haml page, options.merge!(:layout => false)
34
end
35
end
36
37
not_found do
38
LOGGER.info "Request: #{request.to_yaml}"
39
40
haml :error_404
41
end
42
43
EMAIL_DATE_FORMAT = "%m/%d/%Y"
Dec 30, 2008
44
46
# like a before filter on all actions
47
configure do
Dec 30, 2008
48
LOGGER = Logger.new("log/development.log")
49
ENV['APP_ROOT'] ||= "#{File.dirname(__FILE__)}"
50
51
APP_ROOT = ENV['APP_ROOT']
52
APP_ENV = 'development'
53
54
ActiveRecord::Base.logger = LOGGER
Dec 30, 2008
55
APP_URL = "http://localhost:4567"
56
Dec 30, 2008
57
58
connect_database(:development)
59
end
60
61
configure :production do
62
LOGGER = Logger.new("log/production.log")
63
ENV['APP_ROOT'] ||= "#{File.dirname(__FILE__)}"
64
65
APP_ROOT = ENV['APP_ROOT']
66
APP_ENV = 'production'
67
Dec 30, 2008
68
APP_URL = "http://sixmonthletter.com"
Dec 30, 2008
69
Dec 30, 2008
70
ActiveRecord::Base.logger = LOGGER
Dec 30, 2008
71
72
connect_database(:production)
Dec 30, 2008
73
end
74
Dec 30, 2008
75
configure :test do
76
LOGGER = Logger.new("log/test.log")
77
ENV['APP_ROOT'] ||= "#{File.dirname(__FILE__)}"
78
79
APP_ROOT = ENV['APP_ROOT']
80
APP_ENV = 'test'
81
82
ActiveRecord::Base.logger = LOGGER
83
84
connect_database(:test)
85
end
86
87
Dec 30, 2008
88
error do
Dec 30, 2008
89
error = request.env['sinatra.error']
90
error_string = error.message
91
error_string += error.backtrace.join("\n")
92
93
LOGGER.info "ERROR: #{error_string}"
94
95
haml :error_unknown
99
already_logged_in
100
haml :home
101
end
Dec 23, 2008
102
103
get '/about' do
104
haml :about
105
end
106
107
get '/contact' do
108
haml :contact
109
end
Dec 26, 2008
110
111
# regular registration page
112
get '/signup' do
113
already_logged_in
114
haml :signup
115
end
116
117
post '/create_user' do
118
LOGGER.info "CREATE USER"
119
120
user = User.new({
121
:email => params[:email],
122
:password => params[:password]
123
})
124
125
user.regular_signup = true
126
127
LOGGER.info "User: #{user.to_yaml}"
128
129
LOGGER.info "USer SAved: #{user.save}"
130
LOGGER.info "Errors: #{user.errors.to_yaml}"
131
132
redirect('/')
133
end
Dec 26, 2008
134
135
# regular authentication
136
post '/authenticate' do
137
user = User.authenticate(params[:email], params[:password])
138
if user
139
signin(user)
140
redirect('/new_email')
141
return
142
else
143
haml :signin
144
end
145
end
146
147
# begin open id authentication
148
get '/submit_open_id' do
150
already_logged_in
152
begin
Dec 30, 2008
153
open_id_consumer = OpenID::Consumer.new(session, ActiveRecordStore.new)
154
check_id_request = open_id_consumer.begin(params[:open_id_input])
155
156
sregreq = OpenID::SReg::Request.new
157
158
sregreq.request_fields(["email"], true)
159
check_id_request.add_extension(sregreq)
Dec 26, 2008
160
Dec 30, 2008
162
redirect(check_id_request.redirect_url(APP_URL, "#{APP_URL}/authentication_complete"))
163
rescue OpenID::DiscoveryFailure
164
@error = "Whoa there partner! Are you sure you typed your ID in right like?"
165
haml :signin
166
end
167
end
168
Dec 26, 2008
169
# end open id authentication
170
get '/authentication_complete' do
Dec 26, 2008
171
172
open_id_consumer = OpenID::Consumer.new(session, ActiveRecordStore.new)
Dec 26, 2008
173
oidresp = open_id_consumer.complete(session, "/authentication_complete")
175
identity_url = params["openid.identity"]
176
177
#LOGGER.info "User class: #{User.class.to_s}"
178
179
user = User.find_or_create_by_identity_url(identity_url)
180
181
user.email = params["openid.sreg.email"]
182
user.save
Dec 26, 2008
183
Dec 27, 2008
184
signin(user)
Dec 30, 2008
186
redirect('/new_email')
187
end
Dec 23, 2008
188
Dec 26, 2008
189
get '/new_email' do
Dec 28, 2008
190
requires_login
191
Dec 27, 2008
195
get '/emails' do
Dec 29, 2008
196
requires_login
197
198
@emails = current_user.emails
Dec 27, 2008
199
haml :email_index
200
end
201
202
get '/unsent_emails' do
203
requires_login
204
@emails = current_user.unsent_emails
205
haml :unsent_emails
206
end
207
208
get '/sent_emails' do
209
requires_login
210
211
@emails = current_user.sent_emails
212
haml :sent_emails
213
end
214
Dec 27, 2008
215
get '/emails/:id' do
Dec 29, 2008
216
requires_login
217
218
begin
219
@email = current_user.emails.find(params[:id])
220
rescue ActiveRecord::RecordNotFound
221
set_flash_notice("Sorry there, you can only look at your own stuff right?")
222
redirect('/')
223
end
224
Dec 27, 2008
225
haml :show_email
226
end
227
228
get '/emails/:id/edit' do
229
requires_login
230
231
begin
232
@email = current_user.emails.find(params[:id])
233
haml :edit_email
234
rescue ActiveRecord::RecordNotFound
235
set_flash_notice("Sorry there, you can only look at your own stuff right?")
236
redirect('/')
237
end
238
end
239
240
put '/emails/:id' do
241
requires_login
242
243
begin
244
@email = current_user.emails.find(params[:id])
245
@email.update_attribute(:content, params[:email_content])
246
set_flash_notice("Alternative future calculated")
247
redirect('/unsent_emails')
248
rescue ActiveRecord::RecordNotFound
249
set_flash_notice("Sorry there, you can only look at your own stuff right?")
250
redirect('/')
251
end
252
end
253
254
get '/emails/:id/cancel' do
255
requires_login
256
257
begin
258
@email = current_user.emails.find(params[:id])
259
@email.delete
260
redirect('/unsent_emails')
261
rescue ActiveRecord::RecordNotFound
262
set_flash_notice("Sorry there, you can only look at your own stuff right?")
263
redirect('/')
264
end
265
end
266
267
post '/create_email' do
268
Dec 28, 2008
269
requires_login
271
email = Email.new(:content => params[:email_content], :user_id => current_user.id)
272
273
if email.save
274
# send the email
275
body = "Thanks for using sixmonthletter.com"
276
body += "\n\n"
277
body += "Your letter will be delivered on: #{email.send_on.strftime(EMAIL_DATE_FORMAT)}"
278
body += "\n\n"
279
body += "See you in six months!"
280
281
Thread.new do
282
Pony.mail(:to => current_user.email, :from => 'admin@sixmonthletter.com', :subject => 'Your Six Month Letter', :body => body)
283
end
284
redirect('/email_scheduled')
285
else
286
@error = "Now hold on a minute! You surely don't want to send yourself an empty letter do ya?"
287
haml :new_email
288
end
Dec 27, 2008
289
end
290
291
get '/email_scheduled' do
292
haml :email_scheduled
Dec 26, 2008
293
end
294
296
already_logged_in
Dec 27, 2008
297
haml :signin
298
end
299
300
get '/signout' do
301
signout
302
end