ullr 0.1.2 → 0.1.3

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.
@@ -10,3 +10,7 @@
10
10
 
11
11
  * fix typo in readme
12
12
  * fix bug for daytime snow accumulation estimates
13
+
14
+ == 0.1.2 / 2011-12-28
15
+
16
+ * now uses proper min/max values from feed instead of regex matches in forecast text
@@ -2,7 +2,7 @@ module Ullr
2
2
  class Forecast
3
3
  require 'open-uri'
4
4
  require 'ostruct'
5
- attr_accessor :lat, :lon, :forecast_string, :noaa_endpoint, :data, :socket_error
5
+ attr_accessor :lat, :lon, :forecast_string, :noaa_endpoint, :data, :socket_error, :parsed_forecast
6
6
  NOAA_ENDPOINT = "http://forecast.weather.gov/MapClick.php?lat={{lat}}&lon={{lon}}&FcstType=dwml"
7
7
 
8
8
  def initialize(options={})
@@ -14,6 +14,22 @@ module Ullr
14
14
  @noaa_endpoint = NOAA_ENDPOINT.gsub("{{lat}}",@lat.to_s).gsub("{{lon}}",@lon.to_s)
15
15
  end
16
16
 
17
+ def parse_temps
18
+ temps = {:minimum => [], :maximum => []}
19
+ @parsed_forecast.parameters.temperatures.each do |temp_data|
20
+ time_layout = @parsed_forecast.time_layouts.find{|t| t.layout_key =~ /#{temp_data.time_layout}/ }
21
+ temp_data.values.each_with_index do |temperature, i|
22
+ data = OpenStruct.new
23
+ time = time_layout.start_valid_times[i]
24
+ data.temperature = temperature.value
25
+ data.start_time = time.period_start
26
+ data.period_name = time.period_name
27
+ temps[temp_data.type.to_sym] << data
28
+ end
29
+ end
30
+ return temps
31
+ end
32
+
17
33
  def get_noaa_forecast
18
34
  @socket_error = false
19
35
  @data = []
@@ -26,19 +42,23 @@ module Ullr
26
42
  if !@socket_error
27
43
  data = Ullr::NOAA::Data.parse(@forecast_string)
28
44
  forecast = data.find{|o| o.type == 'forecast'}
45
+ @parsed_forecast = forecast
29
46
  if forecast
30
47
  time_layout = forecast.time_layouts.find{|t| t.layout_key =~ /k-p12h/ }
48
+ temperature_data = parse_temps
31
49
  time_layout.start_valid_times.each_with_index do |time,i|
32
50
  params = forecast.parameters
33
51
  word = params.worded_forecast.texts[i]
52
+ min_temp = temperature_data ? temperature_data[:minimum].find{|t| t.period_name =~ /#{time.period_name}/} : false
53
+ max_temp = temperature_data ? temperature_data[:maximum].find{|t| t.period_name =~ /#{time.period_name}/} : false
34
54
  point = OpenStruct.new
35
55
  point.start_time = time.period_start
36
56
  point.name = time.period_name
37
57
  point.pop = params.pops.values[i].value
38
58
  point.text = word.value
39
59
  point.snow = word.has_snow?
40
- point.high_temperature = word.high_temperature
41
- point.low_temperature = word.low_temperature
60
+ point.high_temperature = max_temp ? max_temp.temperature : nil
61
+ point.low_temperature = min_temp ? min_temp.temperature : nil
42
62
  point.wind_direction = word.wind_direction
43
63
  point.wind_speeds = word.wind_speeds
44
64
  point.snow_estimate = word.snow_estimate
@@ -474,6 +474,61 @@ describe Ullr do
474
474
  @forecast.get_noaa_forecast
475
475
  @forecast.socket_error.should be_true
476
476
  end
477
+
478
+ it "should store parsed forecast" do
479
+ @forecast.should_receive(:open).with(@forecast.noaa_endpoint).and_return(@noaa_fixture)
480
+ @forecast.get_noaa_forecast
481
+ @forecast.should respond_to(:parsed_forecast)
482
+ @forecast.parsed_forecast.should be_an_instance_of(Ullr::NOAA::Data)
483
+ end
484
+
485
+ it "should call parse_temps" do
486
+ @forecast.should_receive(:open).with(@forecast.noaa_endpoint).and_return(@noaa_fixture)
487
+ @forecast.should_receive(:parse_temps)
488
+ @forecast.get_noaa_forecast
489
+ end
490
+
491
+ context 'parse_temps' do
492
+ before do
493
+ @forecast.should_receive(:open).with(@forecast.noaa_endpoint).and_return(@noaa_fixture)
494
+ @forecast.get_noaa_forecast
495
+ end
496
+
497
+ it "should respond to parse_temps" do
498
+ @forecast.should respond_to(:parse_temps)
499
+ end
500
+
501
+ it "should return a hash" do
502
+ @forecast.parse_temps.should be_an_instance_of(Hash)
503
+ end
504
+
505
+ context 'min max values' do
506
+ before do
507
+ @temps = @forecast.parse_temps
508
+ end
509
+ it "should have an array of min temps" do
510
+ @temps[:minimum].should be_an_instance_of(Array)
511
+ end
512
+
513
+ it "should have an array of max temps" do
514
+ @temps[:maximum].should be_an_instance_of(Array)
515
+ end
516
+
517
+ it "should decoreate an object with correct min temp data" do
518
+ @min = @temps[:minimum].first
519
+ @min.temperature.should eql('25')
520
+ @min.start_time.should eql(DateTime.parse('2011-12-18T18:00:00-08:00'))
521
+ @min.period_name.should eql('Tonight')
522
+ end
523
+
524
+ it "should decoreate an object with correct max temp data" do
525
+ @max = @temps[:maximum].first
526
+ @max.temperature.should eql('39')
527
+ @max.start_time.should eql(DateTime.parse('2011-12-18T12:00:00-08:00'))
528
+ @max.period_name.should eql('This Afternoon')
529
+ end
530
+ end
531
+ end
477
532
  end
478
533
 
479
534
  end
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ullr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-27 00:00:00.000000000Z
12
+ date: 2011-12-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: happymapper
16
- requirement: &70367029133460 !ruby/object:Gem::Requirement
16
+ requirement: &70173084964980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.4.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70367029133460
24
+ version_requirements: *70173084964980
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bones
27
- requirement: &70367029167180 !ruby/object:Gem::Requirement
27
+ requirement: &70173084963640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 3.7.3
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70367029167180
35
+ version_requirements: *70173084963640
36
36
  description: ullr is a little gem that consumes NOAA weather xml for a given lat/long,
37
37
  and returns an array of data with 12 hour forecasts along with some sugar to let
38
38
  you know if any sugar will be falling from the sky.