-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
60 lines (46 loc) · 1.2 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
Bundler.require(:default, ENV['RACK_ENV'])
require 'active_support/cache'
require 'action_view'
Dotenv.load if defined?(Dotenv)
CITY = ENV['CITY']
STATE = ENV['STATE']
API_KEY = ENV['WUNDERGROUND_API_KEY']
ENDPOINT = 'http://api.wunderground.com/api/%s/conditions/q/%s/%s.json' % [
API_KEY,
URI.escape(STATE),
URI.escape(CITY)
]
Connection = Faraday.new(url: ENDPOINT) do |conn|
conn.response :caching do
ActiveSupport::Cache::MemoryStore.new(expires_in: 300)
end
conn.response :json
conn.response :logger
conn.adapter Faraday.default_adapter
end
class CurrentWeather < Struct.new(:response)
include ActionView::Helpers::DateHelper
SNOW_INDICATOR = "snow".freeze
def self.update(conn)
new(conn.get.body)
end
def snowing?
weather.downcase.include?(SNOW_INDICATOR)
end
def last_updated
Time.at(response['current_observation']['local_epoch'].to_i)
end
def icon_url
response['current_observation']['icon_url']
end
def weather
response['current_observation']['weather']
end
end
helpers do
include ActionView::Helpers::DateHelper
end
get '/' do
@weather = CurrentWeather.update(Connection)
erb :index
end