barometer 0.6.7 → 0.7.0

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.
Files changed (50) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +1 -1
  4. data/README.rdoc +36 -8
  5. data/Rakefile +7 -28
  6. data/TODO +4 -0
  7. data/VERSION.yml +2 -2
  8. data/barometer.gemspec +20 -193
  9. data/bin/barometer +8 -5
  10. data/lib/barometer.rb +5 -1
  11. data/lib/barometer/data/local_datetime.rb +11 -7
  12. data/lib/barometer/formats.rb +2 -1
  13. data/lib/barometer/formats/coordinates.rb +15 -3
  14. data/lib/barometer/formats/format.rb +18 -0
  15. data/lib/barometer/formats/geocode.rb +12 -5
  16. data/lib/barometer/formats/weather_id.rb +0 -15
  17. data/lib/barometer/formats/woe_id.rb +150 -0
  18. data/lib/barometer/query.rb +20 -4
  19. data/lib/barometer/services.rb +2 -1
  20. data/lib/barometer/weather_services/service.rb +0 -4
  21. data/lib/barometer/weather_services/weather_bug.rb +2 -2
  22. data/lib/barometer/weather_services/yahoo.rb +14 -3
  23. data/lib/barometer/web_services/placemaker.rb +95 -0
  24. data/lib/barometer/web_services/web_service.rb +1 -4
  25. data/spec/barometer_spec.rb +14 -32
  26. data/spec/data/local_datetime_spec.rb +7 -2
  27. data/spec/data/zone_spec.rb +5 -2
  28. data/spec/fakeweb_helper.rb +158 -0
  29. data/spec/fixtures/services/placemaker/T5B4M9.xml +65 -0
  30. data/spec/fixtures/services/placemaker/atlanta.xml +65 -0
  31. data/spec/fixtures/services/placemaker/coords.xml +65 -0
  32. data/spec/fixtures/services/placemaker/ksfo.xml +65 -0
  33. data/spec/fixtures/services/placemaker/new_york.xml +65 -0
  34. data/spec/fixtures/services/placemaker/the_hills.xml +65 -0
  35. data/spec/fixtures/services/placemaker/w615702.xml +46 -0
  36. data/spec/fixtures/services/weather_bug/90210_forecast.xml +1 -1
  37. data/spec/formats/coordinates_spec.rb +16 -0
  38. data/spec/formats/format_spec.rb +9 -0
  39. data/spec/formats/woe_id_spec.rb +215 -0
  40. data/spec/query_spec.rb +38 -0
  41. data/spec/spec_helper.rb +7 -160
  42. data/spec/weather_services/google_spec.rb +0 -8
  43. data/spec/weather_services/services_spec.rb +1 -1
  44. data/spec/weather_services/weather_bug_spec.rb +0 -36
  45. data/spec/weather_services/weather_dot_com_spec.rb +0 -20
  46. data/spec/weather_services/wunderground_spec.rb +0 -30
  47. data/spec/weather_services/yahoo_spec.rb +1 -17
  48. data/spec/web_services/placemaker_spec.rb +46 -0
  49. metadata +136 -18
  50. data/lib/barometer/extensions/httparty.rb +0 -21
data/spec/query_spec.rb CHANGED
@@ -104,6 +104,19 @@ describe "Query" do
104
104
  @query.q.should be_nil
105
105
  end
106
106
 
107
+ it "attempts query conversion when reading q (if format known)" do
108
+ query = Barometer::Query.new(@geocode)
109
+ query.format.should == :geocode
110
+ Barometer::Query::Format::Geocode.should_receive(:convert_query).once.with(@geocode).and_return(@geocode)
111
+ query.q.should == @geocode
112
+ end
113
+
114
+ it "does not attempt query conversion when reading q (if format unknown)" do
115
+ @query.format.should be_nil
116
+ Barometer::Query::Format.should_not_receive(:convert_query)
117
+ @query.q.should be_nil
118
+ end
119
+
107
120
  it "responds to format" do
108
121
  @query.format.should be_nil
109
122
  end
@@ -144,6 +157,31 @@ describe "Query" do
144
157
  @query.conversions.should be_nil
145
158
  end
146
159
 
