yahoo-weather 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,22 +5,33 @@
5
5
  yahoo-weather provides an object-oriented interface to the Yahoo! Weather XML
6
6
  RSS feed detailed at http://developer.yahoo.com/weather.
7
7
 
8
- People care a lot about the weather, which sometimes seems ironic given that
9
- they can just glance out the window, but we can all understand the fascination
10
- with details and forecasting.
8
+ People care a lot about the weather. This may seem ironic given they
9
+ can just glance out the window. However, we can all understand a
10
+ fascination with details and forecasting.
11
11
 
12
- Log the weather information to your database! Graph it to your heart's
13
- content! Write a widget that emails the weather to your cell phone every five
14
- minutes with a link to your friend's PayPal account to deposit money if the
15
- weather's sunny and you both bet that it would be rainy. And the fun doesn't
16
- have to stop there.
12
+ Log the weather information to your database! Graph it to your
13
+ heart's content! Write a widget that emails the weather to your cell
14
+ phone every five minutes with a link to your friend's PayPal account
15
+ to deposit money if the weather's sunny and you both bet that it would
16
+ be rainy. And the fun doesn't have to stop there.
17
17
 
18
- The RubyForge project page is at http://rubyforge.org/projects/yahoo-weather.
18
+ The RubyForge project page for this library is at
19
+ http://rubyforge.org/projects/yahoo-weather.
19
20
 
20
21
  Source code is at http://github.com/shaper/yahoo-weather.
21
22
 
22
- == Installation
23
+ NOTE: This library was updated as of December 2009 to use a new
24
+ WOEID-based lookup interface. Yahoo has deprecated the older
25
+ non-WOEID-based lookup API. The archived page with the deprecated API
26
+ details is at:
27
+
28
+ http://developer.yahoo.com/weather/archive.html
23
29
 
30
+ The older interface is still operational but users of this library
31
+ should shift from using YahooWeather::Client::lookup_location to
32
+ YahooWeather::Client::lookup_by_woeid.
33
+
34
+ == Installation
24
35
 
25
36
  Install +rubygems+ if you don't already have it. See http://rubyforge.org/projects/rubygems/.
26
37
 
@@ -36,7 +47,10 @@ A simple example program:
36
47
  require 'yahoo-weather'
37
48
 
38
49
  @client = YahooWeather::Client.new
39
- response = @client.lookup_location('98103')
50
+ # look up WOEID via http://weather.yahoo.com; enter location by city
51
+ # name or zip and WOEID is at end of resulting page url. herein we use
52
+ # the WOEID for Santa Monica, CA.
53
+ response = @client.lookup_by_woeid(2488892)
40
54
 
41
55
  # straight text output
42
56
  print <<edoc
@@ -48,7 +62,7 @@ A simple example program:
48
62
  # sample html output
49
63
  print <<edoc
50
64
  <div>
51
- <img src="#{response.image_url}"><br/>
65
+ <img src="#{response.image.url}"><br/>
52
66
  #{response.condition.temp} degrees #{response.units.temperature}<br/>
53
67
  #{response.condition.text}<br>
54
68
  Forecast:<br/>
@@ -60,17 +74,17 @@ A simple example program:
60
74
 
61
75
  Produces output as:
62
76
 
63
- Conditions for Seattle, WA at 2:53 pm PST
64
- 55 degrees F
65
- Light Rain
77
+ Conditions for Santa Monica, CA at 10:51 am PST
78
+ 56 degrees
79
+ Partly Cloudy
66
80
  <div>
67
- <img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/11.gif"><br/>
68
- 55 degrees F<br/>
69
- Light Rain<br>
81
+ <img src="http://l.yimg.com/a/i/us/nws/th/main_142b.gif"><br/>
82
+ 56 degrees F<br/>
83
+ Partly Cloudy<br>
70
84
  Forecast:<br/>
