Class: Welements::ForecastParameters

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ ForecastParameters

:noinit:



12
13
14
# File 'lib/welements/welements.rb', line 12

def initialize(doc) #:noinit:
  @doc = doc
end

Class Method Details

.from_xml(doc) ⇒ Object

:nodoc:



8
9
10
# File 'lib/welements/welements.rb', line 8

def self.from_xml(doc) #:nodoc:
  new(doc)
end

Instance Method Details

#build(parameter, attributes = {}, value_type = :integer) ⇒ Object

private



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/welements/welements.rb', line 116

def build(parameter, attributes={}, value_type=:integer)
  attributes_filter = ''
  attributes.each {|k, v| attributes_filter += "[@#{k}='#{v}']" }
  
  values = @doc.find("/dwml/data/parameters[1]/#{parameter}#{attributes_filter}[1]/value/text()").map do |node|
    value_type == :integer ? node.to_s.to_i : node.to_s
  end

  layout_key = @doc.find("/dwml/data/parameters[1]/#{parameter}#{attributes_filter}[1]").first[:'time-layout']

  layout = nil
  @doc.find("/dwml/data/time-layout").each do |node|
    node.each_element do |el|
      # the layout key will always be the first element, check it
      # then go on to the next layout node
      layout = node if el.first.to_s == layout_key
      break
    end
  end

  data = []
  layout.each do |el|
    if el.name == 'start-valid-time'
      data.push({:time => Time.parse(el.first.to_s),
                  :value => values[data.length]})
    end
  end
  data
end

#cloud_coverObject



105
106
107
# File 'lib/welements/welements.rb', line 105

def cloud_cover
  build('cloud-amount')
end

#endsObject

datetime of the last reading in the forecast



34
35
36
37
38
39
40
41
# File 'lib/welements/welements.rb', line 34

def ends
  ends = nil
  @doc.find("/dwml/data/time-layout/end-valid-time/text()").map do |node|
    t = Time.parse(node.to_s)
    ends = t if !ends || t > ends
  end
  @ends = ends
end

#fire_outlook_from_dry_thunderstormsObject



109
110
111
112
# File 'lib/welements/welements.rb', line 109

def fire_outlook_from_dry_thunderstorms
  build('fire-weather', {'type' => 'risk from dry thunderstorms'},
  :string)
end

#lengthObject

The number of days provided by the forecast



19
20
21
# File 'lib/welements/welements.rb', line 19

def length
  @length ||= @doc.find("/dwml/data/time-layout[@summarization='24hourly'][1]/start-valid-time").length
end

#maximaObject

TODO remove



44
45
46
47
48
# File 'lib/welements/welements.rb', line 44

def maxima
  @maxima ||= @doc.find("/dwml/data/parameters[1]/temperature[@type='maximum'][@units='Fahrenheit'][1]/value/text()").map do |node|
    node.to_s.to_i
  end
end

#minimaObject

TODO remove



51
52
53
54
55
# File 'lib/welements/welements.rb', line 51

def minima
  @minima ||= @doc.find("/dwml/data/parameters[1]/temperature[@type='minimum'][@units='Fahrenheit'][1]/value/text()").map do |node|
    node.to_s.to_i
  end
end

#precipitation_liquidObject



85
86
87
# File 'lib/welements/welements.rb', line 85

def precipitation_liquid
  build('precipitation', {'type' => 'liquid'})
end

#precipitation_snowObject



89
90
91
# File 'lib/welements/welements.rb', line 89

def precipitation_snow
  build('precipitation', {'type' => 'snow'})
end

#probability_of_precipitationObject



57
58
59
# File 'lib/welements/welements.rb', line 57

def probability_of_precipitation
  build('probability-of-precipitation')
end

#relative_humidityObject



61
62
63
# File 'lib/welements/welements.rb', line 61

def relative_humidity
  build('humidity')
end

#startsObject

datetime of the first reading in the forecast



24
25
26
27
28
29
30
31
# File 'lib/welements/welements.rb', line 24

def starts
  starts = nil
  @doc.find("/dwml/data/time-layout/start-valid-time/text()").map do |node|
    t = Time.parse(node.to_s)
    starts = t if !starts || t < starts
  end
  @starts = starts
end

#temperature_apparentObject



81
82
83
# File 'lib/welements/welements.rb', line 81

def temperature_apparent
  build('temperature', {'type' => 'apparent'})
end

#temperature_dew_pointObject



77
78
79
# File 'lib/welements/welements.rb', line 77

def temperature_dew_point
  build('temperature', {'type' => 'dew point'})
end

#temperature_hourlyObject



73
74
75
# File 'lib/welements/welements.rb', line 73

def temperature_hourly
  build('temperature', {'type' => 'hourly'})
end

#temperature_maximumObject



65
66
67
# File 'lib/welements/welements.rb', line 65

def temperature_maximum
  build('temperature', {'type' => 'maximum'})
end

#temperature_minimumObject



69
70
71
# File 'lib/welements/welements.rb', line 69

def temperature_minimum
  build('temperature', {'type' => 'minimum'})
end

#wind_directionObject



101
102
103
# File 'lib/welements/welements.rb', line 101

def wind_direction
  build('direction', {'type' => 'wind'})
end

#wind_speed_gustObject



97
98
99
# File 'lib/welements/welements.rb', line 97

def wind_speed_gust
  build('wind-speed', {'type' => 'gust'})
end

#wind_speed_sustainedObject



93
94
95
# File 'lib/welements/welements.rb', line 93

def wind_speed_sustained
  build('wind-speed', {'type' => 'sustained'})
end