Class: WeatherHacker::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/weather_hacker/client.rb

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

WEATHER_URL =
"http://weather.livedoor.com/forecast/webservice/json/v1"
AREA_TABLE_URL =
"http://weather.livedoor.com/forecast/rss/primary_area.xml"
ZIPCODE_URL =
"http://zip.cgis.biz/xml/zip.php"

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



12
13
14
# File 'lib/weather_hacker/client.rb', line 12

def initialize
  @city_id_by_zipcode = {}
end

Instance Method Details

#get_weather(zipcode, opts = {}) ⇒ Object

get weather data by zipcode



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/weather_hacker/client.rb', line 17

def get_weather(zipcode, opts = {})
  city_id  = city_id_by_zipcode(zipcode) or return
  query    = { :city => city_id }.merge(opts)
  hash     = get WEATHER_URL, :query => query
  forecast = get_forecast(hash, opts) or return

  weather = forecast["telop"]
  max = forecast["temperature"]["max"]
  min = forecast["temperature"]["min"]
  max_celsius = max["celsius"] unless max.nil?
  min_celsius = min["celsius"] unless min.nil?

  {
    "weather"     => weather,
    "temperature" => {
    "max" => max_celsius,
    "min" => min_celsius,
  }}
rescue ParseError
  nil
end