Class: Weather::ForecastApi

Inherits:
Object
  • Object
show all
Defined in:
lib/forecastapi.rb

Constant Summary collapse

APP_ID =
ENV['API_KEY']

Class Method Summary collapse

Class Method Details

.fetch(zipcode) ⇒ Object

fetch forecast info from OpenWeather API



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/forecastapi.rb', line 8

def self.fetch(zipcode)
    url = "http://api.openweathermap.org/data/2.5/forecast?zip=#{zipcode}&units=imperial&appid=#{APP_ID}"

    response = HTTParty.get(url) 
    query = JSON.parse(response.body, symbolize_names: true)
    query[:list].each do |data|
        Weather::Forecast.new(
            date: Time.at(data[:dt]).strftime('%a %b-%d %H:%M'),
            temp: data[:main][:temp],
            humidity: data[:main][:humidity],
            description: data[:weather].first[:description]
            )
        end
    
    Weather::Forecast.all.each {|forecast| forecast.location = query[:city][:name]}

end