weatherfinder 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/current.rb +46 -0
- data/lib/day.rb +24 -0
- data/lib/forecast.rb +90 -0
- data/lib/future_estimate.rb +57 -0
- data/lib/night.rb +24 -0
- data/lib/weatherfinder.rb +92 -0
- data/lib/wind.rb +32 -0
- data/weatherfinder.gemspec +15 -0
- metadata +61 -0
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__)) + '/future_estimate'
|
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__)) + '/future_estimate'
|
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 WeatherFinder
|
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
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = %q{weatherfinder}
|
3
|
+
s.version = "0.1"
|
4
|
+
|
5
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
|
+
s.authors = ["Guilherme Nascimento"]
|
7
|
+
s.date = %q{2009-02-20}
|
8
|
+
s.description = %q{Weather Finder is a Ruby library to access Weather.com data (XOAP)}
|
9
|
+
s.email = ["javaplayer@gmail.com"]
|
10
|
+
s.files = ["weatherfinder.gemspec","lib/weatherfinder.rb", "lib/current.rb", "lib/day.rb", "lib/forecast.rb", "lib/future_estimate.rb", "lib/night.rb", "lib/wind.rb"]
|
11
|
+
s.require_paths = ["lib"]
|
12
|
+
s.rubyforge_project = %q{weatherfinder}
|
13
|
+
s.rubygems_version = %q{1.3.1}
|
14
|
+
s.summary = %q{Weather Finder is a Ruby library to access Weather.com data (XOAP). The library uses Hpricot and MemCached and allows fetch current forecast data from anywhere location.}
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weatherfinder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Guilherme Nascimento
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-20 00:00:00 -03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Weather Finder is a Ruby library to access Weather.com data (XOAP)
|
17
|
+
email:
|
18
|
+
- javaplayer@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- weatherfinder.gemspec
|
27
|
+
- lib/weatherfinder.rb
|
28
|
+
- lib/current.rb
|
29
|
+
- lib/day.rb
|
30
|
+
- lib/forecast.rb
|
31
|
+
- lib/future_estimate.rb
|
32
|
+
- lib/night.rb
|
33
|
+
- lib/wind.rb
|
34
|
+
has_rdoc: false
|
35
|
+
homepage:
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project: weatherfinder
|
56
|
+
rubygems_version: 1.3.1
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: Weather Finder is a Ruby library to access Weather.com data (XOAP). The library uses Hpricot and MemCached and allows fetch current forecast data from anywhere location.
|
60
|
+
test_files: []
|
61
|
+
|