Class: OWMO::Weather

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

Overview

rdoc A weather class for retrieving current and forecasted weather conditions.

Attributes

Examples

api_key = "<My API Key>"
weather = OWMO::Weather.new api_key: api_key
puts weather.get :current, city_name: "London,uk"

Defined Under Namespace

Classes: WeatherResponseError

Constant Summary collapse

PATHS =

rdoc Access current or forecasted conditions by (required):

{
  current: 'weather', # Current weather data
  group: 'group', # Current weather w/multiple IDs
  box: 'box/city', # Current weather w/in a rectangle box
  circle: 'find', # Current weather w/in a circle
  forecast5: 'forecast', # 5 day / 3 hour forecast
  forecast16: 'forecast/daily' # 16 day / daily forecast
}.freeze
GEOCODE_ALIASES =

rdoc Geocode aliases

{
  city_name: :q,
  city_id: :id,
  zip_code: :zip,
  latitude: :lat,
  longitude: :lon,
  box: :bbox
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, **_kwargs) {|_self| ... } ⇒ Weather

rdoc Either yeild the class, or instanciate it.

Attributes

  • api_key - OpenWEatherMap.Org’s weather API key

  • **kwargs - Any additional paramters

Yields:

  • (_self)

Yield Parameters:

  • _self (OWMO::Weather)

    the object that the method was called on



58
59
60
61
62
63
64
# File 'lib/owmo/weather.rb', line 58

def initialize(api_key, **_kwargs)
  @api_key = api_key

  return unless block_given?

  yield self
end

Instance Attribute Details

#api_keyObject (readonly)

rdoc OpenWeatherMap.Org weather API key



29
30
31
# File 'lib/owmo/weather.rb', line 29

def api_key
  @api_key
end

Instance Method Details

#get(path, **query) ⇒ Object

rdoc A weather class for retrieving current and forecasted weather conditions.

Attributes

  • path - OWMO::Wether.Path parameter

  • query - Hash of query options (Geocode, response format, units, etc.)

Examples

api_key = "<My API Key>"
weather = OWMO::Weather.new api_key: api_key
puts weather.get :current, city_name: "London,uk"


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/owmo/weather.rb', line 76

def get(path, **query)
  # Format Geocode info
  query = alias_geocodes(**query)

  # Add the api key
  query[:APPID] = api_key

  # Create the uri
  uri = format_uri(OWMO::URL, PATHS[path], query)

  # Get the weather data
  http_response = http_get(uri)

  weather_response = WeatherResponse.new(http_response)

  raise(WeatherResponseError, weather_response) if weather_response.error?

  weather_response.weather
end