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

#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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ullr/forecast.rb', line 17

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'}
    if forecast
      time_layout = forecast.time_layouts.find{|t| t.layout_key =~ /k-p12h/ }
      time_layout.start_valid_times.each_with_index do |time,i|
        params = forecast.parameters
        word = params.worded_forecast.texts[i]
        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 = word.high_temperature
        point.low_temperature = word.low_temperature
        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