ckozus / rweather

Ruby gem for weather extraction from weather.com

This URL has Read+Write access

rweather / lib / r_weather_current_condition.rb
100644 32 lines (28 sloc) 0.834 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
RWeatherBar = Struct.new(:r, :d)
RWeatherWind = Struct.new(:s, :gust, :d, :t)
RWeatherUv = Struct.new(:i, :t)
RWeatherMoon = Struct.new(:icon, :t)
 
class RWeatherCurrentCondition
  attr_accessor :lsup, :obst, :tmp, :flik, :t, :icon, :bar, :wind, :hmid, :vis, :uv, :dewp, :moon
  
  def self.parse(simple_xml)
    current_condition = new
    cc = simple_xml['cc'].first
    cc.each do |key, value|
      current_condition.send(:"#{key}=", element_value(key, value))
    end
    current_condition
  end
 
  class << self
    def element_value(key, value)
      if value.size == 1 && !value.first.is_a?(Hash)
        value.first
      else
        data = const_get(:"RWeather#{key.capitalize}").new
        value.first.each do |key, value|
          data.send(:"#{key}=", value.first)
        end
        data
      end
    end
  end
end