Class: Weatherman::Response
- Inherits:
-
Object
- Object
- Weatherman::Response
- 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
-
#document_root ⇒ Object
Returns the value of attribute document_root.
Instance Method Summary collapse
-
#astronomy ⇒ Object
Astronomy:.
-
#atmosphere ⇒ Object
Atmosphere :.
-
#condition ⇒ Object
Returns a hash containing the actual weather condition details:.
-
#description ⇒ Object
(also: #summary)
A short HTML snippet (raw text) with a simple weather description.
-
#description_image ⇒ Object
Description image.
-
#forecasts ⇒ Object
Forecasts for the next 2 days.
-
#image ⇒ Object
A hash like object providing image info:.
-
#initialize(raw, language = nil) ⇒ Response
constructor
A new instance of Response.
-
#latitude ⇒ Object
Latitude:.
-
#location ⇒ Object
Location:.
-
#longitude ⇒ Object
Longitude;.
-
#parsed_description ⇒ Object
Description parsed by Nokogiri.
-
#units ⇒ Object
Units:.
-
#wind ⇒ Object
Wind’s details:.
Constructor Details
#initialize(raw, language = nil) ⇒ Response
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
#document_root ⇒ Object
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
#astronomy ⇒ Object
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 |
#atmosphere ⇒ Object
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 |
#condition ⇒ Object
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 |
#description ⇒ Object 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 |
#description_image ⇒ Object
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 |
#forecasts ⇒ Object
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 |
#image ⇒ Object
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 |
#latitude ⇒ Object
Latitude:
response.latitude => -49.90
114 115 116 |
# File 'lib/yahoo_weatherman/response.rb', line 114 def latitude geo_attribute('lat') end |
#location ⇒ Object
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 |
#longitude ⇒ Object
Longitude;
response.longitude => -45.32
123 124 125 |
# File 'lib/yahoo_weatherman/response.rb', line 123 def longitude geo_attribute('long') end |
#parsed_description ⇒ Object
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 |
#units ⇒ Object
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 |
#wind ⇒ Object
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 |