160
+ it "returns latitude for when recognized as coordinates" do
161
+ @query.q = @coordinates
162
+ @query.format.should be_nil
163
+ @query.analyze!
164
+ @query.format.to_sym.should == :coordinates
165
+ @query.latitude.should == @coordinates.split(',')[0]
166
+ end
167
+
168
+ it "returns longitude for when recognized as coordinates" do
169
+ @query.q = @coordinates
170
+ @query.format.should be_nil
171
+ @query.analyze!
172
+ @query.format.to_sym.should == :coordinates
173
+ @query.longitude.should == @coordinates.split(',')[1]
174
+ end
175
+
176
+ it "returns nothing for latitude/longitude when not coordinates" do
177
+ @query.q = @geocode
178
+ @query.format.should be_nil
179
+ @query.analyze!
180
+ @query.format.to_sym.should == :geocode
181
+ @query.latitude.should be_nil
182
+ @query.longitude.should be_nil
183
+ end
184
+
147
185
  end
148
186
 
149
187
  describe "when returning the query to a Weather API" do
data/spec/spec_helper.rb CHANGED
@@ -1,170 +1,17 @@
1
1
  require 'rubygems'
2
- require 'spec'
2
+ require 'rspec'
3
3
  require 'mocha'
4
- require 'fakeweb'
5
4
  require 'cgi'
6
- require 'yaml'
7
5
 
8
- #$LOAD_PATH.unshift(File.dirname(__FILE__))
9
- #$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require File.expand_path(File.dirname(__FILE__) + '/fakeweb_helper')
7
+
10
8
  $:.unshift((File.join(File.dirname(__FILE__), '..', 'lib')))
11
9
  require 'barometer'
12
10
 
13
- FakeWeb.allow_net_connect = false
14
-
15
- KEY_FILE = File.expand_path(File.join('~', '.barometer'))
16
-
17
- def geocode_google_key_message
18
- puts
19
- puts "Please update the key_file '#{KEY_FILE}' with your google api key"
20
- puts "example:"
21
- puts "google: geocode: YOUR_KEY_KERE"
22
- puts
23
- end
24
-
25
- if File.exists?(KEY_FILE)
26
- keys = YAML.load_file(KEY_FILE)
27
- if keys["google"]
28
- KEY = keys["google"]["geocode"]
29
- else
30
- geocode_google_key_message
31
- exit
32
- end
33
- if keys["weather"]
34
- WEATHER_PARTNER_KEY = keys["weather"]["partner"]
35
- WEATHER_LICENSE_KEY = keys["weather"]["license"]
36
- end
37
- if keys["weather_bug"]
38
- WEATHERBUG_CODE = keys["weather_bug"]["code"]
39
- end
40
-
41
- else
42
- File.open(KEY_FILE, 'w') {|f| f << "google: geocode: YOUR_KEY_KERE" }
43
- geocode_google_key_message
44
- exit
45
- end
46
-
47
- #
48
- # for geocoding
49
- #
50
- geo_url = "http://maps.google.com/maps/geo?"
51
- FakeWeb.register_uri(:get,
52
- "#{geo_url}gl=US&key=#{KEY}&sensor=false&q=90210&output=json",
53
- :body => File.read(File.join(File.dirname(__FILE__),
54
- 'fixtures/geocode',
55
- '90210.json')
56
- )
57
- )
58
- FakeWeb.register_uri(:get,
59
- "#{geo_url}gl=&q=#{CGI.escape("40.756054,-73.986951")}&output=json&key=#{KEY}&sensor=false",
60
- :body => File.read(File.join(File.dirname(__FILE__),
61
- 'fixtures/geocode',
62
- '40_73.json')
63
- )
64
- )
65
-
66
- FakeWeb.register_uri(:get,
67
- "#{geo_url}gl=&q=New%20York%2C%20NY&output=json&key=#{KEY}&sensor=false",
68
- :body => File.read(File.join(File.dirname(__FILE__),
69
- 'fixtures/geocode',
70
- 'newyork_ny.json')
71
- )
72
- )
73
- FakeWeb.register_uri(:get,
74
- "#{geo_url}gl=CA&key=#{KEY}&output=json&q=T5B%204M9&sensor=false",
75
- :body => File.read(File.join(File.dirname(__FILE__),
76
- 'fixtures/geocode',
77
- 'T5B4M9.json')
78
- )
79
- )
80
- FakeWeb.register_uri(:get,
81
- "#{geo_url}gl=US&q=KSFO&output=json&key=#{KEY}&sensor=false",
82
- :body => File.read(File.join(File.dirname(__FILE__),
83
- 'fixtures/geocode',
84
- 'ksfo.json')
85
- )
86
- )
87
- FakeWeb.register_uri(:get,
88
- "#{geo_url}gl=&q=Atlanta%2C%20GA%2C%20US&output=json&key=#{KEY}&sensor=false",
89
- :body => File.read(File.join(File.dirname(__FILE__),
90
- 'fixtures/geocode',
91
- 'atlanta.json')
92
- )
93
- )
94
- FakeWeb.register_uri(:get,
95
- "#{geo_url}output=xml&q=Atlanta%2C%20GA%2C%20US&gl=US&key=#{KEY}",
96
- :body => File.read(File.join(File.dirname(__FILE__),
97
- 'fixtures/geocode',
98
- 'atlanta.xml')
99
- )
100
- )
101
- #
102
- # for weather.com searches
103
- #
104
- FakeWeb.register_uri(:get,
105
- "http://xoap.weather.com:80/search/search?where=Beverly%20Hills%2C%20CA%2C%20USA",
106
- :body => File.read(File.join(File.dirname(__FILE__),
107
- 'fixtures/formats/weather_id',
108
- '90210.xml')
109
- )
110
- )
111
- FakeWeb.register_uri(:get,
112
- "http://xoap.weather.com:80/search/search?where=New%20York%2C%20NY",
113
- :body => File.read(File.join(File.dirname(__FILE__),
114
- 'fixtures/formats/weather_id',
115
- 'new_york.xml')
116
- )
117
- )
118
- FakeWeb.register_uri(:get,
119
- "http://xoap.weather.com:80/search/search?where=New%20York%2C%20NY%2C%20USA",
120
- :body => File.read(File.join(File.dirname(__FILE__),
121
- 'fixtures/formats/weather_id',
122
- 'new_york.xml')
123
- )
124
- )
125
- FakeWeb.register_uri(:get,
126
- "http://xoap.weather.com:80/search/search?where=90210",
127
- :body => File.read(File.join(File.dirname(__FILE__),
128
- 'fixtures/formats/weather_id',
129
- '90210.xml')
130
- )
131
- )
132
- FakeWeb.register_uri(:get,
133
- "http://xoap.weather.com:80/search/search?where=Millbrae%2C%20CA%2C%20USA",
134
- :body => File.read(File.join(File.dirname(__FILE__),
135
- 'fixtures/formats/weather_id',
136
- 'ksfo.xml')
137
- )
138
- )
139
- #
140
- # for yahoo.com searches
141
- #
142
- FakeWeb.register_uri(:get,
143
- "http://weather.yahooapis.com:80/forecastrss?p=USGA0028",
144
- :body => File.read(File.join(File.dirname(__FILE__),
145
- 'fixtures/formats/weather_id',
146
- 'from_USGA0028.xml')
147
- )
148
- )
149
-
150
- #
151
- # For wunderground weather
152
- #
153
- FakeWeb.register_uri(:get,
154
- "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=51.055149%2C-114.062438",
155
- :body => File.read(File.join(File.dirname(__FILE__),
156
- 'fixtures/services/wunderground',
157
- 'current_calgary_ab.xml')
158
- )
159
- )
160
- FakeWeb.register_uri(:get,
161
- "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=51.055149%2C-114.062438",
162
- :body => File.read(File.join(File.dirname(__FILE__),
163
- 'fixtures/services/wunderground',
164
- 'forecast_calgary_ab.xml')
165
- )
166
- )
11
+ #Barometer.debug!
12
+ Barometer.google_geocode_key = "ABC123"
13
+ Barometer.yahoo_placemaker_app_id = "YAHOO"
167
14
 
