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 = nil) ⇒ 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: nil)

    the units system to use Accepted values :

    • none (temperatures in Kelvin)

    • metric (temperatures in Celsius)

    • imperial (temperatures in Fahrenheit)

Raises:



23
24
25
26
27
28
29
30
31
# File 'lib/openweathermap/api.rb', line 23

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

  raise OpenWeatherMap::Exceptions::UnknownLang, "[owm-ruby] error : unknown lang #{lang}" unless OpenWeatherMap::Constants::LANGS.include? lang
  @lang = lang

  raise OpenWeatherMap::Exceptions::UnknownUnits, "[owm-ruby] error : unknown units #{units}" unless OpenWeatherMap::Constants::UNITS.include? units
  @units = units
end

Instance Attribute Details

#langString

Returns Default lang to use.

Returns:

  • (String)

    Default lang to use



5
6
7
# File 'lib/openweathermap/api.rb', line 5

def lang
  @lang
end

#unitsString

Returns Default units to use.

Returns:

  • (String)

    Default units to use



8
9
10
# File 'lib/openweathermap/api.rb', line 8

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 :

    • 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:



41
42
43
44
# File 'lib/openweathermap/api.rb', line 41

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:



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

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