Class: EltiempoParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = "zdo2c683olan") ⇒ EltiempoParser

Returns a new instance of EltiempoParser.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/eltiempo/eltiempo_parser.rb', line 8

def initialize(api_key = "zdo2c683olan")
  @urlcities = {}
  @api_key = api_key

  url = "http://api.tiempo.com/index.php?api_lang=es&provincia=2&affiliate_id=" + @api_key
  nokogiri_parse = Nokogiri::XML(open(url))

  nokogiri_parse.xpath('//data').each do |city|
    @urlcities.store(city.xpath("name").text, city.xpath("url").text)
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/eltiempo/eltiempo_parser.rb', line 6

def api_key
  @api_key
end

#urlcitiesObject (readonly)

Returns the value of attribute urlcities.



6
7
8
# File 'lib/eltiempo/eltiempo_parser.rb', line 6

def urlcities
  @urlcities
end

Instance Method Details

#average_temp(city, value = "max") ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eltiempo/eltiempo_parser.rb', line 21

def average_temp(city, value = "max")
  value == "max"? value = "Temperatura máxima" : value = "Temperatura mínima"
  doc = fetch_city_doc(city)
  element = doc.xpath("//var[./name[contains(text(),'#{value}')]]")
  counter = 0

  element.xpath('data/forecast').each do |temp|
    counter += temp.attribute("value").text.to_i
  end

  counter / 7
end

#today_temp(city) ⇒ Object



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

def today_temp city
  doc = fetch_city_doc(city)
  prediction_url = doc.xpath("//url").text
  html_scraper = Nokogiri::HTML(open(prediction_url))

  html_scraper.css('dd.ddTemp').text
end