Class: OpenWeatherMap::API

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

Overview

The main API class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, lang = 'en', units = 'default') ⇒ API

Initialize the API object

Parameters:

  • api_key (String)

    your OpenWeatherMap’s API key For further information, go to openweathermap.org/price

  • lang (String) (defaults to: 'en')

    the default lang Refer to OpenWeatherMap::Constants::LANGS for accepted values

  • units (String) (defaults to: 'default')

    the units system to use Accepted values :

    • none (temperatures in Kelvin)

    • metric (temperatures in Celsius)

    • imperial (temperatures in Fahrenheit)

Raises:



35
36
37
38
39
40
41
42
43
# File 'lib/openweathermap/api.rb', line 35

def initialize(api_key, lang = 'en', units = 'default')
  @api_key = api_key

  raise OpenWeatherMap::Exceptions::UnknownLang, "unknown lang #{lang}" unless OpenWeatherMap::Constants::LANGS.include? lang
  @lang = lang

  raise OpenWeatherMap::Exceptions::UnknownUnits, "unknown units #{units}" unless OpenWeatherMap::Constants::UNITS.include? units
  @units = units
end

Instance Attribute Details

#langString (readonly)

The default lang to use across the program

Returns:

  • (String)


12
13
14
# File 'lib/openweathermap/api.rb', line 12

def lang
  @lang
end

#unitsString (readonly)

The default unit system to use across the program

Returns:

  • (String)


18
19
20
# File 'lib/openweathermap/api.rb', line 18

def units
  @units
end

Instance Method Details

#current(location) ⇒ OpenWeatherMap::CurrentWeather

Get current weather at a specific location.

Parameters:

  • location (String, Integer, Array)

    the location Can be one of this type :

Returns:



55
56
57
58
# File 'lib/openweathermap/api.rb', line 55

def current(location)
  data = make_request(OpenWeatherMap::Constants::URLS[:current], location)
  OpenWeatherMap::CurrentWeather.new(data)
end

#forecast(location) ⇒ OpenWeatherMap::Forecast

Get weather forecast for a specific location.

Parameters:

  • location (String, Integer, Array)

    the location Can be one of this type :

    • String : search by city name

    • Integer : search by city ID (refer to bulk.openweathermap.org/sample/city.list.json.gz)

    • Array : search by coordinates (format : [lon, lat])

Returns:



70
71
72
73
# File 'lib/openweathermap/api.rb', line 70

def forecast(location)
  data = make_request(OpenWeatherMap::Constants::URLS[:forecast], location)
  OpenWeatherMap::Forecast.new(data)
end