Class: Meteorology::Providers::Yr::XmlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/meteorology/providers/yr.rb

Constant Summary collapse

ATTR_MAPPING =
{
  'fog' => :percent,
  'pressure' => :value,
  'highClouds' => :percent,
  'windDirection' => :name,
  'mediumClouds' => :percent,
  'windSpeed' => :mps,
  'cloudiness' => :percent,
  'lowClouds' => :percent,
  'humidity' => :value,
  'temperature' => :value,
  'symbol' => :number,
  'precipitation' => :value,
  'temperatureProbability' => :value,
  'windProbability' => :value,
  'symbolProbability' => :value
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeXmlParser

Returns a new instance of XmlParser.



35
36
37
38
# File 'lib/meteorology/providers/yr.rb', line 35

def initialize
  @forecasts = WeatherCollection.new
  @location = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



40
41
42
# File 'lib/meteorology/providers/yr.rb', line 40

def method_missing(*args)
  #dont care
end

Instance Attribute Details

#forecastsObject

Returns the value of attribute forecasts.



15
16
17
# File 'lib/meteorology/providers/yr.rb', line 15

def forecasts
  @forecasts
end

Instance Method Details

#new_period?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/meteorology/providers/yr.rb', line 80

def new_period?(attributes)
  attributes['from'] == attributes['to']
end

#on_end_element(element) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/meteorology/providers/yr.rb', line 72

def on_end_element(element)
  if element == 'location' && @location == 2
    # Done with one period
    @location = 0
    @current_period = nil
  end
end

#on_start_element(element, attributes) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/meteorology/providers/yr.rb', line 44

def on_start_element(element, attributes)
  case element
  when 'time'
    if new_period?(attributes)
      key = Time.parse(attributes['from'])
      @current_period = @forecasts[key] = Meteorology::Weather.new(key)
    end
  when 'location'
    if @current_period
      @location += 1
    end
  else
    if @current_period && @location <= 2
      read_value(element, attributes)
    end
  end
end

#read_value(element, attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/meteorology/providers/yr.rb', line 62

def read_value(element, attributes)
  case element
  when 'temperature'
    @current_period.temperature = Meteorology::Temperature.new(attributes['unit'].to_sym, attributes['value'])
  else
    method = "#{element}="
    @current_period.send(method, attributes[ATTR_MAPPING[element].to_s]) if @current_period.respond_to?(method)            
  end
end