Class: Weatherr::Client

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

Constant Summary collapse

BASE_URL =
"https://api.openweathermap.org/data/2.5"
ABSOLUTE_ZERO =
-273.15

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/weatherr.rb', line 9

def initialize(opts = {})
  @api_key = opts[:api_key]
end

Instance Method Details

#current(opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/weatherr.rb', line 13

def current(opts = {})
  params = { q: opts[:city_name], appid: @api_key }
  url = "#{BASE_URL}/weather"
  response = HTTP.get(url, params: params).parse
  return {
    temperature: (response['main']['temp'] + ABSOLUTE_ZERO).round(1),
    comment: response['weather'][0]['main']
  }
end