Class: Roker

Inherits:
Object
  • Object
show all
Defined in:
lib/roker.rb

Constant Summary collapse

WSDL_URL =
"http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl"
WSDL_PARAMETERS =
{ :maxt => true, :mint => true, :temp => true, :dew => true, :appt => false, :pop12 => true, :qpf => true, :snow => false, :sky => true, :rh => true, :wspd => true, :wdir => true, :wx => false, :icons => false, :waveh => true, :incw34 => false, :incw50 => false, :incw64 => false, :cumw34 => false, :cumw50 => false, :cumw64 => false, :wgust => false, :conhazo => false, :ptornado => false, :phail => false, :ptstmwinds => false, :pxtornado => false, :pxhail => false, :pxtstmwinds => false, :ptotsvrtstm => false, :pxtotsvrtstm =>false}
WSDL_PRODUCT =
"time-series"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Roker

Returns a new instance of Roker.



23
24
25
26
27
28
# File 'lib/roker.rb', line 23

def initialize(attributes={})
  attributes.each do |key, value|
    self.send("#{key}=", value)
  end
  self.started_at, self.ended_at = self.ended_at, self.started_at if self.ended_at && self.started_at > self.ended_at
end

Instance Attribute Details

#ended_atObject

Returns the value of attribute ended_at.



17
18
19
# File 'lib/roker.rb', line 17

def ended_at
  @ended_at
end

#latObject

Returns the value of attribute lat.



17
18
19
# File 'lib/roker.rb', line 17

def lat
  @lat
end

#lngObject

Returns the value of attribute lng.



17
18
19
# File 'lib/roker.rb', line 17

def lng
  @lng
end

#started_atObject

Returns the value of attribute started_at.



17
18
19
# File 'lib/roker.rb', line 17

def started_at
  @started_at
end

Instance Method Details

#find_weather_forecasts_attributesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/roker.rb', line 35

def find_weather_forecasts_attributes
  weather_forecasts_attributes = []
  interval = shortest_time_span
  self.started_at.upto(self.ended_at, interval, false) do |current_started_at|
    current_time_span = TimeSpan.new(:start_at => current_started_at, :duration => interval)
    current_weather_forecast_attributes = {
      :lat => self.lat, 
      :lng => self.lng, 
      :started_at => current_time_span.start_at, 
      :ended_at => current_time_span.end_at
    }
    parameters.each do |key, parameter|
      current_weather_forecast_attributes[key] = parameter.value_at(current_time_span) if parameter
    end
    weather_forecasts_attributes << current_weather_forecast_attributes
  end
  weather_forecasts_attributes
end

#parametersObject



86
87
88
# File 'lib/roker.rb', line 86

def parameters
  @parameters ||= parse_parameters
end

#parse_parameter(xpath, value_xpath = nil, calculation_method = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/roker.rb', line 107

def parse_parameter(xpath, value_xpath=nil, calculation_method=nil)
  element = self.weather_doc.at(xpath)
  if element
    time_layout_key = element.attributes['time-layout']
    time_layout = self.time_layouts[time_layout_key]
    values = []
    element = element.at(value_xpath) if value_xpath
    element.search('value').each do |val|
      values << val.inner_html.to_f
    end
    WeatherParameter.new(:time_layout => time_layout, :values => values, :calculation_method => calculation_method)
  end
end

#parse_parametersObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/roker.rb', line 91

def parse_parameters
  {
    :maximum_temperature =>           self.parse_parameter("temperature[@type='maximum']"),
    :minimum_temperature =>           self.parse_parameter("temperature[@type='minimum']"),
    :temperature =>                   self.parse_parameter("temperature[@type='hourly']"),
    :dewpoint_temperature =>          self.parse_parameter("temperature[@type='dew point']"),
    :liquid_precipitation =>          self.parse_parameter("precipitation[@type='liquid']"),
    :probability_of_precipitation =>  self.parse_parameter("probability-of-precipitation"),
    :wind_speed =>                    self.parse_parameter("wind-speed[@type='sustained']"),
    :wind_direction =>                self.parse_parameter("direction[@type='wind']"),
    :cloud_cover =>                   self.parse_parameter("cloud-amount[@type='total']"),
    :relative_humidity =>             self.parse_parameter("humidity[@type='relative']"),
    :wave_height =>                   self.parse_parameter("water-state", "waves[@type='significant']")
  }
end

#parse_time_layoutsObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/roker.rb', line 58

def parse_time_layouts
  tls = {}
  self.weather_doc.search('time-layout').each do |tl|
    key = (tl/'layout-key').first.inner_html
    
    start_at = []
    tl.search('start-valid-time').each do |s|
      start_at << self.class.parse_time(s.inner_html)
    end
    
    end_at = []
    tl.search('end-valid-time').each do |e|
      end_at << self.class.parse_time(e.inner_html)
    end
    
    tls[key] = TimeLayout.new(:start_at => start_at, :end_at => end_at, :key => key)
  end
  tls
end

#shortest_time_spanObject



78
79
80
81
82
83
84
# File 'lib/roker.rb', line 78

def shortest_time_span
  shorty = nil
  self.time_layouts.each do |key, time_layout|
    shorty = time_layout.interval if (shorty.nil? || shorty > time_layout.interval)
  end
  shorty
end

#time_layoutsObject



54
55
56
# File 'lib/roker.rb', line 54

def time_layouts
  @time_layouts ||= parse_time_layouts
end

#weather_docObject



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/roker.rb', line 121

def weather_doc
  @weather_doc ||= Hpricot::XML(self.weather_xml)
rescue Timeout::Error, 
       Errno::EINVAL, 
       Errno::ECONNRESET, 
       EOFError,
       Net::HTTPBadResponse, 
       Net::HTTPHeaderSyntaxError,
       Net::ProtocolError => e
  service_error = ServiceError.new
  service_error.set_backtrace(e.backtrace)
  raise service_error
end

#weather_forecasts_attributesObject



31
32
33
# File 'lib/roker.rb', line 31

def weather_forecasts_attributes
  @weather_forecasts_attributes ||= find_weather_forecasts_attributes
end

#weather_xmlObject



135
136
137
138
139
# File 'lib/roker.rb', line 135

def weather_xml
  @weather_xml ||=  weather_xml_soap
  # TODO can I do away with the weather_xml_soap and just use a url and URI.parse?
  # @weather_xml ||= weather_xml_uri
end