-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.rb
executable file
·147 lines (117 loc) · 2.85 KB
/
application.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
require "rubygems"
require "dotenv/load"
require "open-uri"
require "bundler"
require "dalli"
require "net/http"
require "forecast_io"
require "./bryant_park_api"
Bundler.require :default, (ENV["RACK_ENV"] || "development").to_sym
ForecastIO.configure do |configuration|
configuration.api_key = ENV.fetch("DARK_SKY_API_KEY")
end
LAWN_OPEN_MESSAGES = [
"The Lawn is open"
]
class Lawn
WEATHER_ICONS = {
"clear-day" => "☀️",
"rain" => "☔️",
"snow" => "❄️",
"sleet" => "🌨",
"wind" => "💨",
"cloudy" => "⛅️",
"partly-cloudy-day" => "🌤",
}
def initialize
json = BryantParkApi.json
@page = json["page"]
end
def message
"#{lawn_status} #{page["lawnClosedExplanation"].strip}\n#{weather}"
end
def lawn_status
page["lawnStatus"]
end
def open?
LAWN_OPEN_MESSAGES.include? lawn_status
end
def weather
forecast = ForecastIO.forecast(40.753597, -73.983231, params: {
exclude: "hourly,daily,alerts,flags",
})
temp_f = forecast.currently.temperature.round
temp_c = ((temp_f - 32) / 1.8).round
humidity = (forecast.currently.humidity * 100).round
weather_icon = WEATHER_ICONS.fetch(forecast.currently.icon, "")
"#{weather_icon} #{forecast.minutely.summary} #{temp_f}°F/#{temp_c}°C/#{humidity}% humidity"
end
def to_json
{
open: open?,
message: message,
timestamp: Time.now
}.to_json
end
private
attr_reader :page
end
get "/" do
cache_control :public, max_age: 300 # 5 mins.
lawn = Lawn.new
@lawn_message = lawn.message
if lawn.open?
@open = "Yes"
else
@open = "No"
end
slim :index
end
get "/api" do
cache_control :public, max_age: 300 # 5 mins.
content_type :json
Lawn.new.to_json
end
get "/stylesheets/:name.css" do
cache_control :public, max_age: 1800 # 30 mins.
scss :"/stylesheets/#{params[:name]}"
end
get "/flush" do
BryantParkApi.clear
"ok"
end
get "/lawn-webcam.jpg" do
content_type 'image/jpeg', charset: "utf-8"
uri = URI('http://webcam.bryantpark.org/axis-cgi/jpg/image.cgi?resolution=1920x1080')
lawn = Lawn.new
send_lawn_image(lawn, uri)
end
get "/lawn-webcam-thumb.jpg" do
content_type 'image/jpeg', charset: "utf-8"
uri = URI('http://webcam.bryantpark.org/axis-cgi/jpg/image.cgi?resolution=640x480')
lawn = Lawn.new
send_lawn_image(lawn, uri)
end
helpers do
def send_lawn_image(lawn, uri)
begin
head = Net::HTTP.start(uri.host, uri.port) do |http|
http.head(uri.request_uri)
end
if head.code == "200"
Net::HTTP.get(uri)
else
send_default_lawn_image(lawn)
end
rescue StandardError
send_default_lawn_image(lawn)
end
end
def send_default_lawn_image(lawn)
if lawn.open?
send_file 'public/images/open-min.jpg'
else
send_file 'public/images/closed-min.jpg'
end
end
end