rubyweatherforecast 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2009 Guilherme Nascimento
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,7 @@
1
+ = Ruby Weather Forecast
2
+
3
+ A library that allows fetch weather forecast data from Weather.com
4
+
5
+ == Install
6
+
7
+ == Examples
data/lib/Current.rb ADDED
@@ -0,0 +1,46 @@
1
+ require File.dirname(File.expand_path(__FILE__)) + '/Wind'
2
+
3
+ require "Time"
4
+
5
+ module Weather
6
+
7
+ class Current
8
+ attr_reader :doc
9
+
10
+ def initialize(doc)
11
+ @doc = doc
12
+ end
13
+
14
+ # Icon to be displayed
15
+ def icon
16
+ doc.at("icon").innerHTML
17
+ end
18
+
19
+ # Outlook message
20
+ def description
21
+ doc.at("t").innerHTML
22
+ end
23
+
24
+ # Forecast date
25
+ def date
26
+ Time.now
27
+ end
28
+
29
+ # Temperature
30
+ def temp
31
+ doc.at("tmp").innerHTML
32
+ end
33
+
34
+ # Last update (current)
35
+ def last_update
36
+ Time.parse(doc.at("lsup").innerHTML)
37
+ end
38
+
39
+ # Wind conditions
40
+ def wind
41
+ Weather::Wind.new(doc.at("wind"))
42
+ end
43
+
44
+ end
45
+
46
+ end
data/lib/Day.rb ADDED
@@ -0,0 +1,24 @@
1
+ require File.dirname(File.expand_path(__FILE__)) + '/FutureEstimate'
2
+
3
+ module Weather
4
+
5
+ class Day < Weather::FutureEstimate
6
+ attr_reader :doc
7
+
8
+ def initialize(doc)
9
+ super(doc)
10
+ end
11
+
12
+ # Select day part in the doc
13
+ def part
14
+ day = nil
15
+ (doc/"part").each do |d|
16
+ if d.attributes["p"] == "d"
17
+ day = d
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
data/lib/Forecast.rb ADDED
@@ -0,0 +1,90 @@
1
+ require File.dirname(File.expand_path(__FILE__)) + '/Current'
2
+ require File.dirname(File.expand_path(__FILE__)) + '/Day'
3
+ require File.dirname(File.expand_path(__FILE__)) + '/Night'
4
+
5
+ require "Time"
6
+
7
+ module Weather
8
+
9
+ class Forecast
10
+ attr_reader :doc
11
+
12
+ def initialize(doc)
13
+ @doc = doc
14
+ end
15
+
16
+ # Current conditions
17
+ def current
18
+ Weather::Current.new(doc.at("weather/cc"))
19
+ end
20
+
21
+ # Location name
22
+ def location_name
23
+ doc.at("weather/loc/dnam").innerHTML
24
+ end
25
+
26
+ # Location ID
27
+ def location_id
28
+ doc.at("weather/loc")["id"]
29
+ end
30
+
31
+ # Unit of temperature (F | C)
32
+ def temp_unit
33
+ doc.at("weather/head/ut").innerHTML
34
+ end
35
+
36
+ # Unit of distance
37
+ def distance_unit
38
+ doc.at("weather/head/ud").innerHTML
39
+ end
40
+
41
+ # Unit of speed
42
+ def speed_unit
43
+ doc.at("weather/head/us").innerHTML
44
+ end
45
+
46
+ # Unit of precipitation
47
+ def precipitation_unit
48
+ doc.at("weather/head/up").innerHTML
49
+ end
50
+
51
+ # Unit of presure
52
+ def presure_unit
53
+ doc.at("weather/head/ur").innerHTML
54
+ end
55
+
56
+ # Last update (future)
57
+ def last_update
58
+ Time.parse(doc.at("weather/dayf/lsup").innerHTML)
59
+ end
60
+
61
+ # Used to know the forecast from tomorrow
62
+ def tomorrow
63
+ day(1)
64
+ end
65
+
66
+ # Defines a day
67
+ def day(id)
68
+ day = nil
69
+ (doc/"weather/dayf/day").each do |d|
70
+ if d.attributes["d"] == id.to_s
71
+ day = d
72
+ end
73
+ end
74
+ Weather::Day.new(day)
75
+ end
76
+
77
+ # Defines a night
78
+ def night(id)
79
+ night = nil
80
+ (doc/"weather/dayf/day").each do |d|
81
+ if d.attributes["d"] == id.to_s
82
+ night = d
83
+ end
84
+ end
85
+ Weather::Night.new(night)
86
+ end
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,57 @@
1
+ module Weather
2
+
3
+ class FutureEstimate
4
+ attr_reader :doc
5
+
6
+ def initialize(doc)
7
+ @doc = doc
8
+ end
9
+
10
+ # Icon
11
+ def icon
12
+ part.at("icon").innerHTML
13
+ end
14
+
15
+ # Description
16
+ def description
17
+ part.at("t").innerHTML
18
+ end
19
+
20
+ # Precipitation
21
+ def precipitation
22
+ part.at("ppcp").innerHTML
23
+ end
24
+
25
+ # Humidity
26
+ def humidity
27
+ part.at("hmid").innerHTML
28
+ end
29
+
30
+ # High temperature
31
+ def high
32
+ doc.at("hi").innerHTML
33
+ end
34
+
35
+ # Low temperature
36
+ def low
37
+ doc.at("low").innerHTML
38
+ end
39
+
40
+ # Sunrise
41
+ def sunrise
42
+ doc.at("sunr").innerHTML
43
+ end
44
+
45
+ # Sunset
46
+ def sunset
47
+ doc.at("suns").innerHTML
48
+ end
49
+
50
+ # Wind conditions
51
+ def wind
52
+ Weather::Wind.new(part.at("wind"))
53
+ end
54
+
55
+ end
56
+
57
+ end
data/lib/Night.rb ADDED
@@ -0,0 +1,24 @@
1
+ require File.dirname(File.expand_path(__FILE__)) + '/FutureEstimate'
2
+
3
+ module Weather
4
+
5
+ class Night < Weather::FutureEstimate
6
+ attr_reader :doc
7
+
8
+ def initialize(doc)
9
+ super(doc)
10
+ end
11
+
12
+ # Select night part in the doc
13
+ def part
14
+ night = nil
15
+ (doc/"part").each do |d|
16
+ if d.attributes["p"] == "n"
17
+ night = d
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,92 @@
1
+ require "net/http"
2
+ require "cgi"
3
+ require "hpricot"
4
+ require "memcache"
5
+
6
+ require File.dirname(File.expand_path(__FILE__)) + '/Forecast'
7
+
8
+ module Weather
9
+
10
+ class WeatherService
11
+ # Weather.com HOST
12
+ XOAP_HOST = "xoap.weather.com"
13
+
14
+ # Default unit ajusted to Farenheit degrees :)
15
+ def load_forecast(location_id, days = 1, unit = 's')
16
+ # Partner ID and License Key provided by Weather.com
17
+ partner_id = "1094780686"
18
+ license_key = "d2f90d27351df0a8"
19
+
20
+ # URL to fetch the forecast from Weather.com
21
+ url = "/weather/local/#{location_id}?cc=*&dayf=#{days}&par=#{partner_id}&key=#{license_key}&unit=#{unit}"
22
+
23
+ if (cache_ok?)
24
+ # Retrieving data from cache
25
+ xml = @cache.get("#{location_id}:#{days}")
26
+ end
27
+
28
+ if (xml == nil)
29
+ # Retrieving the XML doc from weather.com
30
+ xml = Net::HTTP.get(XOAP_HOST, url)
31
+
32
+ if (cache_ok?)
33
+ # Setting data on the cache
34
+ @cache.set("#{location_id}:#{days}", xml.to_s, cache_expiry)
35
+ end
36
+ end
37
+
38
+ # Using Hpricot to parse the data
39
+ doc = Hpricot.XML(xml)
40
+
41
+ # Passing doc to the Forecast object
42
+ Weather::Forecast.new(doc)
43
+ end
44
+
45
+ def find_location_id(search_string)
46
+ # URL encode
47
+ url = "/weather/search/search?where=#{CGI.escape(search_string)}"
48
+
49
+ # Retrieving the XML doc
50
+ xml = Net::HTTP.get(XOAP_HOST, url);
51
+
52
+ # Using Hpricot to parser the data
53
+ doc = Hpricot.XML(xml)
54
+ location_id = doc.at("/search/loc")["id"]
55
+ end
56
+
57
+ # Enables cache
58
+ def enable_cache
59
+ @enable_cache = true
60
+ end
61
+
62
+ # Disables cache
63
+ def disable_cache
64
+ @enable_cache = false
65
+ end
66
+
67
+ # Defines default expiration time in seconds (ten minutes)
68
+ def cache_expiry=(seconds)
69
+ @cache_expiry = seconds
70
+ end
71
+
72
+ # Defines expiration time in seconds
73
+ def cache_expiry
74
+ @cache_expiry || 60 * 10
75
+ end
76
+
77
+ # Used to instantiate the MemCache
78
+ def cache
79
+ @cache ||= MemCache.new(:namespace => 'Weather')
80
+ end
81
+
82
+ # Verifies if the cache is enabled and actived
83
+ def cache_ok?
84
+ @enable_cache and
85
+ @cache.active? and
86
+ servers = @cache.instance_variable_get(:@servers) and
87
+ servers.collect{|s| s.alive?}.include?(true)
88
+ end
89
+
90
+ end
91
+
92
+ end
data/lib/Wind.rb ADDED
@@ -0,0 +1,32 @@
1
+ module Weather
2
+
3
+ class Wind
4
+ attr_reader :doc
5
+
6
+ def initialize(doc)
7
+ @doc = doc
8
+ end
9
+
10
+ # Wind speed
11
+ def speed
12
+ doc.at("s").innerHTML
13
+ end
14
+
15
+ # Wind gust speed
16
+ def gust_speed
17
+ doc.at("gust").innerHTML
18
+ end
19
+
20
+ # Wind direction
21
+ def direction
22
+ doc.at("d").innerHTML
23
+ end
24
+
25
+ # Wind description
26
+ def description
27
+ doc.at("t").innerHTML
28
+ end
29
+
30
+ end
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyweatherforecast
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - Guilherme Nascimento
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-06 00:00:00 -03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby Weather Forecast built using Hpricot and MemCached.
17
+ email: matt@roughest.net
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE.txt
25
+ files:
26
+ - lib/Current.rb
27
+ - lib/Day.rb
28
+ - lib/Forecast.rb
29
+ - lib/FutureEstimate.rb
30
+ - lib/Night.rb
31
+ - lib/WeatherService.rb
32
+ - lib/Wind.rb
33
+ - README
34
+ - LICENSE.txt
35
+ has_rdoc: true
36
+ homepage: http://rubyforge.org/projects/weatherforecast
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --title
40
+ - RubyWeatherForecast 1.0 RDocs
41
+ - --main
42
+ - README
43
+ - --line-numbers
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project: weatherforecast
61
+ rubygems_version: 1.3.1
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: Ruby Weather Forecast Library.
65
+ test_files: []
66
+