168
- Spec::Runner.configure do |config|
15
+ RSpec.configure do |config|
169
16
 
170
17
  end
@@ -102,14 +102,6 @@ describe "Google" do
102
102
  before(:each) do
103
103
  @query = Barometer::Query.new("Calgary,AB")
104
104
  @measurement = Barometer::Measurement.new
105
-
106
- FakeWeb.register_uri(:get,
107
- "http://google.com/ig/api?weather=#{CGI.escape(@query.q)}&hl=en-GB",
108
- :body => File.read(File.join(File.dirname(__FILE__),
109
- '../fixtures/services/google',
110
- 'calgary_ab.xml')
111
- )
112
- )
113
105
  end
114
106
 
115
107
  describe "all" do
@@ -31,7 +31,7 @@ describe "WeatherServices" do
31
31
  end
32
32
 
33
33
  it "returns the corresponding Service object" do
34
- Barometer::WeatherService.source(:wunderground).should == WeatherService::Wunderground
34
+ Barometer::WeatherService.source(:wunderground).should == Barometer::WeatherService::Wunderground
35
35
  Barometer::WeatherService.source(:wunderground).superclass.should == Barometer::WeatherService
36
36
  end
37
37
 
@@ -138,24 +138,6 @@ describe "WeatherBug" do
138
138
  before(:each) do
139
139
  @query = Barometer::Query.new("90210")
140
140
  @measurement = Barometer::Measurement.new
