-
Notifications
You must be signed in to change notification settings - Fork 0
/
smhi.rb
48 lines (39 loc) · 1.25 KB
/
smhi.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
module SMHI
OneForecast = Struct.new :time, :celsius, :symbol do
def day_or_night
time.day_or_night?
end
end
def weather_url lat, lon
"https://opendata-download-metfcst.smhi.se/api/category/pmp3g/version/2/geotype/point/lon/#{lon}/lat/#{lat}/data.json"
end
def parse_raw_into_forecasts smhi_data, count
time_series = smhi_data['timeSeries']
all_forecasts = time_series.map do |one_point_in_time|
params = one_point_in_time['parameters']
forecast = OneForecast.new
forecast.time = DateTime.parse( one_point_in_time['validTime'] )
forecast.celsius = find_celsius params
forecast.symbol = find_symbol params
forecast
end
future_forecasts = all_forecasts.select { |forecast| forecast.time.is_future? }
future_forecasts[0..count]
end
def find_celsius params
find_value 't', params
end
def find_symbol params
symbol = find_value 'Wsymb2', params
symbol.to_i
end
def find_value name, params
the_one = params.detect { |p| p['name'] == name }
the_one['values'].first
end
def hourly_forecasts lat, lon, count=20
response = RestClient.get weather_url(lat, lon)
the_data = JSON.parse response
parse_raw_into_forecasts the_data, count
end
end