Class: Weatheruby

Inherits:
Object
  • Object
show all
Includes:
Weather::Actions, Weather::Planner
Defined in:
lib/weatheruby.rb

Overview

TODO:

Return proper objects instead of ugly hashes throughout the library

Defined Under Namespace

Classes: WeatherError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Weather::Planner

#chance_of_cloudy, #chance_of_fog, #chance_of_freezing, #chance_of_groundsnow, #chance_of_hail, #chance_of_heat, #chance_of_high_wind, #chance_of_humid, #chance_of_not_freezing, #chance_of_partlycloudy, #chance_of_precipitation, #chance_of_rain, #chance_of_snow, #chance_of_sultry, #chance_of_sunny, #chance_of_thunderstorms, #chance_of_tornado, #chance_of_warmth, #get_chance_of, #get_dewpoints, #get_planner_response, #get_precipitation, #get_temperatures

Methods included from Weather::Actions

#alerts, #complex_forecast, #complex_forecast_10day, #conditions, #hurricane_data, #moon_phase, #parse_complex_forecast, #parse_simple_forecast, #record_high, #record_low, #simple_forecast, #simple_forecast_10day, #sun_info

Constructor Details

#initialize(api_key, language = 'EN', use_pws = true, use_bestfct = true) ⇒ Weatheruby

Creates a new instance of Weatheruby.

Parameters:

  • api_key (String)

    Your personal API key obtained on sign up for Weather Underground.

  • language (String) (defaults to: 'EN')

    The language code you would like to use.

  • use_pws (Boolean) (defaults to: true)

    Whether to use the Personal Weather Station feature.

  • use_bestfct (Boolean) (defaults to: true)

    Whether to use BestForecast.



33
34
35
36
37
38
39
40
# File 'lib/weatheruby.rb', line 33

def initialize(api_key, language = 'EN', use_pws = true, use_bestfct = true)
  @api_key = api_key
  @language_key = language.upcase
  @use_pws = use_pws ? 1 : 0
  @use_bestfct = use_bestfct ? 1 : 0

  @client = HTTPClient.new
end

Instance Attribute Details

#language_keyObject

Returns the value of attribute language_key



26
27
28
# File 'lib/weatheruby.rb', line 26

def language_key
  @language_key
end

Instance Method Details

#get(feature, location) ⇒ Hash (private)

Performs a generic HTTP GET request. This method should generally not be used by a standard user, unless there is not a method for a particular action/feature.

Parameters:

  • feature (String)

    The “feature” parameter defined by Wunderground.

  • location (String)

    The location of the query.

Returns:

  • (Hash)

    Parsed JSON response

Raises:

  • (WeatherError)

    If anything goes wrong with the API, or it returns too many results for us to handle.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/weatheruby.rb', line 50

def get(feature, location)
  url = "http://api.wunderground.com/api/#{@api_key}/#{feature}/lang:#{@language_key}/pws:#{@use_pws}/bestfct:#{@use_bestfct}/q/#{location}.json"
  json = JSON.parse(@client.get(URI.parse(URI.encode(url))).body)
  if json['response'].key?('error')
    error = json['response']['error']
    fail(WeatherError.new(json, error['type'], error['description'].capitalize))
  end
  if json['response'].key?('results')
    fail(WeatherError.new(json, 'toomanyresults', 'Too many results were returned. Try narrowing your search.'))
  end

  json
end