Class: CoffeeOutside::OWM

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, time = DateTime.now) ⇒ OWM

Returns a new instance of OWM.



9
10
11
12
13
# File 'lib/coffeeoutside/weather.rb', line 9

def initialize(config, time = DateTime.now)
  @city_id = config["city_id"]
  @api_key = config["api_key"]
  @start_time = time
end

Instance Attribute Details

#result ⇒ Object

Returns the value of attribute result.



7
8
9
# File 'lib/coffeeoutside/weather.rb', line 7

def result
  @result
end

Instance Method Details

#api_call ⇒ Object



15
16
17
18
# File 'lib/coffeeoutside/weather.rb', line 15

def api_call
  api = OpenWeatherMap::API.new(@api_key, "en", "metric")
  @result = api.forecast(@city_id)
end

#closest_forecast ⇒ Object



20
21
22
23
24
25
# File 'lib/coffeeoutside/weather.rb', line 20

def closest_forecast
  # Doing this as 'The Price Is Right' rules, forecast with the closest
  # time to the start without going over wins.
  @result.forecast.reject! { |x| x.time.to_datetime > @start_time }
  @result.forecast.last
end

#forecast ⇒ Object



31
32
33
34
35
# File 'lib/coffeeoutside/weather.rb', line 31

def forecast
  api_call
  fc = closest_forecast
  Forecast.new(humidity: fc.humidity, temperature: fc.temperature)
end

#parse_owm_datestring(str) ⇒ Object



27
28
29
# File 'lib/coffeeoutside/weather.rb', line 27

def parse_owm_datestring(str)
  DateTime.strptime(str, "%Y-%m-%d %H-%M-%S")
end