class EltiempoParser
Attributes
api_key[R]
urlcities[R]
Public Class Methods
new(api_key = "zdo2c683olan")
click to toggle source
# 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
Public Instance Methods
all_cities()
click to toggle source
# File lib/eltiempo/eltiempo_parser.rb, line 42 def all_cities urlcities.keys end
average_temp(city, value = "max")
click to toggle source
# 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)
click to toggle source
# 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
Private Instance Methods
fetch_city_doc(city)
click to toggle source
# File lib/eltiempo/eltiempo_parser.rb, line 48 def fetch_city_doc city unless urlcities[city] raise Thor::Error, "Error: #{city} city not found" end Nokogiri::XML(open(urlcities[city] + "&affiliate_id=" + api_key)) end