Class: Regentanz::Parser::GoogleWeather

Inherits:
Object
  • Object
show all
Defined in:
lib/regentanz/parser/google_weather.rb

Constant Summary collapse

XML_ENCODING =

source encoding of the XML data

'LATIN1'

Instance Method Summary collapse

Instance Method Details

#convert_encoding(data) ⇒ Object

Converts a string from XML_ENCODING to UTF-8 using the Iconv library.

Note

FIXME The XML source document seems to be delivered LATIN1 or UTF-8, completely random and unpredictable.

Parameters

  • data: string to convert



16
17
18
19
# File 'lib/regentanz/parser/google_weather.rb', line 16

def convert_encoding data
  return if data.blank?
  self.class::XML_ENCODING != "UTF-8" ? Iconv.iconv("UTF-8", self.class::XML_ENCODING, data).flatten.join(" ") : data
end

#parse_current!(xml) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/regentanz/parser/google_weather.rb', line 21

def parse_current!(xml)
  return if xml.blank?
  begin
    doc = REXML::Document.new(xml)
    if doc.elements['xml_api_reply/weather/current_conditions']
      attributes = {}
      doc.elements['xml_api_reply/weather/current_conditions'].each_element do |ele|
        attributes.merge! parse_node(ele)
      end
      Regentanz::Conditions::Current.new(attributes)
    end
  rescue
    # FIXME should report error
  end
end

#parse_forecast!(xml) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/regentanz/parser/google_weather.rb', line 37

def parse_forecast!(xml)
  return if xml.blank?
  forecasts = []
  begin
    doc = REXML::Document.new(xml)
    if doc.elements['xml_api_reply/weather/forecast_conditions']
      doc.elements.each('xml_api_reply/weather/forecast_conditions') do |forecast_element|
        attributes = {}
        forecast_element.each_element { |ele| attributes.merge! parse_node(ele) }
        forecasts << Regentanz::Conditions::Forecast.new(attributes)
      end
    end
    forecasts
  rescue
    # FIXME should report error
  end
end

#parse_node(element) ⇒ Object

Handles some special treatment for certain sub-elements, transforms element into a hash and returns it.

Parameters

  • element childnode of type REXML::Element



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/regentanz/parser/google_weather.rb', line 62

def parse_node(element)
  hash = {}
  hash.merge! parse_style(element.attribute("data").to_s) if element.name == "icon"
  if element.name == "humidity"
    # Handle the small whitespace just before the '%'
    hash.merge! element.name => element.attribute("data").to_s.gsub(/(\d+).+%$/, '\1 %')
  elsif element.name == "icon"
    # We want the full URL
    hash.merge! element.name => "http://www.google.com" + element.attribute("data").to_s
  elsif (element.name == "high" or element.name == "low") and @lang == "en"
    # Forecast-temperatures in EN are listed in °F; we fix that here
    temp_f = element.attribute("data").to_s.to_i
    hash.merge! element.name => ( temp_f - 32 ) * 5 / 9
  else
    hash.merge! element.name => element.attribute("data").to_s
  end
  hash
end