-
Notifications
You must be signed in to change notification settings - Fork 1
/
subway.rb
129 lines (108 loc) · 4.33 KB
/
subway.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
# subway.rb - alexa nyc mta subway
#
require 'sinatra'
require 'nokogiri'
require 'json'
require 'alexa_verifier'
require_relative 'lib/alexa_nyc_mta'
def lines_from_state(lines)
line_text = String.new
# for line in delayed_lines do
lines.each_with_index do |line, idx|
if (idx != (lines.count-1))
line_text << line.gsub(/(.{1})/, '\1 ')
if (idx != lines.count-2) then line_text << ", " end
puts "idx is #{idx} lines count is #{lines.count}"
puts line
else
line_text << " and the " + line.gsub(/(.{1})/, '\1 ')
puts line
end
end
line_text
end
verifier = AlexaVerifier.build do |c|
c.verify_signatures = true
c.verify_timestamps = true
c.timestamp_tolerance = 60 # seconds
end
get '/subway/' do
"app is alive"
end
post '/subway/v1.0' do
begin
subway = AlexaNYCMTA.new
status = subway.subway_status
request_body_unparsed = request.body.read
authentic = verifier.verify!(
request.env['HTTP_SIGNATURECERTCHAINURL'],
request.env['HTTP_SIGNATURE'],
request_body_unparsed
)
puts "authenticity is #{authentic}"
request_body = JSON.parse request_body_unparsed
puts "request body is #{request_body}"
# puts "subway #{status}"
intent = request_body["request"]["intent"]["name"] if request_body["request"]["intent"]
puts "intent #{intent}"
say_this = ""
if (intent == "GetAllStatus" || intent == nil)
good_lines = status.select { |key, value| value.match("GOOD SERVICE")}.transpose[0]
delayed_lines = status.select { |key, value| value.match("DELAYS")}.transpose[0]
planned_work_lines = status.select { |key, value| value.match("PLANNED WORK")}.transpose[0]
service_change_lines = status.select { |key, value| value.match("SERVICE CHANGE")}.transpose[0]
puts "good lines are #{good_lines}"
if (delayed_lines || planned_work_lines || service_change_lines)
say_this = "ok. here's the situation... "
if (delayed_lines && delayed_lines.count > 0)
if (delayed_lines.count == 1)
delayed_line = delayed_lines[0].gsub(/(.{1})/, '\1 ')
say_this << "The #{delayed_line} is currently experiencing delays. "
else
say_this << "The #{lines_from_state(delayed_lines)} are currently experiencing delays. "
end
end
if (planned_work_lines && planned_work_lines.count > 0)
puts "planned_work_lines are #{planned_work_lines}"
if (planned_work_lines.count == 1)
planned_work_line = planned_work_lines[0].gsub(/(.{1})/, '\1 ')
say_this << "The #{planned_work_line} is currently undergoing planned work. "
else
say_this << "The #{lines_from_state(planned_work_lines)} are currently undergoing planned work. "
end
end
if (service_change_lines && service_change_lines.count > 0)
if (service_change_lines.count == 1)
service_change_line = service_change_lines[0].gsub(/(.{1})/, '\1 ')
say_this << "The #{service_change_line} has a major service change. "
else
say_this << "The #{lines_from_state(service_change_lines)} have major service changes. "
end
end
say_this << "All other lines are currently running smoothly."
else
say_this = "ok. Great news. "
say_this << "All lines are currently running smoothly."
end
#say_this = "The #{delayed_lines}"
else
say_this = "I can't do status of individual lines yet. Please check back in a few days."
end
rescue
say_this = "rut roh. something failed"
end
text = {"outputSpeech" => {"type" => "PlainText", "text" => say_this}}
if (authentic)
response = {
"version" => "1.0",
"response" => text,
"shouldEndSession" => true
}
else
response = {
"error" => "your response does not appear to be from a real alexa"
}
end
puts "response: #{response.to_json}"
response.to_json
end