-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.rb
executable file
·170 lines (144 loc) · 4.32 KB
/
app.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
require 'rubygems'
require 'bundler'
Bundler.setup
# Load before Sinatra
require 'compass' # must be loaded before sinatra
# Load Sinatra
require 'sinatra'
require 'lib/render_partial'
require 'haml' # must be loaded after sinatra
require 'ninesixty'
# Set Sinatra's variables (cucumber needs them)
set :app_file, __FILE__
set :root, File.dirname(__FILE__)
set :views, 'views'
set :public, 'public'
configure do
Compass.configuration.parse(File.join(Sinatra::Application.root, 'config.rb'))
enable :sessions
require 'rack/flash'
use Rack::Flash, :sweep => false
end
# Load models
require 'db/setup'
configure :development, :cucumber do
require 'ruby-debug'
DataMapper.auto_migrate!
end
configure :development do
set :host, "http://local.local"
set :subdirectory, "/swal"
end
configure :cucumber do
set :host, "http://localhost"
set :subdirectory, ""
end
configure :production do
set :host, "http://bmoreonrails.org"
set :subdirectory, "/stay-with-a-local"
end
require 'mailer'
configure :development do
require 'db/seeds'
require 'lib/mail_hijack'
end
require 'lib/twitter_auth'
include TwitterAuth
def require_unbooked_guest
if !logged_in?
flash[:error] = "You must be logged into twitter to reserve a room."
elsif booked?
flash[:error] = "You've already booked a room!"
else
return true
end
redirect url_for("/")
halt
end
def require_unbooked_host
@host = Host.get(params[:id])
if @host.available_rooms < 1
flash[:error] = "#{@host.name} no longer has any rooms available."
redirect url_for("/")
halt
end
end
def require_valid_room_request
@room_request = RoomRequest.get(params[:id])
if @room_request.guest.booked? && @room_request.guest.host != @room_request.host
flash[:error] = "#{@room_request.guest.twitter} has already booked a room with #{@room_request.guest.host.name}"
elsif @room_request.host.available_rooms < 1
flash[:error] = "You have already approved room requests for all of your rooms"
elsif !@room_request.pending?
flash[:error] = "You have already processed the room request from #{@room_request.guest.twitter}"
elsif @room_request.token != params[:token]
flash[:error] = "Unable to find this request"
else
return true
end
redirect url_for("/")
halt
end
helpers do
def booked?
@guest = Guest.get(session[:guest_id])
@guest && @guest.booked?
end
def can_reserve?
logged_in? && !booked?
end
def guest_name
@guest = Guest.get(session[:guest_id])
@guest && @guest.twitter
end
def url_for(rooted_path)
"#{Sinatra::Application.subdirectory}#{rooted_path}"
end
def full_url_for(rooted_path)
"#{Sinatra::Application.host}#{url_for(rooted_path)}"
end
end
# At a minimum the main sass file must reside within the views directory
# We create /views/stylesheets where all our sass files can safely reside
get '/stylesheets/:name.css' do
content_type 'text/css', :charset => 'utf-8'
sass(:"stylesheets/#{params[:name]}", Compass.sass_engine_options)
end
get '/?' do
haml :index, :layout => :'/layouts/page'
end
get '/twitter_callback' do
login_from_twitter
redirect url_for("/")
end
get '/twitter' do
save_token_and_redirect_to_twitter
end
get '/logout' do
session.delete(:guest_id)
redirect url_for("/")
end
get '/hosts/:id/room_requests/new' do
require_unbooked_guest
require_unbooked_host
haml :'room_requests/new', :layout => :'/layouts/page'
end
post '/hosts/:id/room_requests' do
require_unbooked_guest
require_unbooked_host
room_request = RoomRequest.create :host => @host, :guest => @guest, :comments => params[:comments], :email => params[:email]
flash[:notice] = "You have submitted a room request to #{@host.name}. You will receive email confirmation when the request has been accepted or declined."
redirect url_for("/")
end
get '/room_requests/:id/accept/:token' do
require_valid_room_request
@room_request.accept
flash[:notice] = "You have accepted a room request from #{@room_request.guest.name}. Rooms you have available: #{@room_request.host.available_rooms}"
redirect url_for("/")
end
get '/room_requests/:id/decline/:token' do
require_valid_room_request
@room_request.decline
flash[:notice] = "You have declined a room request from #{@room_request.guest.name}. Rooms you have available: #{@room_request.host.available_rooms}"
redirect url_for("/")
end