Class: Weatherman::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo_weatherman/response.rb

Overview

Response

This is where we get access to the contents parsed by Nokogiri in a object-oriented way. We also use this class to do the i18n stuff.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Response) initialize(raw, language = nil)

Returns a new instance of Response



13
14
15
16
# File 'lib/yahoo_weatherman/response.rb', line 13

def initialize(raw, language = nil)
  @document_root = Nokogiri::XML(raw).xpath('rss/channel')
  @i18n = Weatherman::I18N.new(language)
end

Instance Attribute Details

- (Object) document_root

Returns the value of attribute document_root



11
12
13
# File 'lib/yahoo_weatherman/response.rb', line 11

def document_root
  @document_root
end

Instance Method Details

- (Object) astronomy

Astronomy:

astronomy = response.astronomy
astronomy['sunrise'] => "6:01 am"
astronomy['sunset'] => "7:20 pm"


92
93
94
# File 'lib/yahoo_weatherman/response.rb', line 92

def astronomy
  attribute('yweather:astronomy')
end

- (Object) atmosphere

Atmosphere :

atmosphere  = response.atmosphere 
atmosphere['humidity'] => "62"
atmosphere['visibility'] => "9.99"
atmosphere['pressure'] => "982.05"
atmosphere['rising'] => "0"


105
106
107
108
# File 'lib/yahoo_weatherman/response.rb', line 105

def atmosphere
  atm = attribute('yweather:atmosphere')
  do_convertions(atm, [:humidity, :to_f], [:visibility, :to_f], [:pressure, :to_f], [:rising, :to_f])
end

- (Object) condition

Returns a hash containing the actual weather condition details:

condition = response.condition
condition['text'] => "Tornado"
condition['code'] => 0
condition['temp'] => 21
condition['date'] => #<Date: -1/2,0,2299161>


27
28
29
30
# File 'lib/yahoo_weatherman/response.rb', line 27

def condition
  condition = item_attribute('yweather:condition')
  translate! do_convertions(condition, [:code, :to_i], [:temp, :to_i], [:date, :to_date], :text)
end

- (Object) description Also known as: summary

A short HTML snippet (raw text) with a simple weather description.



152
153
154
# File 'lib/yahoo_weatherman/response.rb', line 152

def description
  text_attribute('description')
end

- (Object) description_image

Description image. You might gonna need this if you have to customize the forecast summary.



145
146
147
# File 'lib/yahoo_weatherman/response.rb', line 145

def description_image
  parsed_description.css('img').first # there's only one
end

- (Object) forecasts

Forecasts for the next 2 days.

forecast = response.forecasts.first
forecast['low'] => 20
forecast['high'] => 31
forecast['text'] => "Tornado"
forecast['code'] => 0
forecast['day'] => "Sat"


54
55
56
57
58
59
# File 'lib/yahoo_weatherman/response.rb', line 54

def forecasts
  convertions = [[:date, :to_date], [:low, :to_i], [:high, :to_i], [:code, :to_i], :day, :text]
  item_attribute('yweather:forecast').collect do |forecast|
    translate! do_convertions(forecast, *convertions)
  end
end

- (Object) image

A hash like object providing image info:

image = reponse.image
image['width'] => 142
image['height'] => 18
image['title'] => "Yahoo! Weather"
image['link'] => "http://weather.yahoo.com"


136
137
138
139
# File 'lib/yahoo_weatherman/response.rb', line 136

def image
  image = Weatherman::Image.new(attribute('image'))
  do_convertions(image, [:width, :to_i], [:height, :to_i], :title, :link, :url)
end

- (Object) latitude

Latitude:

response.latitude => -49.90


114
115
116
# File 'lib/yahoo_weatherman/response.rb', line 114

def latitude
  geo_attribute('lat')
end

- (Object) location

Location:

location = response.location
location['country'] => "Brazil"
location['region'] => "MG"
location['city'] => Belo Horizonte


69
70
71
# File 'lib/yahoo_weatherman/response.rb', line 69

def location
  translate! attribute('yweather:location')
end

- (Object) longitude

Longitude;

response.longitude => -45.32


123
124
125
# File 'lib/yahoo_weatherman/response.rb', line 123

def longitude
  geo_attribute('long')
end

- (Object) parsed_description

Description parsed by Nokogiri. This is better then #description if you have to walk through its nodes.



161
162
163
# File 'lib/yahoo_weatherman/response.rb', line 161

def parsed_description
  @parsed_description ||= Nokogiri::HTML(description) 
end

- (Object) units

Units:

units = response.units
units['temperature']  => "C"
units['distance']  => "km"
units['pressure']  => "mb"
units['speed']  => "km/h"


81
82
83
# File 'lib/yahoo_weatherman/response.rb', line 81

def units
  attribute('yweather:units')
end

- (Object) wind

Wind's details:

wind = response.wind
wind['chill'] => 21 
wind['direction'] => 340 
wind['chill'] => 9.66


40
41
42
# File 'lib/yahoo_weatherman/response.rb', line 40

def wind
  do_convertions(attribute('yweather:wind'), [:chill, :to_i], [:direction, :to_i], [:speed, :to_f]) 
end