141
-
142
- url = "http://#{WEATHERBUG_CODE}.api.wxbug.net:80/getLiveWeatherRSS.aspx?"
143
- FakeWeb.register_uri(:get,
144
- "#{url}ACode=#{WEATHERBUG_CODE}&OutputType=1&UnitType=1&zipCode=90210",
145
- :body => File.read(File.join(File.dirname(__FILE__),
146
- '../fixtures/services/weather_bug',
147
- '90210_current.xml')
148
- )
149
- )
150
-
151
- url2 = "http://#{WEATHERBUG_CODE}.api.wxbug.net:80/getForecastRSS.aspx?"
152
- FakeWeb.register_uri(:get,
153
- "#{url2}ACode=#{WEATHERBUG_CODE}&OutputType=1&UnitType=1&zipCode=90210",
154
- :body => File.read(File.join(File.dirname(__FILE__),
155
- '../fixtures/services/weather_bug',
156
- '90210_forecast.xml')
157
- )
158
- )
159
141
  end
160
142
 
161
143
  describe "all" do
@@ -194,24 +176,6 @@ describe "WeatherBug" do
194
176
  before(:each) do
195
177
  @query = Barometer::Query.new("90210")
196
178
  @measurement = Barometer::Measurement.new
197
-
198
- url = "http://#{WEATHERBUG_CODE}.api.wxbug.net:80/getLiveWeatherRSS.aspx?"
199
- FakeWeb.register_uri(:get,
200
- "#{url}ACode=#{WEATHERBUG_CODE}&OutputType=1&UnitType=1&zipCode=90210",
201
- :body => File.read(File.join(File.dirname(__FILE__),
202
- '../fixtures/services/weather_bug',
203
- '90210_current.xml')
204
- )
205
- )
206
-
207
- url2 = "http://#{WEATHERBUG_CODE}.api.wxbug.net:80/getForecastRSS.aspx?"
208
- FakeWeb.register_uri(:get,
209
- "#{url2}ACode=#{WEATHERBUG_CODE}&OutputType=1&UnitType=1&zipCode=90210",
210
- :body => File.read(File.join(File.dirname(__FILE__),
211
- '../fixtures/services/weather_bug',
212
- '90210_forecast.xml')
213
- )
214
- )
215
179
  end
216
180
 
217
181
  it "should correctly build the data" do
@@ -118,16 +118,6 @@ describe "WeatherDotCom" do
118
118
  before(:each) do
119
119
  @query = Barometer::Query.new("90210")
120
120
  @measurement = Barometer::Measurement.new
121
-
122
- url = "http://xoap.weather.com:80/weather/local/"
123
-
124
- FakeWeb.register_uri(:get,
125
- "#{url}90210?dayf=5&unit=m&link=xoap&par=#{WEATHER_PARTNER_KEY}&prod=xoap&key=#{WEATHER_LICENSE_KEY}&cc=*",
126
- :body => File.read(File.join(File.dirname(__FILE__),
127
- '../fixtures/services/weather_dot_com',
128
- '90210.xml')
129
- )
130
- )
131
121
  end
132
122
 
133
123
  describe "all" do
@@ -166,16 +156,6 @@ describe "WeatherDotCom" do
166
156
  before(:each) do
167
157
  @query = Barometer::Query.new("90210")
168
158
  @measurement = Barometer::Measurement.new
169
-
170
- url = "http://xoap.weather.com:80/weather/local/"
171
-
172
- FakeWeb.register_uri(:get,
173
- "#{url}90210?dayf=5&unit=m&link=xoap&par=#{WEATHER_PARTNER_KEY}&prod=xoap&key=#{WEATHER_LICENSE_KEY}&cc=*",
174
- :body => File.read(File.join(File.dirname(__FILE__),
175
- '../fixtures/services/weather_dot_com',
176
- '90210.xml')
177
- )
178
- )
179
159
  end
180
160
 
181
161
  it "should correctly build the data" do
@@ -142,21 +142,6 @@ describe "Wunderground" do
142
142
  before(:each) do
143
143
  @query = Barometer::Query.new("Calgary,AB")
144
144
  @measurement = Barometer::Measurement.new
145
-
146
- FakeWeb.register_uri(:get,
147
- "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=#{CGI.escape(@query.q)}",
148
- :body => File.read(File.join(File.dirname(__FILE__),
149
- '../fixtures/services/wunderground',
150
- 'current_calgary_ab.xml')
151
- )
152
- )
153
- FakeWeb.register_uri(:get,
154
- "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=#{CGI.escape(@query.q)}",
155
- :body => File.read(File.join(File.dirname(__FILE__),
156
- '../fixtures/services/wunderground',
157
- 'forecast_calgary_ab.xml')
158
- )
159
- )
160
145
  end
