Class: Forecast::Adapters::OpenWeatherMapAdapter

Inherits:
Object
  • Object
show all
Includes:
Forecast::Adapter
Defined in:
lib/forecast/adapters/open_weather_map_adapter.rb

Instance Method Summary collapse

Methods included from Forecast::Adapter

#config, included, #initialize, instance, #options

Instance Method Details

#current(latitude, longitude) ⇒ Object



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

def current(latitude, longitude)
  forecast = nil
  result = get_json(api_url('weather', latitude, longitude))
  if result
    forecast = Forecast.new(latitude: latitude, longitude: longitude)
    forecast.date = Time.at(result['dt']).to_datetime
    forecast.temp = get_temp(kelvin_to_fahrenheit(result['main']['temp'])) 
    result['weather'].each do |obj|
      condition = get_condition(obj['description'])
      if condition != nil
        forecast.condition = condition
        break
      end 
    end
  end
  return forecast
end

#daily(latitude, longitude) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/forecast/adapters/open_weather_map_adapter.rb', line 48

def daily(latitude, longitude)
  forecasts = Forecast::Collection.new
  result = get_json(api_url('forecast/daily', latitude, longitude))
  result['list'].each do |item|
    forecast = Forecast.new(latitude: latitude, longitude:longitude)
    forecast.date = Time.at(item['dt'])
    forecast.temp_min = get_temp(kelvin_to_fahrenheit(item['temp']['min']))
    forecast.temp_max = get_temp(kelvin_to_fahrenheit(item['temp']['max']))
    forecast.temp = (forecast.temp_min + forecast.temp_max) / 2
    item['weather'].each do |obj|
      condition = get_condition(obj['description'])
      if condition != nil
        forecast.condition = condition
        break
      end 
    end
    forecasts << forecast
  end
  return forecasts
end

#hourly(latitude, longitude) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/forecast/adapters/open_weather_map_adapter.rb', line 25

def hourly(latitude, longitude)
  forecasts = Forecast::Collection.new
  result = get_json(api_url('forecast', latitude, longitude))
  if result
    result['list'].each do |item|
      forecast = Forecast.new(latitude: latitude, longitude:longitude)
      forecast.date = Time.at(item['dt']).to_datetime
      forecast.temp = get_temp(kelvin_to_fahrenheit(item['main']['temp']))
      item['weather'].each do |obj|
        condition = get_condition([obj['description'], obj['id']])
        if condition != nil
          forecast.condition = condition
          break
        end 
      end
      # forecast.temp_min = item['main']['temp_min']
      # forecast.temp_max = item['main']['temp_max']
      forecasts << forecast
    end
  end
  return forecasts
end