noaa 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,141 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class TestCurrentConditions < NOAA::TestCase
4
+ XML_DOC = File.open(File.join(File.dirname(__FILE__), 'data', 'KVAY.xml')) { |f| Nokogiri::XML(f) }
5
+
6
+ test 'should return observation time' do
7
+ conditions.observed_at.should == Time.parse('2008-12-23 10:54:00 -0500')
8
+ end
9
+
10
+ test 'should return weather description' do
11
+ conditions.weather_description.should == 'Fair'
12
+ end
13
+
14
+ test 'should return weather description from #weather_summary' do
15
+ conditions.weather_summary.should == 'Fair'
16
+ end
17
+
18
+ test 'should return weather type code' do
19
+ conditions.weather_type_code.should == :skc
20
+ end
21
+
22
+ test 'should return image URL' do
23
+ conditions.image_url.should == 'http://weather.gov/weather/images/fcicons/skc.jpg'
24
+ end
25
+
26
+ test 'should return temperature in fahrenheit by default' do
27
+ conditions.temperature.should == 24
28
+ end
29
+
30
+ test 'should return temperature in fahrenheit when specified' do
31
+ conditions.temperature(:f).should == 24
32
+ end
33
+
34
+ test 'should return temperature in celsius when specified' do
35
+ conditions.temperature(:c).should == -4
36
+ end
37
+
38
+ test 'should raise ArgumentError if unknown unit specified for temperature' do
39
+ lambda { conditions.temperature(:kelvin) }.should raise_error(ArgumentError)
40
+ end
41
+
42
+ test 'should return relative humidity' do
43
+ conditions.relative_humidity.should == 52
44
+ end
45
+
46
+ test 'should return wind direction' do
47
+ conditions.wind_direction.should == 'Northwest'
48
+ end
49
+
50
+ test 'should return wind degrees' do
51
+ conditions.wind_degrees.should == 330
52
+ end
53
+
54
+ test 'should return wind speed in MPH' do
55
+ conditions.wind_speed.should == 3.45
56
+ end
57
+
58
+ test 'should return wind gust in MPH' do
59
+ conditions.wind_gust.should == 10.25
60
+ end
61
+
62
+ #TODO wind gust can be NA
63
+
64
+ test 'should return pressure in inches by default' do
65
+ conditions.pressure.should == 30.7
66
+ end
67
+
68
+ test 'should return pressure in inches when specified' do
69
+ conditions.pressure(:in).should == 30.7
70
+ end
71
+
72
+ test 'should return pressure in millibars when specified' do
73
+ conditions.pressure(:mb).should == 1039.5
74
+ end
75
+
76
+ test 'should throw ArgumentError when unrecognized pressure specified for pressure' do
77
+ lambda { conditions.pressure(:psi) }.should raise_error(ArgumentError)
78
+ end
79
+
80
+ test 'should return dew point in fahrenheit by default' do
81
+ conditions.dew_point.should == 9
82
+ end
83
+
84
+ test 'should return dew point in fahrenheit when specified' do
85
+ conditions.dew_point(:f).should == 9
86
+ end
87
+
88
+ test 'should return dew point in celsius when specified' do
89
+ conditions.dew_point(:c).should == -13
90
+ end
91
+
92
+ test 'should throw ArgumentError when unrecognized unit specified for dew point' do
93
+ lambda { conditions.dew_point(:kelvin) }.should raise_error(ArgumentError)
94
+ end
95
+
96
+ #TODO heat index can be NA
97
+
98
+ test 'should return heat index in fahrenheit by default' do
99
+ conditions.heat_index.should == 105
100
+ end
101
+
102
+ test 'should return heat index in fahrenheit when specified' do
103
+ conditions.heat_index(:f).should == 105
104
+ end
105
+
106
+ test 'should return heat index in celsius when specified' do
107
+ conditions.heat_index(:c).should == 41
108
+ end
109
+
110
+ test 'should throw ArgumentError when unrecognized unit specified for heat index' do
111
+ lambda { conditions.heat_index(:kelvin) }.should raise_error(ArgumentError)
112
+ end
113
+
114
+ #TODO wind chill can be NA
115
+
116
+ test 'should return wind chill in fahrenheit by default' do
117
+ conditions.wind_chill.should == 19
118
+ end
119
+
120
+ test 'should return wind chill in fahrenheit when specified' do
121
+ conditions.wind_chill(:f).should == 19
122
+ end
123
+
124
+ test 'should return wind chill in celsius when specified' do
125
+ conditions.wind_chill(:c).should == -7
126
+ end
127
+
128
+ test 'should throw ArgumentError when unrecognized unit specified for wind chill' do
129
+ lambda { conditions.wind_chill(:kelvin) }.should raise_error(ArgumentError)
130
+ end
131
+
132
+ test 'should return visibility in miles' do
133
+ conditions.visibility.should == 10.0
134
+ end
135
+
136
+ private
137
+
138
+ def conditions
139
+ @conditions ||= NOAA::CurrentConditions.from_xml(XML_DOC)
140
+ end
141
+ end
@@ -0,0 +1,69 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class TestForecast < NOAA::TestCase
4
+ XML_DOC = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'data', '4-day.xml'))
5
+
6
+ should 'return number of days' do
7
+ forecast.length.should == 4
8
+ end
9
+
10
+ ['2008-12-23', '2008-12-24', '2008-12-25', '2008-12-26'].each_with_index do |date, i|
11
+ should "return correct start time for day #{i}" do
12
+ forecast[i].starts_at.should == Time.parse("#{date} 06:00:00 -05:00")
13
+ end
14
+ end
15
+
16
+ ['2008-12-24', '2008-12-25', '2008-12-26', '2008-12-27'].each_with_index do |date, i|
17
+ should "return correct end time for day #{i}" do
18
+ forecast[i].ends_at.should == Time.parse("#{date} 06:00:00 -05:00")
19
+ end
20
+ end
21
+
22
+ [32, 49, 43, 41].each_with_index do |temp, i|
23
+ should "return correct high for day #{i}" do
24
+ forecast[i].high.should == temp
25
+ end
26
+ end
27
+
28
+ [31, 41, 33, 39].each_with_index do |temp, i|
29
+ should "return correct low for day #{i}" do
30
+ forecast[i].low.should == temp
31
+ end
32
+ end
33
+
34
+ ['Rain', 'Rain', 'Slight Chance Rain', 'Chance Rain'].each_with_index do |summary, i|
35
+ should "return correct weather summary for day #{i}" do
36
+ forecast[i].weather_summary.should == summary
37
+ end
38
+ end
39
+
40
+ 4.times do |i|
41
+ should "return correct weather type code for day #{i}" do
42
+ forecast[i].weather_type_code.should == :ra
43
+ end
44
+ end
45
+
46
+ [80, 90, 20, 50].each_with_index do |probability, i|
47
+ should "return correct image URL for day #{i}" do
48
+ forecast[i].image_url.should == "http://www.nws.noaa.gov/weather/images/fcicons/ra#{probability}.jpg"
49
+ end
50
+ end
51
+
52
+ [5, 94, 22, 50].each_with_index do |probability, i|
53
+ should "return correct daytime probability of precipitation for day #{i}" do
54
+ forecast[i].daytime_precipitation_probability.should == probability
55
+ end
56
+ end
57
+
58
+ [77, 84, 19, 50].each_with_index do |probability, i|
59
+ should "return correct evening probability of precipitation for day #{i}" do
60
+ forecast[i].evening_precipitation_probability.should == probability
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def forecast
67
+ @forecast ||= NOAA::Forecast.from_xml(XML_DOC)
68
+ end
69
+ end
@@ -0,0 +1,15 @@
1
+ begin
2
+ require 'context'
3
+ require 'matchy'
4
+ rescue LoadError => e
5
+ if require 'rubygems' then retry
6
+ else raise(e)
7
+ end
8
+ end
9
+
10
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'noaa')
11
+
12
+ module NOAA
13
+ class TestCase < ::Test::Unit::TestCase
14
+ end
15
+ end
@@ -0,0 +1,57 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class TestHttpService < NOAA::TestCase
4
+ before :each do
5
+ HTTP.reset
6
+ end
7
+
8
+ test 'should send properly-formed URL for current conditions' do
9
+ http_service.get_current_conditions('KNYC')
10
+ HTTP.requests.should == [URI.parse('http://www.weather.gov/xml/current_obs/KNYC.xml')]
11
+ end
12
+
13
+ test 'should return XML document for current conditions' do
14
+ http_service.get_current_conditions('KNYC').to_s.should == %Q{<?xml version="1.0" encoding="UTF-8"?>\n<test/>\n}
15
+ end
16
+
17
+ test 'should send properly-formed URL for forecast' do
18
+ http_service.get_forecast(4, 40.72, -73.99)
19
+ HTTP.requests.should == [URI.parse('http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?lat=40.72&lon=-73.99&format=24+hourly&numDays=4')]
20
+ end
21
+
22
+ test 'should return XML document for forecast' do
23
+ http_service.get_forecast(4, 40.72, -73.99).to_s.should == %Q{<?xml version="1.0" encoding="UTF-8"?>\n<test/>\n}
24
+ end
25
+
26
+ test 'should send properly-formed URL for station list' do
27
+ http_service.get_station_list
28
+ HTTP.requests.should == [URI.parse('http://www.weather.gov/xml/current_obs/index.xml')]
29
+ end
30
+
31
+ test 'should return XML document for station list' do
32
+ http_service.get_station_list.to_s.should == %Q{<?xml version="1.0" encoding="UTF-8"?>\n<test/>\n}
33
+ end
34
+
35
+ private
36
+
37
+ def http_service
38
+ NOAA::HttpService.new(HTTP)
39
+ end
40
+
41
+ module HTTP
42
+ class <<self
43
+ def reset
44
+ requests.clear
45
+ end
46
+
47
+ def requests
48
+ @requests ||= []
49
+ end
50
+
51
+ def get(uri)
52
+ requests << uri
53
+ "<test/>"
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,65 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class TestStation < NOAA::TestCase
4
+ before :all do
5
+ NOAA::Station.stations_file = File.join(File.dirname(__FILE__), 'data', 'stations.yml')
6
+ end
7
+
8
+ after :all do
9
+ NOAA::Station.stations_file = nil
10
+ end
11
+
12
+ test 'should load station by id' do
13
+ NOAA::Station.find('KNYC').id.should == 'KNYC'
14
+ end
15
+
16
+ test 'should find closest to coordinates' do
17
+ NOAA::Station.closest_to(GeoKit::LatLng.new(40.8, -73.96)).id.should == 'KNYC'
18
+ end
19
+
20
+ test 'should find closest to lat/lng' do
21
+ NOAA::Station.closest_to(40.8, -73.96).id.should == 'KNYC'
22
+ end
23
+
24
+ test 'should find closest to lat/lng passed as array' do
25
+ NOAA::Station.closest_to([40.8, -73.96]).id.should == 'KNYC'
26
+ end
27
+
28
+ test 'should throw ArgumentError if bad argument passed into #closest_to()' do
29
+ lambda { NOAA::Station.closest_to('hey') }.should raise_error(ArgumentError)
30
+ end
31
+
32
+ test 'should throw ArgumentError if more than two arguments passed into #closest_to()' do
33
+ lambda { NOAA::Station.closest_to(1, 2, 3) }.should raise_error(ArgumentError)
34
+ end
35
+
36
+ test 'should return name' do
37
+ station.name.should == 'New York City, Central Park'
38
+ end
39
+
40
+ test 'should return state' do
41
+ station.state.should == 'NY'
42
+ end
43
+
44
+ test 'should return XML URL' do
45
+ station.xml_url.should == 'http://weather.gov/xml/current_obs/KNYC.xml'
46
+ end
47
+
48
+ test 'should return latitude' do
49
+ station.latitude.should == 40.783
50
+ end
51
+
52
+ test 'should return longitude' do
53
+ station.longitude.should == -73.967
54
+ end
55
+
56
+ test 'should return coordinates' do
57
+ station.coordinates.should == GeoKit::LatLng.new(40.783, -73.967)
58
+ end
59
+
60
+ private
61
+
62
+ def station
63
+ NOAA::Station.find('KNYC')
64
+ end
65
+ end
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class TestStationWriter < NOAA::TestCase
4
+ XML_DOC = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'data', 'stations-abridged.xml'))
5
+
6
+ [40.66, 40.77, 40.783].each_with_index do |latitude, i|
7
+ test "should write latitude for station #{i}" do
8
+ yaml[i]['latitude'].should == latitude
9
+ end
10
+ end
11
+
12
+ [-73.78, -73.9, -73.967].each_with_index do |longitude, i|
13
+ test "should write longitude for station #{i}" do
14
+ yaml[i]['longitude'].should == longitude
15
+ end
16
+ end
17
+
18
+ ['KJFK', 'KLGA', 'KNYC'].each_with_index do |id, i|
19
+ test "should write id for station #{i}" do
20
+ yaml[i]['id'].should == id
21
+ end
22
+ end
23
+
24
+ ['New York/John F. Kennedy Intl Airport', 'New York, La Guardia Airport', 'New York City, Central Park'].each_with_index do |name, i|
25
+ test "should write name for station #{i}" do
26
+ yaml[i]['name'].should == name
27
+ end
28
+ end
29
+
30
+ (%w(NY) * 3).each_with_index do |state, i|
31
+ test "should write state for station #{i}" do
32
+ yaml[i]['state'].should == state
33
+ end
34
+ end
35
+
36
+ 3.times do |i|
37
+ test "should write XML URL for station #{i}" do
38
+ yaml[i]['xml_url'].should == "http://weather.gov/xml/current_obs/#{yaml[i]['id']}.xml"
39
+ end
40
+ end
41
+
42
+ def yaml
43
+ @yaml ||= begin
44
+ io = StringIO.new
45
+ NOAA::StationWriter.new(XML_DOC).write(io)
46
+ YAML.load(io.string)
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: noaa
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 4
10
+ version: 0.2.4
11
+ platform: ruby
12
+ authors:
13
+ - Mat Brown
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-07 00:00:00 -04:00
19
+ default_executable: noaa-update-stations
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: nokogiri
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 53
30
+ segments:
31
+ - 0
32
+ - 9
33
+ - 7
34
+ version: 0.9.7
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: geokit
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 1
48
+ - 5
49
+ - 0
50
+ version: 1.5.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: mcmire-context
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 63
62
+ segments:
63
+ - 0
64
+ - 0
65
+ - 16
66
+ version: 0.0.16
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: jnunemaker-matchy
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 15
78
+ segments:
79
+ - 0
80
+ - 4
81
+ - 0
82
+ version: 0.4.0
83
+ type: :development
84
+ version_requirements: *id004
85
+ description: Ruby API for National Oceanic and Atmospheric Administration weather data
86
+ email: mat@patch.com
87
+ executables:
88
+ - noaa-update-stations
89
+ extensions: []
90
+
91
+ extra_rdoc_files: []
92
+
93
+ files:
94
+ - bin/noaa-update-stations
95
+ - lib/noaa/station_writer.rb
96
+ - lib/noaa/forecast_day.rb
97
+ - lib/noaa/forecast.rb
98
+ - lib/noaa/current_conditions.rb
99
+ - lib/noaa/http_service.rb
100
+ - lib/noaa/version.rb
101
+ - lib/noaa/station.rb
102
+ - lib/noaa.rb
103
+ - test/data/4-day.xml
104
+ - test/data/stations.yml
105
+ - test/data/stations-abridged.xml
106
+ - test/data/KVAY.xml
107
+ - test/data/stations.xml
108
+ - test/test_http_service.rb
109
+ - test/test_forecast.rb
110
+ - test/test_current_conditions.rb
111
+ - test/test_helper.rb
112
+ - test/test_station_writer.rb
113
+ - test/test_station.rb
114
+ - data/stations.yml
115
+ - CHANGELOG
116
+ - README
117
+ has_rdoc: true
118
+ homepage: http://github.com/outoftime/noaa
119
+ licenses: []
120
+
121
+ post_install_message: |-
122
+ Be sure to update the weather station list:
123
+
124
+ sudo noaa-update-stations
125
+
126
+ This can be run at any time to update the stations, but you must run it once to initially populate the station list.
127
+ rdoc_options:
128
+ - --line-numbers
129
+ - --inline-source
130
+ - --title
131
+ - Noaa
132
+ - --main
133
+ - README
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 3
142
+ segments:
143
+ - 0
144
+ version: "0"
145
+ required_rubygems_version: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ hash: 11
151
+ segments:
152
+ - 1
153
+ - 2
154
+ version: "1.2"
155
+ requirements: []
156
+
157
+ rubyforge_project: noaa
158
+ rubygems_version: 1.3.7
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Ruby API for National Oceanic and Atmospheric Administration weather data
162
+ test_files:
163
+ - test/test_station_writer.rb
164
+ - test/test_forecast.rb
165
+ - test/test_helper.rb
166
+ - test/test_station.rb
167
+ - test/test_http_service.rb
168
+ - test/test_current_conditions.rb