161
146
 
162
147
  describe "all" do
@@ -195,21 +180,6 @@ describe "Wunderground" do
195
180
  before(:each) do
196
181
  @query = Barometer::Query.new("Calgary,AB")
197
182
  @measurement = Barometer::Measurement.new
198
-
199
- FakeWeb.register_uri(:get,
200
- "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=#{CGI.escape(@query.q)}",
201
- :body => File.read(File.join(File.dirname(__FILE__),
202
- '../fixtures/services/wunderground',
203
- 'current_calgary_ab.xml')
204
- )
205
- )
206
- FakeWeb.register_uri(:get,
207
- "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=#{CGI.escape(@query.q)}",
208
- :body => File.read(File.join(File.dirname(__FILE__),
209
- '../fixtures/services/wunderground',
210
- 'forecast_calgary_ab.xml')
211
- )
212
- )
213
183
  end
214
184
 
215
185
  # TODO: complete this
@@ -4,7 +4,7 @@ include Barometer
4
4
  describe "Yahoo" do
5
5
 
6
6
  before(:each) do
7
- @accepted_formats = [:zipcode, :weather_id]
7
+ @accepted_formats = [:zipcode, :weather_id, :woe_id]
8
8
  #@base_uri = "http://google.com"
9
9
  end
10
10
 
@@ -89,14 +89,6 @@ describe "Yahoo" do
89
89
  before(:each) do
90
90
  @query = Barometer::Query.new("90210")
91
91
  @measurement = Barometer::Measurement.new
92
-
93
- FakeWeb.register_uri(:get,
94
- "http://weather.yahooapis.com:80/forecastrss?u=c&p=#{CGI.escape(@query.q)}",
95
- :body => File.read(File.join(File.dirname(__FILE__),
96
- '../fixtures/services/yahoo',
97
- '90210.xml')
98
- )
99
- )
100
92
  end
101
93
 
102
94
  describe "all" do
@@ -135,14 +127,6 @@ describe "Yahoo" do
135
127
  before(:each) do
136
128
  @query = Barometer::Query.new("90210")
137
129
  @measurement = Barometer::Measurement.new
138
-
139
- FakeWeb.register_uri(:get,
140
- "http://weather.yahooapis.com:80/forecastrss?u=c&p=#{CGI.escape(@query.q)}",
141
- :body => File.read(File.join(File.dirname(__FILE__),
142
- '../fixtures/services/yahoo',
143
- '90210.xml')
144
- )
145
- )
146
130
  end
147
131
 
148
132
  # TODO: complete this
@@ -0,0 +1,46 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "WebService::Placemaker" do
4
+
5
+ before(:each) do
6
+ @coordinates = "40.756054,-73.986951"
7
+ @geocode = "New York, NY"
8
+ end
9
+
10
+ describe "and the class methods" do
11
+
12
+ describe "fetch," do
13
+
14
+ it "requires a Query object" do
15
+ lambda { Barometer::WebService::Placemaker.fetch }.should raise_error(ArgumentError)
16
+ lambda { Barometer::WebService::Placemaker.fetch("invalid") }.should raise_error(ArgumentError)
17
+ query = Barometer::Query.new(@coordinates)
18
+ query.is_a?(Barometer::Query).should be_true
19
+ lambda { Barometer::WebService::Placemaker.fetch(query) }.should_not raise_error(ArgumentError)
20
+ end
21
+
22
+ # it "detects the key" do
23
+ # query = Barometer::Query.new(@zipcode)
24
+ # Barometer.google_geocode_key = nil
25
+ # Barometer::WebService::Geocode.fetch(query).should be_nil
26
+ # Barometer.google_geocode_key = KEY
27
+ # Barometer::WebService::Geocode.fetch(query).should_not be_nil
28
+ # end
29
+ #
30
+ # it "returns a Geo object" do
31
+ # query = Barometer::Query.new(@zipcode)
32
+ # Barometer::WebService::Geocode.fetch(query).is_a?(Data::Geo).should be_true
33
+ # end
34
+
35
+ end
36
+
37
+ # it "detects the Google Geocode Key" do
38
+ # Barometer.google_geocode_key = nil
39
+ # Barometer::WebService::Geocode.send("_has_geocode_key?").should be_false
40
+ # Barometer.google_geocode_key = KEY
41
+ # Barometer::WebService::Geocode.send("_has_geocode_key?").should be_true
42
+ # end
43
+
44
+ end
45
+
46
+ end