-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
176 lines (131 loc) · 4.22 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
171
172
173
174
175
176
require 'dotenv'
require 'sinatra'
require 'sinatra/activerecord'
require './config/environments'
require './models/user'
require 'mbta-realtime'
class TextBus < Sinatra::Base
Dotenv.load
@@mbta = MBTARealtime::Client.new(api_key: ENV['MBTA'])
enable :sessions
enable :partial_underscores
get '/' do
@number = params[:From]
@input = params[:Body]
@user = User.find_or_create_by(phone_number: @number)
@user.update_attribute(:total_sent, @user.total_sent += 1)
session[:input] = @input
session[:user] = @user
redirect to ("/help") if @input.match /^\s*(h(e|a)lp[\?\!]*|[\?\!]+)\s*$/i
redirect to ("/help/#{$1}") if @input.match /^\s*([abcde]{1})\s*$/i
redirect to ("save/#{$1}") if @input.match /^\s*save\s{1}(\d+)$/i
redirect to ("remove/#{$1}") if @input.match /^\s*remove\s{1}(\d+)$/i
redirect to ("/stops") if @input.match /^\s*stops\s*$/i
redirect to ("/count") if @input.match /^\s*count\s*$/i
redirect to ("/settings") if @input.match /^\s*settings\s*$/i
redirect to ("/contact") if @input.match /^\s*contact\s*$/i
# Send request to /address for geocoding and searching
if mostly_text(@input)
puts "I AM REDIRECTING TO ADDRESS #{@input}"
stop_id = nil
begin
stop_id = @@mbta.nearest_stop_id_by_location_name(@input)
redirect to ("/stop/#{stop_id}")
rescue
redirect to("/no-match")
end
end
@input = @input.split(" ")
stop_id, routes = @input.shift, @input.join(',')
redirect to("/stop/#{stop_id}?routes=#{routes}") if routes
redirect to("/stop/#{stop_id}")
end
get '/help' do
haml :help_index
end
get '/help/:help_id' do
page = params[:help_id]
haml :"help/#{page}"
end
get '/settings' do
haml "We haven't implemented the settings menu yet. Check back soon."
end
get '/contact' do
haml "We haven't implemented the contact page yet. Check back soon."
end
get '/stop/:stop_id' do
stop_id = params[:stop_id]
@stop = OpenStruct.new({id: stop_id,
name: @@mbta.stop_name(stop_id)})
@routes = params[:routes] ? params[:routes].split(',') : []
@user = session[:user] || User.find("+15087692973")
begin
@predictions = @@mbta.flat_complex_predictions(@stop.id, @routes)
rescue NoMethodError
redirect to('/no-predictions')
ensure
puts @predictions
end
@time = Time.now.strftime("%I:%M %P")
haml :"predictions/all"
end
get "/no-predictions" do
haml "Sorry, there are no predictions available for that stop and/or route presently."
end
get "/stops" do
@user = session[:user] || User.find("+15087692973")
@stops = @user.saved_stops
if @stops.empty?
haml :no_stops
else
@stops = @stops.map {|s| OpenStruct.new(id: s, name: @@mbta.stop_name(s)) }
haml :user_stops
end
end
get "/save/:stop_id" do
@user = session[:user] || User.find("+15087692973")
@stop = OpenStruct.new({id: params[:stop_id],
name: @@mbta.stop_name(params[:stop_id]) })
saved_stops = @user.saved_stops + [ @stop.id ]
@user.saved_stops = saved_stops.uniq
if @user.save
haml :save_success
else
redirect to('/no-match')
end
end
get "/remove/:stop_id" do
@user = session[:user] || User.find("+15087692973")
@stop = OpenStruct.new({id: params[:stop_id],
name: @@mbta.stop_name(params[:stop_id]) })
saved_stops = @user.saved_stops.delete(@stop.id)
@user.saved_stops = saved_stops
if @user.save
haml :remove_success
else
redirect to('/no-match')
end
end
get "/count" do
@user = session[:user] || User.find("+15087692973")
haml "You have sent #{@user.total_sent} messages to TextBus."
end
get '/no-match' do
haml "Sorry, we couldn't find that stop."
end
error 500 do
haml :"500"
end
error 404 do
haml :"404"
end
private
def mostly_numbers(string)
is_digit = /^\d{1}$/
arr = string.split('')
arr.count {|e| e.match is_digit } / arr.length.to_f > 0.6
end
def mostly_text(string)
!mostly_numbers(string)
end
end