ckozus-rweather 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +31 -0
- data/lib/r_weather.rb +53 -0
- data/lib/r_weather_current_condition.rb +31 -0
- data/spec/current_condition.xml +69 -0
- data/spec/r_weather_current_condition_spec.rb +54 -0
- metadata +66 -0
data/README
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
== rweather
|
2
|
+
|
3
|
+
Ruby gem to access Weather Channel (weather.com) XML API.
|
4
|
+
|
5
|
+
This gems lets you access to the search and get current conditions features. Forecast it's not implemented yet.
|
6
|
+
|
7
|
+
In order to use the API you will need first to get your partner_id and licence key from weather.com at this address:
|
8
|
+
http://www.weather.com/services/xmloap.html
|
9
|
+
|
10
|
+
Sign up, enter all your data and you'll get an email with the partner_id, licence key and a link to download the SDK info.
|
11
|
+
If you need to read the license agreement and the API doc you can do it here:
|
12
|
+
http://download.weather.com/web/xml/sdk.zip
|
13
|
+
|
14
|
+
== Example
|
15
|
+
|
16
|
+
require 'rubygems'
|
17
|
+
require 'r_weather'
|
18
|
+
|
19
|
+
RWeather.partner_id = "xxxxxxxxxx"
|
20
|
+
RWeather.key = "yyyyyyyyyyyyyyyy"
|
21
|
+
|
22
|
+
locations = RWeather.search('tucuman') # that's where I'm from :)
|
23
|
+
unless locations.empty?
|
24
|
+
locations.each_with_index do |location, i|
|
25
|
+
puts "#{i}) #{location.id} - #{location.name}"
|
26
|
+
end
|
27
|
+
cc = RWeather.current_conditions(locations.first.id)
|
28
|
+
puts "Current conditions: "
|
29
|
+
puts " Temperature: #{cc.tmp}"
|
30
|
+
puts " Feels like: #{cc.flik}"
|
31
|
+
end
|
data/lib/r_weather.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'xmlsimple'
|
3
|
+
require 'r_weather_current_condition'
|
4
|
+
|
5
|
+
RWeatherLocation = Struct.new(:type, :id, :name)
|
6
|
+
|
7
|
+
class RWeather
|
8
|
+
|
9
|
+
NOT_AUTHENTICATED = 'You should set partner_id and key values on RWeather before calling data request actions.'
|
10
|
+
SEARCH_URL = 'http://xoap.weather.com/search/search?where=%s'
|
11
|
+
LOCAL_URL = 'http://xoap.weather.com/weather/local/%s?par=%s&key=%s&link=xoap&prod=xoap'
|
12
|
+
|
13
|
+
VALID_UNITS = ['s', 'm']
|
14
|
+
DEFAULT_UNIT = 's' #standard
|
15
|
+
|
16
|
+
def self.partner_id; @@partner_id; end
|
17
|
+
def self.partner_id=(value); @@partner_id = value; end
|
18
|
+
|
19
|
+
def self.key; @@key; end
|
20
|
+
def self.key=(value); @@key = value; end
|
21
|
+
|
22
|
+
def self.search(where)
|
23
|
+
if data = get_and_parse(sprintf(SEARCH_URL, URI.encode(where)))
|
24
|
+
data['loc'].map{|loc| RWeatherLocation.new(loc['type'], loc['id'], loc['content'])}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.current_conditions(location_id, unit = DEFAULT_UNIT)
|
29
|
+
check_authentication
|
30
|
+
url = sprintf(LOCAL_URL, location_id, partner_id, key)
|
31
|
+
url << "&cc=true"
|
32
|
+
url << "&unit=#{unit}" if VALID_UNITS.include?(unit)
|
33
|
+
if data = get_and_parse(URI.encode(url))
|
34
|
+
RWeatherCurrentCondition.parse(data)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.local(partner_id, key)
|
39
|
+
check_authentication
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
class << self
|
44
|
+
def check_authentication
|
45
|
+
raise Exception.new(NOT_AUTHENTICATED) if @@partner_id.nil? || @@partner_id.empty? || @@key.nil? || @@key.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_and_parse(url)
|
49
|
+
xml_data = Net::HTTP.get_response(URI.parse(url)).body
|
50
|
+
XmlSimple.xml_in(xml_data)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
RWeatherBar = Struct.new(:r, :d)
|
2
|
+
RWeatherWind = Struct.new(:s, :gust, :d, :t)
|
3
|
+
RWeatherUv = Struct.new(:i, :t)
|
4
|
+
RWeatherMoon = Struct.new(:icon, :t)
|
5
|
+
|
6
|
+
class RWeatherCurrentCondition
|
7
|
+
attr_accessor :lsup, :obst, :tmp, :flik, :t, :icon, :bar, :wind, :hmid, :vis, :uv, :dewp, :moon
|
8
|
+
|
9
|
+
def self.parse(simple_xml)
|
10
|
+
current_condition = new
|
11
|
+
cc = simple_xml['cc'].first
|
12
|
+
cc.each do |key, value|
|
13
|
+
current_condition.send(:"#{key}=", element_value(key, value))
|
14
|
+
end
|
15
|
+
current_condition
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def element_value(key, value)
|
20
|
+
if value.size == 1 && !value.first.is_a?(Hash)
|
21
|
+
value.first
|
22
|
+
else
|
23
|
+
data = const_get(:"RWeather#{key.capitalize}").new
|
24
|
+
value.first.each do |key, value|
|
25
|
+
data.send(:"#{key}=", value.first)
|
26
|
+
end
|
27
|
+
data
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1"?>
|
2
|
+
<!--This document is intended only for use by authorized licensees of The Weather Channel. Unauthorized use is prohibited. Copyright 1995-2005, The Weather Channel Interactive, Inc. All Rights Reserved.-->
|
3
|
+
<weather ver="2.0">
|
4
|
+
<head>
|
5
|
+
<locale>en_US</locale>
|
6
|
+
<form>MEDIUM</form>
|
7
|
+
<ut>C</ut>
|
8
|
+
<ud>km</ud>
|
9
|
+
<us>km/h</us>
|
10
|
+
<up>mb</up>
|
11
|
+
<ur>mm</ur>
|
12
|
+
</head>
|
13
|
+
<loc id="ARTN0088">
|
14
|
+
<dnam>San Miguel de Tucuman, Argentina</dnam>
|
15
|
+
<tm>11:23 AM</tm>
|
16
|
+
<lat>-26.83</lat>
|
17
|
+
<lon>-65.21</lon>
|
18
|
+
<sunr>8:09 AM</sunr>
|
19
|
+
<suns>6:36 PM</suns>
|
20
|
+
<zone>-3</zone>
|
21
|
+
</loc>
|
22
|
+
<lnks type="prmo">
|
23
|
+
<link pos="1">
|
24
|
+
<l>http://www.weather.com/allergies?par=xoap&site=textlink&cm_ven=XOAP&cm_cat=TextLink&cm_pla=Link1&cm_ite=Allergies</l>
|
25
|
+
<t>Local Pollen Reports</t>
|
26
|
+
</link>
|
27
|
+
<link pos="2">
|
28
|
+
<l>http://www.weather.com/flights?par=xoap&site=textlink&cm_ven=XOAP&cm_cat=TextLink&cm_pla=Link2&cm_ite=BusinessTraveler</l>
|
29
|
+
<t>Airport Conditions</t>
|
30
|
+
</link>
|
31
|
+
<link pos="3">
|
32
|
+
<l>http://www.weather.com/garden?par=xoap&site=textlink&cm_ven=XOAP&cm_cat=TextLink&cm_pla=Link3&cm_ite=Garden</l>
|
33
|
+
<t>Lawn and Garden Weather</t>
|
34
|
+
</link>
|
35
|
+
<link pos="4">
|
36
|
+
<l>http://www.weather.com/traffic?par=xoap&site=textlink&cm_ven=XOAP&cm_cat=TextLink&cm_pla=Link4&cm_ite=Traffic</l>
|
37
|
+
<t>Rush Hour Traffic</t>
|
38
|
+
</link>
|
39
|
+
</lnks>
|
40
|
+
<cc>
|
41
|
+
<lsup>6/22/08 11:00 AM Local Time</lsup>
|
42
|
+
<obst>San Miguel de Tucuman, Argentina</obst>
|
43
|
+
<tmp>7</tmp>
|
44
|
+
<flik>5</flik>
|
45
|
+
<t>Partly Cloudy</t>
|
46
|
+
<icon>30</icon>
|
47
|
+
<bar>
|
48
|
+
<r>1026.1</r>
|
49
|
+
<d>steady</d>
|
50
|
+
</bar>
|
51
|
+
<wind>
|
52
|
+
<s>11</s>
|
53
|
+
<gust>N/A</gust>
|
54
|
+
<d>50</d>
|
55
|
+
<t>NE</t>
|
56
|
+
</wind>
|
57
|
+
<hmid>87</hmid>
|
58
|
+
<vis>6.0</vis>
|
59
|
+
<uv>
|
60
|
+
<i>2</i>
|
61
|
+
<t>Low</t>
|
62
|
+
</uv>
|
63
|
+
<dewp>5</dewp>
|
64
|
+
<moon>
|
65
|
+
<icon>18</icon>
|
66
|
+
<t>Waning Gibbous</t>
|
67
|
+
</moon>
|
68
|
+
</cc>
|
69
|
+
</weather>
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'lib/r_weather_current_condition'
|
2
|
+
require 'xmlsimple'
|
3
|
+
|
4
|
+
describe RWeatherCurrentCondition, 'parse' do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
file_data = File.open('spec/current_condition.xml', 'r') {|f| f.read}
|
8
|
+
@xml_data = XmlSimple.xml_in(file_data)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should parse simple xml data" do
|
12
|
+
cc = RWeatherCurrentCondition.parse(@xml_data)
|
13
|
+
cc.lsup.should == '6/22/08 11:00 AM Local Time'
|
14
|
+
cc.obst.should == 'San Miguel de Tucuman, Argentina'
|
15
|
+
cc.tmp.should == "7"
|
16
|
+
cc.flik.should == "5"
|
17
|
+
cc.t.should == 'Partly Cloudy'
|
18
|
+
cc.icon.should == "30"
|
19
|
+
cc.hmid.should == "87"
|
20
|
+
cc.vis.should == "6.0"
|
21
|
+
cc.dewp.should == "5"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should parse bar xml data" do
|
25
|
+
cc = RWeatherCurrentCondition.parse(@xml_data)
|
26
|
+
cc.bar.should be_kind_of(RWeatherBar)
|
27
|
+
cc.bar.r.should == '1026.1'
|
28
|
+
cc.bar.d.should == 'steady'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should parse wind xml data" do
|
32
|
+
cc = RWeatherCurrentCondition.parse(@xml_data)
|
33
|
+
cc.wind.should be_kind_of(RWeatherWind)
|
34
|
+
cc.wind.s.should == '11'
|
35
|
+
cc.wind.gust.should == 'N/A'
|
36
|
+
cc.wind.d.should == '50'
|
37
|
+
cc.wind.t.should == 'NE'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should parse uv xml data" do
|
41
|
+
cc = RWeatherCurrentCondition.parse(@xml_data)
|
42
|
+
cc.uv.should be_kind_of(RWeatherUv)
|
43
|
+
cc.uv.i.should == '2'
|
44
|
+
cc.uv.t.should == 'Low'
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should parse moon xml data" do
|
48
|
+
cc = RWeatherCurrentCondition.parse(@xml_data)
|
49
|
+
cc.moon.should be_kind_of(RWeatherMoon)
|
50
|
+
cc.moon.icon.should == '18'
|
51
|
+
cc.moon.t.should == 'Waning Gibbous'
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ckozus-rweather
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Carlos Kozuszko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-09-06 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: xml-simple
|
17
|
+
version_requirement:
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.11
|
23
|
+
version:
|
24
|
+
description: This gems lets you access to the search and get current conditions features.
|
25
|
+
email: ckozus@gmail.com
|
26
|
+
executables: []
|
27
|
+
|
28
|
+
extensions: []
|
29
|
+
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
files:
|
33
|
+
- lib/r_weather.rb
|
34
|
+
- lib/r_weather_current_condition.rb
|
35
|
+
- spec/current_condition.xml
|
36
|
+
- spec/r_weather_current_condition_spec.rb
|
37
|
+
- README
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://github.com/ckozus/rweather
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --main
|
43
|
+
- README
|
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:
|
61
|
+
rubygems_version: 1.2.0
|
62
|
+
signing_key:
|
63
|
+
specification_version: 2
|
64
|
+
summary: Ruby gem to access Weather Channel (weather.com) XML API.
|
65
|
+
test_files: []
|
66
|
+
|