roker 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/roker ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + '/lib/roker'
3
+ require 'ruby-debug'
4
+
5
+ started_at = Time.now.beginning_of_day + 1.day
6
+ ended_at = started_at.end_of_day
7
+ lat = 37.0625
8
+ lng = -95.677068
9
+
10
+ ARGV.each do |a|
11
+ # puts "Argument: #{a}"
12
+ end
13
+ roker = Roker.new(:started_at => started_at, :ended_at => ended_at, :lat => lat, :lng => lng)
14
+
15
+ weather_forecasts_attributes = roker.weather_forecasts_attributes
16
+
17
+ forecast = "Roker says the forecast for #{started_at.strftime("%B %d, %Y")} is:\n"
18
+ weather_forecasts_attributes.each do |wfa|
19
+ # wfa => {
20
+ # :started_at=>Sat Dec 05 00:17:32 -0500 2009,
21
+ # :wind_speed=>7.0,
22
+ # :minimum_temperature=>21.0,
23
+ # :ended_at=>Sat Dec 05 03:17:32 -0500 2009,
24
+ # :wind_direction=>180.0,
25
+ # :temperature=>24.0,
26
+ # :cloud_cover=>0.0,
27
+ # :dewpoint_temperature=>12.0,
28
+ # :lat=>37.0625,
29
+ # :relative_humidity=>60.0,
30
+ # :liquid_precipitation=>0.0,
31
+ # :lng=>-95.677068,
32
+ # :maximum_temperature=>nil,
33
+ # :wave_height=>0.0,
34
+ # :probability_of_precipitation=>0.0
35
+ # }
36
+ temp = wfa[:temperature].nan? ? '?' : wfa[:temperature].to_i.to_s
37
+ pop = wfa[:probability_of_precipitation].nan? ? '?' : wfa[:probability_of_precipitation].to_i.to_s
38
+ forecast << wfa[:started_at].strftime("%I:%M%p") + "\t" + temp + " deg" + "\t" + pop + "% P.O.P."
39
+ forecast << "\n"
40
+ end
41
+
42
+ puts forecast
data/roker.gemspec ADDED
@@ -0,0 +1,75 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{roker}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Greg Sterndale"]
12
+ s.date = %q{2009-12-05}
13
+ s.description = %q{Weather forecasts from weather.gov}
14
+ s.email = %q{gsterndale@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/numeric.rb",
27
+ "lib/roker.rb",
28
+ "lib/time.rb",
29
+ "lib/time_layout.rb",
30
+ "lib/time_span.rb",
31
+ "lib/weather_parameter.rb",
32
+ "roker",
33
+ "roker.gemspec",
34
+ "test/helper.rb",
35
+ "test/test_roker.rb",
36
+ "test/test_time.rb",
37
+ "test/test_time_layout.rb",
38
+ "test/test_time_span.rb",
39
+ "test/test_weather_parameter.rb",
40
+ "test/weather_xml.xml"
41
+ ]
42
+ s.homepage = %q{http://github.com/gsterndale/roker}
43
+ s.rdoc_options = ["--charset=UTF-8"]
44
+ s.require_paths = ["lib"]
45
+ s.rubygems_version = %q{1.3.5}
46
+ s.summary = %q{Weather forecasts from weather.gov}
47
+ s.test_files = [
48
+ "test/helper.rb",
49
+ "test/test_roker.rb",
50
+ "test/test_time.rb",
51
+ "test/test_time_layout.rb",
52
+ "test/test_time_span.rb",
53
+ "test/test_weather_parameter.rb"
54
+ ]
55
+
56
+ if s.respond_to? :specification_version then
57
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
62
+ s.add_development_dependency(%q<mocha>, [">= 0.9.1"])
63
+ s.add_development_dependency(%q<hpricot>, [">= 0.6.164"])
64
+ else
65
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
66
+ s.add_dependency(%q<mocha>, [">= 0.9.1"])
67
+ s.add_dependency(%q<hpricot>, [">= 0.6.164"])
68
+ end
69
+ else
70
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
71
+ s.add_dependency(%q<mocha>, [">= 0.9.1"])
72
+ s.add_dependency(%q<hpricot>, [">= 0.6.164"])
73
+ end
74
+ end
75
+
data/test/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'ruby-debug'
4
+ require 'shoulda'
5
+ require 'mocha'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ require 'roker'
10
+
11
+ class Test::Unit::TestCase
12
+
13
+ def stub_weather_service
14
+ Roker.any_instance.stubs(:weather_xml).returns(File.open(File.join(File.dirname(__FILE__), 'weather_xml.xml'), "r").read)
15
+ end
16
+
17
+ end
@@ -0,0 +1,94 @@
1
+ require 'helper'
2
+
3
+ class TestRoker < Test::Unit::TestCase
4
+ def setup
5
+ super
6
+ stub_weather_service
7
+ end
8
+
9
+ context "roker attributes" do
10
+ setup do
11
+ @num_days = 2
12
+ @started_at = Time.mktime(2008, 11, 24, 4, 0, 0) # Time.parse("2008-11-24T04:00:00-05:00") #Time.now+3.days
13
+ @ended_at = @started_at+@num_days.days
14
+ @attributes = {:lat => 42, :lng => -81, :started_at => @started_at, :ended_at => @ended_at}
15
+ end
16
+
17
+ context "a roker" do
18
+ setup { assert @roker = Roker.new(@attributes)}
19
+
20
+ should "respond to lat" do
21
+ assert @roker.respond_to?(:lat)
22
+ end
23
+ should "respond to lng" do
24
+ assert @roker.respond_to?(:lng)
25
+ end
26
+ should "respond to started_at" do
27
+ assert @roker.respond_to?(:started_at)
28
+ assert_equal @started_at, @roker.started_at
29
+ end
30
+ should "respond to ended_at" do
31
+ assert @roker.respond_to?(:ended_at)
32
+ assert_equal @ended_at, @roker.ended_at
33
+ end
34
+
35
+ context "weather_forecasts_attributes" do
36
+ setup { assert @weather_forecasts_attributes = @roker.weather_forecasts_attributes }
37
+
38
+ should "weather_forecasts_attributes be array" do
39
+ assert @weather_forecasts_attributes.is_a?(Array)
40
+ end
41
+
42
+ should "attributes have at least one item" do
43
+ assert !@weather_forecasts_attributes.empty?
44
+ end
45
+
46
+ context "first weather forecast attributes" do
47
+ setup { assert @weather_forecast_attributes = @weather_forecasts_attributes.first }
48
+ should "have lat, lng, started_at, ended_at" do
49
+ assert_not_nil @weather_forecast_attributes[:lat]
50
+ assert_not_nil @weather_forecast_attributes[:lng]
51
+ assert_not_nil @weather_forecast_attributes[:started_at]
52
+ assert_not_nil @weather_forecast_attributes[:ended_at]
53
+ end
54
+ should "have weather attributes" do
55
+ # assert_not_nil @weather_forecast_attributes[:maximum_temperature]
56
+ assert_not_nil @weather_forecast_attributes[:minimum_temperature]
57
+ assert_not_nil @weather_forecast_attributes[:temperature]
58
+ assert_not_nil @weather_forecast_attributes[:dewpoint_temperature]
59
+ assert_not_nil @weather_forecast_attributes[:liquid_precipitation]
60
+ assert_not_nil @weather_forecast_attributes[:probability_of_precipitation]
61
+ assert_not_nil @weather_forecast_attributes[:wind_speed]
62
+ assert_not_nil @weather_forecast_attributes[:wind_direction]
63
+ assert_not_nil @weather_forecast_attributes[:cloud_cover]
64
+ assert_not_nil @weather_forecast_attributes[:relative_humidity]
65
+ assert_not_nil @weather_forecast_attributes[:wave_height]
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+ should "have weather xml" do
72
+ assert_not_nil @roker.weather_xml # stubbed
73
+ end
74
+
75
+ should "have weather doc" do
76
+ assert_not_nil @roker.weather_doc
77
+ end
78
+
79
+ should "parse parameters" do
80
+ assert_not_nil @roker.parse_parameters
81
+ end
82
+
83
+ should "parse parameters as hash" do
84
+ assert @roker.parse_parameters.is_a?(Hash)
85
+ end
86
+
87
+ should "parameters not be empty?" do
88
+ assert !@roker.parse_parameters.empty?
89
+ end
90
+
91
+ end
92
+ end
93
+
94
+ end
data/test/test_time.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'helper'
2
+
3
+ class TestTime < Test::Unit::TestCase
4
+
5
+ context "a date time" do
6
+ setup do
7
+ @time = Time.mktime(1979, 3, 29, 14, 32, 40)
8
+ @bom = Time.mktime(1979, 3, 29, 14, 32, 0)
9
+ @eom = Time.mktime(1979, 3, 29, 14, 32, 59)
10
+ @boh = Time.mktime(1979, 3, 29, 14, 0, 0)
11
+ @eoh = Time.mktime(1979, 3, 29, 14, 59, 59)
12
+ end
13
+ should "beginning_of_minute be beginning of minute" do
14
+ assert_equal @bom, @time.beginning_of_minute
15
+ end
16
+ should "end_of_minute be end of minute" do
17
+ assert_equal @eom, @time.end_of_minute
18
+ end
19
+ should "beginning_of_hour be beginning of hour" do
20
+ assert_equal @boh, @time.beginning_of_hour
21
+ end
22
+ should "end_of_hour be end of hour" do
23
+ assert_equal @eoh, @time.end_of_hour
24
+ end
25
+
26
+ should "beginning_of(1.minute) equal beginning_of_minute" do
27
+ assert_equal @time.beginning_of_minute, @time.beginning_of(1.minute)
28
+ end
29
+ should "beginning_of(1.hour) equal beginning_of_hour" do
30
+ assert_equal @time.beginning_of_hour, @time.beginning_of(1.hour)
31
+ end
32
+ should "beginning_of(1.day) equal beginning_of_day" do
33
+ assert_equal @time.beginning_of_day, @time.beginning_of(1.day)
34
+ end
35
+ should "beginning_of(1.week) equal beginning_of_week" do
36
+ assert_equal @time.beginning_of_week, @time.beginning_of(1.week)
37
+ end
38
+ should "beginning_of(1.month) equal beginning_of_month" do
39
+ assert_equal @time.beginning_of_month, @time.beginning_of(1.month)
40
+ end
41
+ should "beginning_of(1.year) equal beginning_of_year" do
42
+ assert_equal @time.beginning_of_year, @time.beginning_of(1.year)
43
+ end
44
+ should "end_of(1.minute) equal end_of_minute" do
45
+ assert_equal @time.end_of_minute, @time.end_of(1.minute)
46
+ end
47
+ should "end_of(1.hour) equal end_of_hour" do
48
+ assert_equal @time.end_of_hour, @time.end_of(1.hour)
49
+ end
50
+ should "end_of(1.day) equal end_of_day" do
51
+ assert_equal @time.end_of_day, @time.end_of(1.day)
52
+ end
53
+ should "end_of(1.week) equal end_of_week" do
54
+ assert_equal @time.end_of_week, @time.end_of(1.week)
55
+ end
56
+ should "end_of(1.month) equal end_of_month" do
57
+ assert_equal @time.end_of_month, @time.end_of(1.month)
58
+ end
59
+ should "end_of(1.year) equal end_of_year" do
60
+ assert_equal @time.end_of_year, @time.end_of(1.year)
61
+ end
62
+
63
+ context "a time 1 day later and a one hour interval" do
64
+ setup do
65
+ @time2 = @time + 1.day
66
+ @interval = 1.hour
67
+ end
68
+ should "yield block 25 times on upto" do
69
+ @time.expects(:foo).times(25).returns(1)
70
+ @time.upto(@time2, @interval) do
71
+ @time.foo
72
+ end
73
+ end
74
+ should "yield block 24 times on upto not inclusive" do
75
+ @time.expects(:foo).times(24).returns(1)
76
+ @time.upto(@time2, @interval, false) do
77
+ @time.foo
78
+ end
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,155 @@
1
+ require 'helper'
2
+
3
+ class TestTimeLayout < Test::Unit::TestCase
4
+ context "a new TimeLayout passed a time spans" do
5
+ setup do
6
+ @interval = 3.hours
7
+ @start_at = Time.now+1.hour
8
+ @length = 4
9
+ @time_spans = []
10
+ (0..(@length-1)).each do |i|
11
+ @time_spans << TimeSpan.new(:start_at => @start_at+i*@interval, :duration => @interval)
12
+ end
13
+ @time_layout = TimeLayout.new(:time_spans => @time_spans)
14
+ end
15
+
16
+ should "have time-spans" do
17
+ assert !@time_layout.time_spans.empty?
18
+ end
19
+ should "have a start_at equal to first time_span start_at" do
20
+ assert_equal @time_spans.first.start_at, @time_layout.start_at
21
+ end
22
+ should "have a end_at equal to last time_span end_at" do
23
+ assert_equal @time_spans.last.end_at, @time_layout.end_at
24
+ end
25
+ should "have a duration" do
26
+ assert_equal @length * @interval, @time_layout.duration
27
+ end
28
+ should "have an interval" do
29
+ assert_equal @interval, @time_layout.interval
30
+ end
31
+ should "have a length" do
32
+ assert_equal @length, @time_layout.length
33
+ end
34
+
35
+ should "find second time span when using bracket access with 1 as index" do
36
+ assert_equal @time_spans[1], @time_layout[1]
37
+ end
38
+
39
+ should "find index_at of 2 for specified time enveloped by third time span" do
40
+ assert_equal 2, @time_layout.index_at(@time_spans[2].start_at+@interval/2)
41
+ end
42
+
43
+ should "find index_at of 2 for specified time when second time span ends and third time span starts" do
44
+ assert_equal @time_spans[2].start_at, @time_spans[1].end_at
45
+ assert_equal 2, @time_layout.index_at(@time_spans[2].start_at)
46
+ end
47
+
48
+ should "find index_at of nil for specified time outside time layout" do
49
+ assert_nil @time_layout.index_at(@start_at - 1.hour)
50
+ end
51
+
52
+ should "have indices_enveloping of [1,2], [0.5, 1.0, 0.5] for time span overlapping second, third and fourth @time_spans" do
53
+ assert_equal [[1,2,3], [0.5, 1.0, 0.5]], @time_layout.indices_enveloping(TimeSpan.new(:start_at => @time_spans[1].start_at+@interval/2, :end_at => @time_spans[3].end_at-@interval/2))
54
+ end
55
+ end
56
+
57
+ context "a new TimeLayout passed start_at and end_at arrays" do
58
+ setup do
59
+ @interval = 3.hours
60
+ @start_at = Time.now+1.hour
61
+ @length = 4
62
+ @start_ats = []
63
+ @end_ats = []
64
+ (0..(@length-1)).each do |i|
65
+ @start_ats << @start_at+i*@interval
66
+ @end_ats << @start_at+(i+1)*@interval
67
+ end
68
+ @time_layout = TimeLayout.new(:start_at => @start_ats, :end_at => @end_ats)
69
+ end
70
+
71
+ should "have time_spans" do
72
+ assert !@time_layout.time_spans.empty?
73
+ end
74
+ should "have a start_at equal to first start_at" do
75
+ assert_equal @start_ats.first, @time_layout.start_at
76
+ end
77
+ should "have a end_at equal to last end_at" do
78
+ assert_equal @end_ats.last, @time_layout.end_at
79
+ end
80
+ should "have a duration" do
81
+ assert_equal @length * @interval, @time_layout.duration
82
+ end
83
+ should "have an interval" do
84
+ assert_equal @interval, @time_layout.interval
85
+ end
86
+ should "have a length" do
87
+ assert_equal @length, @time_layout.length
88
+ end
89
+ end
90
+
91
+
92
+ context "a new TimeLayout passed start_at array only" do
93
+ setup do
94
+ @interval = 2.days
95
+ @start_at = Time.now+1.hour
96
+ @length = 4
97
+ @start_ats = []
98
+ (0..(@length-1)).each do |i|
99
+ @start_ats << @start_at+i*@interval
100
+ end
101
+ @time_layout = TimeLayout.new(:start_at => @start_ats)
102
+ end
103
+
104
+ should "have interval of difference between start_ats" do
105
+ assert_equal @start_ats[1] - @start_ats[0], @time_layout.interval
106
+ end
107
+ should "have time_spans" do
108
+ assert !@time_layout.time_spans.empty?
109
+ end
110
+ should "have a start_at equal to first start_at" do
111
+ assert_equal @start_ats.first, @time_layout.start_at
112
+ end
113
+ should "have a end_at equal to last end_at" do
114
+ assert_equal @start_ats.last+@interval, @time_layout.end_at
115
+ end
116
+ should "have a duration" do
117
+ assert_equal @length * @interval, @time_layout.duration
118
+ end
119
+ should "have a length" do
120
+ assert_equal @length, @time_layout.length
121
+ end
122
+ end
123
+
124
+ context "a new TimeLayout passed start_at array with one element only" do
125
+ setup do
126
+ @interval = 2.days
127
+ @start_at = Time.now+1.hour
128
+ @length = 1
129
+ @start_ats = []
130
+ (0..(@length-1)).each do |i|
131
+ @start_ats << @start_at+i*@interval
132
+ end
133
+ @time_layout = TimeLayout.new(:start_at => @start_ats)
134
+ end
135
+
136
+ should "have default interval" do
137
+ assert_equal TimeLayout::DEFAULT_INTERVAL, @time_layout.interval
138
+ end
139
+ should "have time_spans" do
140
+ assert !@time_layout.time_spans.empty?
141
+ end
142
+ should "have a start_at equal to first start_at" do
143
+ assert_equal @start_ats.first, @time_layout.start_at
144
+ end
145
+ should "have a end_at equal to last end_at" do
146
+ assert_equal @start_ats.last+TimeLayout::DEFAULT_INTERVAL, @time_layout.end_at
147
+ end
148
+ should "have a duration" do
149
+ assert_equal @length * TimeLayout::DEFAULT_INTERVAL, @time_layout.duration
150
+ end
151
+ should "have a length" do
152
+ assert_equal @length, @time_layout.length
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,91 @@
1
+ require 'helper'
2
+
3
+ class TestTimeSpan < Test::Unit::TestCase
4
+ context "a new TimeSpan passed a start_at and end_at" do
5
+ setup do
6
+ @start_at = Time.now+1.hour
7
+ @duration = 3.hours
8
+ @end_at = @start_at+@duration
9
+ @time_span = TimeSpan.new(:start_at => @start_at, :end_at => @end_at)
10
+ end
11
+ should "have a start_at" do
12
+ assert_equal @start_at, @time_span.start_at
13
+ end
14
+ should "have a end_at" do
15
+ assert_equal @end_at, @time_span.end_at
16
+ end
17
+ should "have a duration" do
18
+ assert_equal @end_at - @start_at, @time_span.duration
19
+ end
20
+
21
+ should "envelopes? @start_at" do
22
+ assert @time_span.envelopes?(@start_at)
23
+ end
24
+
25
+ should "envelopes? @end_at" do
26
+ assert @time_span.envelopes?(@end_at)
27
+ end
28
+
29
+ should "envelopes? @end_at when include_end param specified as true" do
30
+ assert @time_span.envelopes?(@end_at, true)
31
+ end
32
+
33
+ should "not envelopes? @end_at when include_end param specified as false" do
34
+ assert !@time_span.envelopes?(@end_at, false)
35
+ end
36
+
37
+ should "not envelopes? prior time" do
38
+ assert !@time_span.envelopes?(@start_at - 1.hour)
39
+ end
40
+
41
+ should "not envelopes? later time" do
42
+ assert !@time_span.envelopes?(@end_at + 1.hour)
43
+ end
44
+
45
+ should "overlaps? another TimeSpan that happens during @time_span" do
46
+ assert @time_span.overlaps?(TimeSpan.new(:start_at => @start_at+1, :end_at => @end_at-1))
47
+ end
48
+
49
+ should "overlaps? another TimeSpan that starts during @time_span, but ends after" do
50
+ assert @time_span.overlaps?(TimeSpan.new(:start_at => @start_at+1, :end_at => @end_at+1))
51
+ end
52
+
53
+ should "overlaps? another TimeSpan that starts before @time_span and ends during" do
54
+ assert @time_span.overlaps?(TimeSpan.new(:start_at => @start_at-1, :end_at => @end_at-1))
55
+ end
56
+
57
+ should "overlaps? another TimeSpan that starts before @time_span and ends after" do
58
+ assert @time_span.overlaps?(TimeSpan.new(:start_at => @start_at-1, :end_at => @end_at+1))
59
+ end
60
+
61
+ should "overlaps? another TimeSpan that abutts @time_span" do
62
+ assert @time_span.overlaps?(TimeSpan.new(:start_at => @end_at, :end_at => @end_at+10))
63
+ end
64
+
65
+ should "not overlaps? another TimeSpan that abutts @time_span afterward when include_end param specified as false" do
66
+ assert !@time_span.overlaps?(TimeSpan.new(:start_at => @end_at, :end_at => @end_at+10), false)
67
+ end
68
+
69
+ should "not overlaps? another TimeSpan that abutts @time_span beforehand when include_end param specified as false" do
70
+ assert !@time_span.overlaps?(TimeSpan.new(:start_at => @start_at-10, :end_at => @start_at), false)
71
+ end
72
+
73
+ should "not overlaps? another TimeSpan that happens before @time_span" do
74
+ assert !@time_span.overlaps?(TimeSpan.new(:start_at => @start_at-10, :end_at => @start_at-5))
75
+ end
76
+
77
+ should "not overlaps? another TimeSpan that happens after @time_span" do
78
+ assert !@time_span.overlaps?(TimeSpan.new(:start_at => @end_at+10, :end_at => @end_at+50))
79
+ end
80
+
81
+ should "have overlap of @duration/2 with another TimeSpan that starts halfway through @time_span and ends after" do
82
+ assert_equal @duration/2, @time_span.overlap(TimeSpan.new(:start_at => @start_at+@duration/2, :end_at => @end_at+@duration*2))
83
+ end
84
+
85
+ should "have overlap_by of 0.5 with another TimeSpan that starts halfway through @time_span and ends after" do
86
+ assert_equal 0.5, @time_span.overlap_by(TimeSpan.new(:start_at => @start_at+@duration/2, :end_at => @end_at+@duration*2))
87
+ end
88
+
89
+ should_eventually "enveloped_by?"
90
+ end
91
+ end