-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.rb
35 lines (32 loc) · 873 Bytes
/
request.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
module Api
module OpenWeatherMap
class Request
attr_accessor :query
def initialize(lat, lon)
@query = {
lat: lat,
lon: lon,
units: 'metric',
lang: 'ja',
appid: ENV['WEATHER_API_KEY']
}
end
def request
client = HTTPClient.new
request = client.get(ENV['URI'], query)
JSON.parse(request.body)
end
# attrsにはlib/tasks/open_weather_api.rakeの(response['daily'])が入る
def self.attributes_for(attrs)
date = Time.zone.at(attrs[0]['dt'])
{
dated_on: date,
weather_id: attrs[0]['weather'][0]['id'],
weather: attrs[0]['weather'][0]['description'],
weather_icon: attrs[0]['weather'][0]['icon'],
temperature: attrs[0]['temp']['day']
}
end
end
end
end