Class: Ullr::Forecast
- Inherits:
-
Object
- Object
- Ullr::Forecast
- Defined in:
- lib/ullr/forecast.rb
Constant Summary collapse
- NOAA_ENDPOINT =
"http://forecast.weather.gov/MapClick.php?lat={{lat}}&lon={{lon}}&FcstType=dwml"
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#forecast_string ⇒ Object
Returns the value of attribute forecast_string.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lon ⇒ Object
Returns the value of attribute lon.
-
#noaa_endpoint ⇒ Object
Returns the value of attribute noaa_endpoint.
-
#parsed_forecast ⇒ Object
Returns the value of attribute parsed_forecast.
-
#socket_error ⇒ Object
Returns the value of attribute socket_error.
Instance Method Summary collapse
- #get_noaa_forecast ⇒ Object
-
#initialize(options = {}) ⇒ Forecast
constructor
A new instance of Forecast.
- #parse_temps ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Forecast
Returns a new instance of Forecast.
8 9 10 11 12 13 14 15 |
# File 'lib/ullr/forecast.rb', line 8 def initialize(={}) [:lat,:lon].each do |o| raise(ArgumentError, "#{o.to_s} is required!") if ![o] || ![o].instance_of?(Float) end @lat = [:lat] @lon = [:lon] @noaa_endpoint = NOAA_ENDPOINT.gsub("{{lat}}",@lat.to_s).gsub("{{lon}}",@lon.to_s) end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
5 6 7 |
# File 'lib/ullr/forecast.rb', line 5 def data @data end |
#forecast_string ⇒ Object
Returns the value of attribute forecast_string.
5 6 7 |
# File 'lib/ullr/forecast.rb', line 5 def forecast_string @forecast_string end |
#lat ⇒ Object
Returns the value of attribute lat.
5 6 7 |
# File 'lib/ullr/forecast.rb', line 5 def lat @lat end |
#lon ⇒ Object
Returns the value of attribute lon.
5 6 7 |
# File 'lib/ullr/forecast.rb', line 5 def lon @lon end |
#noaa_endpoint ⇒ Object
Returns the value of attribute noaa_endpoint.
5 6 7 |
# File 'lib/ullr/forecast.rb', line 5 def noaa_endpoint @noaa_endpoint end |
#parsed_forecast ⇒ Object
Returns the value of attribute parsed_forecast.
5 6 7 |
# File 'lib/ullr/forecast.rb', line 5 def parsed_forecast @parsed_forecast end |
#socket_error ⇒ Object
Returns the value of attribute socket_error.
5 6 7 |
# File 'lib/ullr/forecast.rb', line 5 def socket_error @socket_error end |
Instance Method Details
#get_noaa_forecast ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ullr/forecast.rb', line 33 def get_noaa_forecast @socket_error = false @data = [] begin resp = open(@noaa_endpoint) @forecast_string = resp.read rescue SocketError @socket_error = true end if !@socket_error data = Ullr::NOAA::Data.parse(@forecast_string) forecast = data.find{|o| o.type == 'forecast'} @parsed_forecast = forecast if forecast time_layout = forecast.time_layouts.find{|t| t.layout_key =~ /k-p12h/ } temperature_data = parse_temps time_layout.start_valid_times.each_with_index do |time,i| params = forecast.parameters word = params.worded_forecast.texts[i] min_temp = temperature_data ? temperature_data[:minimum].find{|t| t.period_name =~ /#{time.period_name}/} : false max_temp = temperature_data ? temperature_data[:maximum].find{|t| t.period_name =~ /#{time.period_name}/} : false point = OpenStruct.new point.start_time = time.period_start point.name = time.period_name point.pop = params.pops.values[i].value point.text = word.value point.snow = word.has_snow? point.high_temperature = max_temp ? max_temp.temperature : nil point.low_temperature = min_temp ? min_temp.temperature : nil point.wind_direction = word.wind_direction point.wind_speeds = word.wind_speeds point.snow_estimate = word.snow_estimate @data << point end end end return @data end |
#parse_temps ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ullr/forecast.rb', line 17 def parse_temps temps = {:minimum => [], :maximum => []} @parsed_forecast.parameters.temperatures.each do |temp_data| time_layout = @parsed_forecast.time_layouts.find{|t| t.layout_key =~ /#{temp_data.time_layout}/ } temp_data.values.each_with_index do |temperature, i| data = OpenStruct.new time = time_layout.start_valid_times[i] data.temperature = temperature.value data.start_time = time.period_start data.period_name = time.period_name temps[temp_data.type.to_sym] << data end end return temps end |