Class: Ullr::Forecast

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options={})
  [:lat,:lon].each do |o|
   raise(ArgumentError, "#{o.to_s} is required!") if !options[o] || !options[o].instance_of?(Float)
  end
  @lat = options[:lat]
  @lon = options[:lon]
  @noaa_endpoint = NOAA_ENDPOINT.gsub("{{lat}}",@lat.to_s).gsub("{{lon}}",@lon.to_s)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/ullr/forecast.rb', line 5

def data
  @data
end

#forecast_stringObject

Returns the value of attribute forecast_string.



5
6
7
# File 'lib/ullr/forecast.rb', line 5

def forecast_string
  @forecast_string
end

#latObject

Returns the value of attribute lat.



5
6
7
# File 'lib/ullr/forecast.rb', line 5

def lat
  @lat
end

#lonObject

Returns the value of attribute lon.



5
6
7
# File 'lib/ullr/forecast.rb', line 5

def lon
  @lon
end

#noaa_endpointObject

Returns the value of attribute noaa_endpoint.



5
6
7
# File 'lib/ullr/forecast.rb', line 5

def noaa_endpoint
  @noaa_endpoint
end

#parsed_forecastObject

Returns the value of attribute parsed_forecast.



5
6
7
# File 'lib/ullr/forecast.rb', line 5

def parsed_forecast
  @parsed_forecast
end

#socket_errorObject

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_forecastObject



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_tempsObject



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