71
- Sun - Rain/Wind. High: 56 Low: 54<br/>
72
- Mon - Heavy Rain/Wind. High: 60 Low: 54<br/>
73
- More information <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Seattle__WA/*http://xml.weather.yahoo.com/forecast/98103_f.html">here</a>.
85
+ Sat - Mostly Cloudy. High: 59 Low: 46<br/>
86
+ Sun - Partly Cloudy. High: 64 Low: 48<br/>
87
+ More information <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Santa_Monica__CA/*http://weather.yahoo.com/forecast/USCA1024_f.html">here</a>.
74
88
  </div>
75
89
 
76
90
  There is a variety of detailed weather information in other attributes of the
data/Rakefile CHANGED
@@ -8,7 +8,7 @@ Hoe.spec('yahoo-weather') do |p|
8
8
  self.rubyforge_name = 'yahoo-weather'
9
9
  self.author = 'Walter Korman'
10
10
  self.email = 'shaper@fatgoose.com'
11
- self.extra_deps << [ 'xml-simple', '>= 1.0.9' ]
11
+ self.extra_deps << [ 'nokogiri', '>= 1.4.1' ]
12
12
  self.summary = 'A Ruby object-oriented interface to the Yahoo! Weather service.'
13
13
  self.description = <<EDOC
14
14
  The yahoo-weather rubygem provides a Ruby object-oriented interface to the
@@ -17,7 +17,6 @@ Yahoo! Weather service described in detail at:
17
17
  http://developer.yahoo.com/weather
18
18
  EDOC
19
19
  self.url = 'http://rubyforge.org/projects/yahoo-weather'
20
- self.changes = p.paragraphs_of('CHANGELOG.rdoc', 0..1).join("\n\n")
21
20
  self.remote_rdoc_dir = '' # Release to root
22
21
  self.readme_file = 'README.rdoc'
23
22
  self.history_file = 'CHANGELOG.rdoc'
@@ -4,19 +4,22 @@ require 'rubygems'
4
4
  require 'yahoo-weather'
5
5
 
6
6
  @client = YahooWeather::Client.new
7
- response = @client.lookup_location('98103')
7
+ # look up WOEID via http://weather.yahoo.com; enter location by city
8
+ # name or zip and WOEID is at end of resulting page url. herein we use
9
+ # the WOEID for Santa Monica, CA.
10
+ response = @client.lookup_by_woeid(2488892)
8
11
 
9
12
  # straight text output
10
- print <<edoc
13
+ print <<EDOC
11
14
  #{response.title}
12
- #{response.condition.temp} degrees #{response.units.temperature}
15
+ #{response.condition.temp} degrees
13
16
  #{response.condition.text}
14
- edoc
17
+ EDOC
15
18
 
16
- # html output
17
- print <<edoc
19
+ # sample html output
20
+ print <<EDOC
18
21
  <div>
19
- <img src="#{response.image_url}"><br/>
22
+ <img src="#{response.image.url}"><br/>
20
23
  #{response.condition.temp} degrees #{response.units.temperature}<br/>
21
24
  #{response.condition.text}<br>
22
25
  Forecast:<br/>
@@ -24,4 +27,4 @@ print <<edoc
24
27
  #{response.forecasts[1].day} - #{response.forecasts[1].text}. High: #{response.forecasts[1].high} Low: #{response.forecasts[1].low}<br/>
25
28
  More information <a href="#{response.page_url}">here</a>.
26
29
  </div>
27
- edoc
30
+ EDOC
@@ -1,5 +1,5 @@
1
1
  # yahoo-weather -- provides OO access to the Yahoo! Weather RSS XML feed
2
- # Copyright (C) 2006 Walter Korman <shaper@wgks.org>
2
+ # Copyright (C) 2006 - 2009 Walter Korman <shaper@fatgoose.com>
3
3
  #
4
4
  # This library is free software; you can redistribute it and/or
5
5
  # modify it under the terms of the GNU Lesser General Public
@@ -16,306 +16,25 @@
16
16
  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
 
18
18
  require 'net/http'
19
+ require 'cgi'
19
20
  require 'time'
20
- require 'xmlsimple'
21
+ require 'nokogiri'
21
22
 
22
23
  class YahooWeather
24
+ VERSION = '1.2.0'
23
25
 
24
- VERSION = '1.1.0'
25
-
26
- # Describes astronomy information for a particular location.
27
- class Astronomy
28
- # a Time object detailing the sunrise time for a location.
29
- attr_reader :sunrise
30
-
31
- # a Time object detailing the sunset time for a location.
32
- attr_reader :sunset
33
-
34
- def initialize (payload)
35
- @sunrise = YahooWeather._parse_time(payload['sunrise'])
36
- @sunset = YahooWeather._parse_time(payload['sunset'])
37
- end
38
- end
39
-
40
- # Describes a geographical location.
41
- class Location
42
- # the name of the city.
43
- attr_reader :city
44
-
45
- # the name of the country.
46
- attr_reader :country
47
-
48
- # the name of the region, such as a state.
49
- attr_reader :region
50
-
51
- def initialize (payload)
52
- @city = payload['city']
53
- @country = payload['country']
54
- @region = payload['region']
55
- end
56
- end
57
-
58
- # Describes the units of measure with which weather information is provided.
59
- class Units
60
- # the units in which temperature is measured, e.g. +F+ for +Fahrenheit+ or +C+ for +Celsius+.
61
- attr_reader :temperature
62
-
63
- # the units in which distance is measured, e.g. +mi+ for +miles+.
64
- attr_reader :distance
65
-
66
- # the units in which pressure is measured, e.g. +in+ for +inches+.
67
- attr_reader :pressure
68
-
69
- # the units in which speed is measured, e.g. +mph+ for <tt>miles per hour</tt>.
70
- attr_reader :speed
71
-
72
- def initialize (payload)
73
- @temperature = payload['temperature']
74
- @distance = payload['distance']
75
- @pressure = payload['pressure']
76
- @speed = payload['speed']
77
- end
78
- end
79
-
80
- # Describes the specific wind conditions at a location.
81
- class Wind
82
- # the temperature factoring in wind chill.
83
- attr_reader :chill
84
-
85
- # the direction of the wind in degrees.
86
- attr_reader :direction
87
-
88
- # the speed of the wind.
89
- attr_reader :speed
90
-
91
- def initialize (payload)
92
- @chill = payload['chill'].to_i
93
- @direction = payload['direction'].to_i
94
- @speed = payload['speed'].to_i
95
- end
96
- end
97
-
98
- # Describes the specific atmospheric conditions at a location.
99
- class Atmosphere
100
- # Constants representing the state of the barometric pressure.
101
- #
102
- class Barometer
103
- STEADY = 'steady'
104
- RISING = 'rising'
105
- FALLING = 'falling'
106
-
107
- # lists all possible barometer constants
108
- ALL = [ STEADY, RISING, FALLING ]
109
- end
110
-
111
- # the humidity of the surroundings.
112
- attr_reader :humidity
113
-
114
- # the visibility level of the surroundings
115
- attr_reader :visibility
116
-
117
- # the pressure level of the surroundings.
118
- attr_reader :pressure
119
-
120
- # the state of the barometer, defined as one of the
121
- # YahooWeather::Atmosphere::Barometer constants.
122
- attr_reader :barometer
123
-
124
- def initialize (payload)
125
- @humidity = payload['humidity'].to_i
126
- @visibility = payload['visibility'].to_i
127
- @pressure = payload['pressure'].to_f
128
-
129
- # map barometric pressure direction to appropriate constant
130
- @barometer = nil
131
- case payload['rising'].to_i
132
- when 0: @barometer = Barometer::STEADY
133
- when 1: @barometer = Barometer::RISING
134
- when 2: @barometer = Barometer::FALLING
135
- end
136
- end
137
- end
138
-
139
- class Condition
140
- # the Yahoo! Weather condition code, as detailed at http://developer.yahoo.com/weather.
141
- attr_reader :code
142
-
143
- # the date and time associated with these conditions.
144
- attr_reader :date
145
-
146
- # the temperature of the location.
147
- attr_reader :temp
148
-
149
- # the brief prose text description of the weather conditions of the location.
150
- attr_reader :text
151
-
152
- def initialize (payload)
153
- @code = payload['code'].to_i
154
- @date = YahooWeather._parse_time(payload['date'])
155
- @temp = payload['temp'].to_i
156
- @text = payload['text']
157
- end
158
- end
159
-
160
- # The forecasted weather conditions for a specific location.
161
- class Forecast
162
- # the brief name of the day associated with the forecast.
163
- attr_reader :day
164
-
165
- # the date associated with the forecast.
166
- attr_reader :date
167
-
168
- # the low temperature forecasted.
169
- attr_reader :low
170
-
171
- # the high temperature forecasted.
172
- attr_reader :high
173
-
174
- # the brief prose text description of the forecasted weather conditions.
175
- attr_reader :text
176
-
177
- # the Yahoo! Weather code associated with the forecast, per http://developer.yahoo.com/weather.
178
- attr_reader :code
179
-
180
- def initialize (payload)
181
- @day = payload['day']
182
- @date = YahooWeather._parse_time(payload['date'])
183
- @low = payload['low'].to_i
184
- @high = payload['high'].to_i
185
- @text = payload['text']
186
- @code = payload['code'].to_i
187
- end
188
- end
189
-
190
- # Describes the weather conditions for a particular requested location.
191
- class Response
192
- # a YahooWeather::Astronomy object detailing the sunrise and sunset
193
- # information for the requested location.
194
- attr_reader :astronomy
195
-
196
- # a YahooWeather::Location object detailing the precise geographical names
197
- # to which the requested location was mapped.
198
- attr_reader :location
199
-
200
- # a YahooWeather::Units object detailing the units corresponding to the
201
- # information detailed in the response.
202
- attr_reader :units
203
-
204
- # a YahooWeather::Wind object detailing the wind information at the
205
- # requested location.
206
- attr_reader :wind
207
-
208
- # a YahooWeather::Atmosphere object detailing the atmosphere information
209
- # of the requested location.
210
- attr_reader :atmosphere
211
-
212
- # a YahooWeather::Condition object detailing the current conditions of the
213
- # requested location.
214
- attr_reader :condition
215
-
216
- # a list of YahooWeather::Forecast objects detailing the high-level
217
- # forecasted weather conditions for upcoming days.
218
- attr_reader :forecasts
219
-
220
- # the raw HTML generated by the Yahoo! Weather service summarizing current
221
- # weather conditions for the requested location.
222
- attr_reader :description
223
-
224
- # a link to the Yahoo! Weather image icon representing the current weather
225
- # conditions visually.
226
- attr_reader :image_url
227
-
228
- # the latitude of the location for which weather is detailed.
229
- attr_reader :latitude
230
-
231
- # the longitude of the location for which weather is detailed.
232
- attr_reader :longitude
233
-
234
- # a link to the Yahoo! Weather page with full detailed information on the
235
- # requested location's current weather conditions.
236
- attr_reader :page_url
237
-
238
- # the location string initially requested of the service.
239
- attr_reader :request_location
240
-
241
- # the url with which the Yahoo! Weather service was accessed to build the response.
242
- attr_reader :request_url
243
-
244
- # the prose descriptive title of the weather information.
245
- attr_reader :title
246
-
247
- # regular expression used to pull the weather image icon from full
248
- # description text.
249
- @@REGEXP_IMAGE = Regexp.new(/img src="([^"]+)"/)
250
-
251
- def initialize (request_location, request_url, payload)
252
- @request_location = request_location
253
- @request_url = request_url
254
-
255
- root = payload['channel'].first
256
- @astronomy = YahooWeather::Astronomy.new root['astronomy'].first
257
- @location = YahooWeather::Location.new root['location'].first
258
- @units = YahooWeather::Units.new root['units'].first
259
- @wind = YahooWeather::Wind.new root['wind'].first
260
- @atmosphere = YahooWeather::Atmosphere.new root['atmosphere'].first
261
-
262
- item = root['item'].first
263
- @condition = YahooWeather::Condition.new item['condition'].first
264
- @forecasts = []
265
- item['forecast'].each { |forecast| @forecasts << YahooWeather::Forecast.new(forecast) }
266
- @latitude = item['lat'].first.to_f
267
- @longitude = item['long'].first.to_f
268
- @page_url = item['link'].first
269
- @title = item['title'].first
270
- @description = item['description'].first
271
-
272
- match_data = @@REGEXP_IMAGE.match(description)
273
- @image_url = (match_data) ? match_data[1] : nil
274
- end
275
- end
276
-
277
- # The main client object through which the Yahoo! Weather service may be accessed.
278
- class Client
279
- # the url with which we obtain weather information from yahoo
280
- @@API_URL = "http://xml.weather.yahoo.com/forecastrss"
281
-
282
- def initialize (api_url = @@API_URL)
283
- @api_url = api_url
284
- end
285
-
286
- # Returns a YahooWeather::Response object detailing the current weather
287
- # information for the specified location.
288
- #
289
- # +location+ can be either a US zip code or a location code. Location
290
- # codes can be looked up at http://weather.yahoo.com, where it will appear
291
- # in the URL that results from searching on the city or zip code. For
292
- # instance, searching on 'Seattle, WA' results in a URL ending in
293
- # 'USWA0395.html', so the location code for Seattle is 'USWA0395'.
294
- #
295
- # +units+ allows specifying whether to retrieve information in
296
- # +Fahrenheit+ as +f+, or +Celsius+ as +c+, and defaults to +f+.
297
- def lookup_location (location, units = 'f')
298
- # query the service to grab the xml data
299
- url = _request_url(location, units)
300
- begin
301
- response = Net::HTTP.get_response(URI.parse(url)).body.to_s
302
- rescue
303
- raise "failed to get weather via '#{url}': " + $!
304
- end
305
-
306
- # create the response object
307
- response = XmlSimple.xml_in(response)
308
- YahooWeather::Response.new(location, url, response)
309
- end
310
-
311
- private
312
- def _request_url (location, units)
313
- @api_url + '?p=' + URI.encode(location) + '&u=' + URI.encode(units)
314
- end
315
- end
316
-
317
- private
318
26
  def self._parse_time (text)
319
27
  (text) ? Time.parse(text) : nil
320
28
  end
321
29
  end
30
+
31
+ require 'yahoo-weather/astronomy'
32
+ require 'yahoo-weather/atmosphere'
33
+ require 'yahoo-weather/client'
34
+ require 'yahoo-weather/condition'
35
+ require 'yahoo-weather/forecast'
36
+ require 'yahoo-weather/image'
37
+ require 'yahoo-weather/location'
38
+ require 'yahoo-weather/response'
39
+ require 'yahoo-weather/units'
40
+ require 'yahoo-weather/wind'