tomk32-yahoo-weather 1.2.1 → 1.2.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ # Describes astronomy information for a particular location.
2
+ class YahooWeather::Astronomy
3
+ # a Time object detailing the sunrise time for a location.
4
+ attr_reader :sunrise
5
+
6
+ # a Time object detailing the sunset time for a location.
7
+ attr_reader :sunset
8
+
9
+ def initialize (payload)
10
+ @sunrise = YahooWeather._parse_time(payload['sunrise'])
11
+ @sunset = YahooWeather._parse_time(payload['sunset'])
12
+ end
13
+ end
@@ -0,0 +1,40 @@
1
+ # Describes the specific atmospheric conditions at a location.
2
+ class YahooWeather::Atmosphere
3
+ # Constants representing the state of the barometric pressure.
4
+ #
5
+ class Barometer
6
+ STEADY = 'steady'
7
+ RISING = 'rising'
8
+ FALLING = 'falling'
9
+
10
+ # lists all possible barometer constants
11
+ ALL = [ STEADY, RISING, FALLING ]
12
+ end
13
+
14
+ # the humidity of the surroundings.
15
+ attr_reader :humidity
16
+
17
+ # the visibility level of the surroundings
18
+ attr_reader :visibility
19
+
20
+ # the pressure level of the surroundings.
21
+ attr_reader :pressure
22
+
23
+ # the state of the barometer, defined as one of the
24
+ # YahooWeather::Atmosphere::Barometer constants.
25
+ attr_reader :barometer
26
+
27
+ def initialize (payload)
28
+ @humidity = payload['humidity'].to_i
29
+ @visibility = payload['visibility'].to_i
30
+ @pressure = payload['pressure'].to_f
31
+
32
+ # map barometric pressure direction to appropriate constant
33
+ @barometer = nil
34
+ case payload['rising'].to_i
35
+ when 0 then @barometer = Barometer::STEADY
36
+ when 1 then @barometer = Barometer::RISING
37
+ when 2 then @barometer = Barometer::FALLING
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,70 @@
1
+ # The main client object through which the Yahoo! Weather service may be accessed.
2
+ class YahooWeather::Client
3
+ # the url with which we obtain weather information from yahoo
4
+ @@API_URL = "http://weather.yahooapis.com/forecastrss"
5
+
6
+ def initialize (api_url = @@API_URL)
7
+ @api_url = api_url
8
+ end
9
+
10
+ # Returns a YahooWeather::Response object detailing the current weather
11
+ # information for the specified location.
12
+ #
13
+ # The lookup requires the unique WOEID for the location whose
14
+ # weather is sought.. To find your WOEID, browse or search for your
15
+ # city from the Weather (http://weather.yahoo.com/) home page. The
16
+ # WOEID is in the URL for the forecast page for that city. You can
17
+ # also get the WOEID by entering your zip code on the home page. For
18
+ # example, if you search for Los Angeles on the Weather home page,
19
+ # the forecast page for that city is
20
+ # http://weather.yahoo.com/united-states/california/los-angeles-2442047/. The
21
+ # WOEID is 2442047.
22
+ #
23
+ # +units+ allows specifying whether to retrieve information in
24
+ # +Fahrenheit+ as YahooWeather::Units::FAHRENHEIT, or +Celsius+ as
25
+ # YahooWeather::Units::CELSIUS, and defaults to fahrenheit.
26
+ #
27
+ def lookup_by_woeid (woeid, units = 'f')
28
+ url = @api_url + '?w=' + CGI.escape(woeid.to_s) + '&u=' + CGI.escape(units)
29
+ _lookup(woeid, url)
30
+ end
31
+
32
+ # Returns a YahooWeather::Response object detailing the current weather
33
+ # information for the specified location.
34
+ #
35
+ # NOTE: This method is deprecated as Yahoo has deprecated this
36
+ # non-WOEID-based lookup function. Please use the new
37
+ # +lookup_by_woeid+ method instead.
38
+ #
39
+ # +location+ can be either a US zip code or a location code. Location
40
+ # codes can be looked up at http://weather.yahoo.com, where it will appear
41
+ # in the URL that results from searching on the city or zip code. For
42
+ # instance, searching on 'Seattle, WA' results in a URL ending in
43
+ # 'USWA0395.html', so the location code for Seattle is 'USWA0395'.
44
+ #
45
+ # +units+ allows specifying whether to retrieve information in
46
+ # +Fahrenheit+ as YahooWeather::Units::FAHRENHEIT, or +Celsius+ as
47
+ # YahooWeather::Units::CELSIUS, and defaults to fahrenheit.
48
+ #
49
+ def lookup_location (location, units = 'f')
50
+ url = @api_url + '?p=' + CGI.escape(location) + '&u=' + CGI.escape(units)
51
+ _lookup(location, url)
52
+ end
53
+
54
+ private
55
+
56
+ def _lookup (src, url)
57
+ begin
58
+ response = Net::HTTP.get_response(URI.parse(url)).body.to_s
59
+
60
+ rescue => e
61
+ raise RuntimeError.new("failed to get weather [src=#{src}, " +
62
+ "url=#{url}, e=#{e}].")
63
+ end
64
+
65
+ # create the response object
66
+ doc = Nokogiri::XML.parse(response)
67
+ YahooWeather::Response.new(src, url, doc)
68
+ end
69
+
70
+ end
@@ -0,0 +1,20 @@
1
+ class YahooWeather::Condition
2
+ # the Yahoo! Weather condition code, as detailed at http://developer.yahoo.com/weather.
3
+ attr_reader :code
4
+
5
+ # the date and time associated with these conditions.
6
+ attr_reader :date
7
+
8
+ # the temperature of the location.
9
+ attr_reader :temp
10
+
11
+ # the brief prose text description of the weather conditions of the location.
12
+ attr_reader :text
13
+
14
+ def initialize (payload)
15
+ @code = payload['code'].to_i
16
+ @date = YahooWeather._parse_time(payload['date'])
17
+ @temp = payload['temp'].to_i
18
+ @text = payload['text']
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ # The forecasted weather conditions for a specific location.
2
+ class YahooWeather::Forecast
3
+ # the brief name of the day associated with the forecast.
4
+ attr_reader :day
5
+
6
+ # the date associated with the forecast.
7
+ attr_reader :date
8
+
9
+ # the low temperature forecasted.
10
+ attr_reader :low
11
+
12
+ # the high temperature forecasted.
13
+ attr_reader :high
14
+
15
+ # the brief prose text description of the forecasted weather conditions.
16
+ attr_reader :text
17
+
18
+ # the Yahoo! Weather code associated with the forecast, per http://developer.yahoo.com/weather.
19
+ attr_reader :code
20
+
21
+ def initialize (payload)
22
+ @day = payload['day']
23
+ @date = YahooWeather._parse_time(payload['date'])
24
+ @low = payload['low'].to_i
25
+ @high = payload['high'].to_i
26
+ @text = payload['text']
27
+ @code = payload['code'].to_i
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ class YahooWeather::Image
2
+ # the height of the image in pixels.
3
+ attr_reader :height
4
+
5
+ # the url intended to be used as a link wrapping the image, for
6
+ # instance, to send the user to the main Yahoo Weather home page.
7
+ attr_reader :link
8
+
9
+ # the title of hte image.
10
+ attr_reader :title
11
+
12
+ # the full url to the image.
13
+ attr_reader :url
14
+
15
+ # the width of the image in pixels.
16
+ attr_reader :width
17
+
18
+ def initialize (payload)
19
+ @title = payload.xpath('title').first.content
20
+ @link = payload.xpath('link').first.content
21
+ @url = payload.xpath('url').first.content
22
+ @height = payload.xpath('height').first.content.to_i
23
+ @width = payload.xpath('width').first.content.to_i
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ # Describes a geographical location.
2
+ class YahooWeather::Location
3
+ # the name of the city.
4
+ attr_reader :city
5
+
6
+ # the name of the country.
7
+ attr_reader :country
8
+
9
+ # the name of the region, such as a state.
10
+ attr_reader :region
11
+
12
+ def initialize (payload)
13
+ @city = payload['city']
14
+ @country = payload['country']
15
+ @region = payload['region']
16
+ end
17
+ end
@@ -0,0 +1,85 @@
1
+ # Describes the weather conditions for a particular requested location.
2
+ class YahooWeather::Response
3
+ # a YahooWeather::Astronomy object detailing the sunrise and sunset
4
+ # information for the requested location.
5
+ attr_reader :astronomy
6
+
7
+ # a YahooWeather::Location object detailing the precise geographical names
8
+ # to which the requested location was mapped.
9
+ attr_reader :location
10
+
11
+ # a YahooWeather::Units object detailing the units corresponding to the
12
+ # information detailed in the response.
13
+ attr_reader :units
14
+
15
+ # a YahooWeather::Wind object detailing the wind information at the
16
+ # requested location.
17
+ attr_reader :wind
18
+
19
+ # a YahooWeather::Atmosphere object detailing the atmosphere information
20
+ # of the requested location.
21
+ attr_reader :atmosphere
22
+
23
+ # a YahooWeather::Condition object detailing the current conditions of the
24
+ # requested location.
25
+ attr_reader :condition
26
+
27
+ # a list of YahooWeather::Forecast objects detailing the high-level
28
+ # forecasted weather conditions for upcoming days.
29
+ attr_reader :forecasts
30
+
31
+ # the raw HTML generated by the Yahoo! Weather service summarizing current
32
+ # weather conditions for the requested location.
33
+ attr_reader :description
34
+
35
+ # a YahooWeather::Image record describing an image icon
36
+ # representing the current weather.
37
+ attr_reader :image
38
+
39
+ # the latitude of the location for which weather is detailed.
40
+ attr_reader :latitude
41
+
42
+ # the longitude of the location for which weather is detailed.
43
+ attr_reader :longitude
44
+
45
+ # a link to the Yahoo! Weather page with full detailed information on the
46
+ # requested location's current weather conditions.
47
+ attr_reader :page_url
48
+
49
+ # the location string initially requested of the service.
50
+ attr_reader :request_location
51
+
52
+ # the url with which the Yahoo! Weather service was accessed to build the response.
53
+ attr_reader :request_url
54
+
55
+ # the prose descriptive title of the weather information.
56
+ attr_reader :title
57
+
58
+ def initialize (request_location, request_url, doc)
59
+ # save off the request params
60
+ @request_location = request_location
61
+ @request_url = request_url
62
+
63
+ # parse the nokogiri xml document to gather response data
64
+ root = doc.xpath('/rss/channel').first
65
+
66
+ @astronomy = YahooWeather::Astronomy.new(root.xpath('yweather:astronomy').first)
67
+ @location = YahooWeather::Location.new(root.xpath('yweather:location').first)
68
+ @units = YahooWeather::Units.new(root.xpath('yweather:units').first)
69
+ @wind = YahooWeather::Wind.new(root.xpath('yweather:wind').first)
70
+ @atmosphere = YahooWeather::Atmosphere.new(root.xpath('yweather:atmosphere').first)
71
+ @image = YahooWeather::Image.new(root.xpath('image').first)
72
+
73
+ item = root.xpath('item').first
74
+ @condition = YahooWeather::Condition.
75
+ new(item.xpath('yweather:condition').first)
76
+ @forecasts = []
77
+ item.xpath('yweather:forecast').each { |forecast|
78
+ @forecasts << YahooWeather::Forecast.new(forecast) }
79
+ @latitude = item.xpath('geo:lat').first.content.to_f
80
+ @longitude = item.xpath('geo:long').first.content.to_f
81
+ @page_url = item.xpath('link').first.content
82
+ @title = item.xpath('title').first.content
83
+ @description = item.xpath('description').first.content
84
+ end
85
+ end
@@ -0,0 +1,24 @@
1
+ # Describes the units of measure with which weather information is provided.
2
+ class YahooWeather::Units
3
+ FAHRENHEIT = 'f'
4
+ CELSIUS = 'c'
5
+
6
+ # the units in which temperature is measured, e.g. +F+ for +Fahrenheit+ or +C+ for +Celsius+.
7
+ attr_reader :temperature
8
+
9
+ # the units in which distance is measured, e.g. +mi+ for +miles+.
10
+ attr_reader :distance
11
+
12
+ # the units in which pressure is measured, e.g. +in+ for +inches+.
13
+ attr_reader :pressure
14
+
15
+ # the units in which speed is measured, e.g. +mph+ for <tt>miles per hour</tt>.
16
+ attr_reader :speed
17
+
18
+ def initialize (payload)
19
+ @temperature = payload['temperature']
20
+ @distance = payload['distance']
21
+ @pressure = payload['pressure']
22
+ @speed = payload['speed']
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ # Describes the specific wind conditions at a location.
2
+ class YahooWeather::Wind
3
+ # the temperature factoring in wind chill.
4
+ attr_reader :chill
5
+
6
+ # the direction of the wind in degrees.
7
+ attr_reader :direction
8
+
9
+ # the speed of the wind.
10
+ attr_reader :speed
11
+
12
+ def initialize (payload)
13
+ @chill = payload['chill'].to_i
14
+ @direction = payload['direction'].to_i
15
+ @speed = payload['speed'].to_i
16
+ end
17
+ end
metadata CHANGED
@@ -6,7 +6,8 @@ version: !ruby/object:Gem::Version
6
6
  - 1
7
7
  - 2
8
8
  - 1
9
- version: 1.2.1
9
+ - 1
10
+ version: 1.2.1.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Walter Korman
@@ -62,10 +63,20 @@ files:
62
63
  - TODO
63
64
  - examples/example.rb
64
65
  - examples/get-weather.rb
66
+ - lib/yahoo-weather/astronomy.rb
67
+ - lib/yahoo-weather/atmosphere.rb
68
+ - lib/yahoo-weather/client.rb
69
+ - lib/yahoo-weather/condition.rb
70
+ - lib/yahoo-weather/forecast.rb
71
+ - lib/yahoo-weather/image.rb
72
+ - lib/yahoo-weather/location.rb
73
+ - lib/yahoo-weather/response.rb
74
+ - lib/yahoo-weather/units.rb
75
+ - lib/yahoo-weather/wind.rb
65
76
  - lib/yahoo-weather.rb
66
77
  - test/test_api.rb
67
78
  has_rdoc: true
68
- homepage: http://rubyforge.org/projects/yahoo-weather
79
+ homepage: http://github.com/tomk32/yahoo-weather
69
80
  licenses: []
70
81
 
71
82
  post_install_message: