class OWMO::Weather

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"

Constants

Geocode_Aliases

Geocode aliases

Paths

Access current or forecasted conditions by (required):

Attributes

api_key[R]

OpenWeatherMap.Org weather API key

Public Class Methods

new(api_key, **kwargs) { |self| ... } click to toggle source

Either yeild the class, or instanciate it.

Attributes

  • api_key - OpenWEatherMap.Org's weather API key

  • +**kwargs+ - Any additional paramters

# File lib/owmo/weather.rb, line 71
def initialize(api_key, **kwargs)
  @api_key = api_key

  if block_given?
    yield self
  end
end

Public Instance Methods

get(path, **query) click to toggle source

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"
# File lib/owmo/weather.rb, line 90
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
  GET(uri)
end

Private Instance Methods

GET(uri) click to toggle source

Sends the GET request to OpenWeatherMap.org

# File lib/owmo/weather.rb, line 135
def GET(uri)
  response = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(Net::HTTP::Get.new(uri))
  end

  # Check the response
  raise WeatherResponseError.new(response) if response.has_error?

  response.weather
end
alias_geocodes(**query) click to toggle source

Aliases some geocode parameters to the correct ones, for example :city_name is easier to read than :q

# File lib/owmo/weather.rb, line 118
def alias_geocodes(**query)
  query_geocode_keys(query).each do |key|
    query[Geocode_Aliases[key]] = query.delete(key)
  end
  query
end
format_uri(url, path, query) click to toggle source

Formats the url with the given url, path, and query

# File lib/owmo/weather.rb, line 128
def format_uri(url, path, query)
  URI "#{url}/#{path}?#{URI.encode_www_form(query).gsub('%2C', ',')}"
end
query_geocode_keys(**query) click to toggle source

Retruns the geocode keys from specified query.

# File lib/owmo/weather.rb, line 110
def query_geocode_keys(**query)
  query.keys & Geocode_Aliases.keys
end