Class: Place

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, max_today, min_today, max_avg, min_avg) ⇒ Place

Returns a new instance of Place.



8
9
10
11
12
13
14
# File 'lib/place.rb', line 8

def initialize( name, max_today, min_today, max_avg, min_avg )
   @name = name
   @max_today = max_today
   @min_today = min_today
   @max_avg = max_avg
   @min_avg = min_avg
end

Instance Attribute Details

#max_avgObject

Returns the value of attribute max_avg.



4
5
6
# File 'lib/place.rb', line 4

def max_avg
  @max_avg
end

#max_todayObject

Returns the value of attribute max_today.



4
5
6
# File 'lib/place.rb', line 4

def max_today
  @max_today
end

#min_avgObject

Returns the value of attribute min_avg.



4
5
6
# File 'lib/place.rb', line 4

def min_avg
  @min_avg
end

#min_todayObject

Returns the value of attribute min_today.



4
5
6
# File 'lib/place.rb', line 4

def min_today
  @min_today
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/place.rb', line 4

def name
  @name
end

Class Method Details

.get_array_from_xml(place, places_list) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/place.rb', line 26

def Place.get_array_from_xml( place, places_list )
  aff_id = "&affiliate_id=fvzoaihj8486"
  necessary_data = [ :temperaturaminima, :temperaturamaxima ]
  city_values = {}

  place_url = ( places_list[place].sub( "&", "&" ) + aff_id )
  doc = Nokogiri::XML( open( place_url ) )
  data_city = doc.xpath( "//var" )
  data_city.each do |weather_param|
    city_values[ weather_param.children.children[0].to_s.to_sym_city ] = weather_param.children.children[2..-1].map{ |att| att.attributes['value'].value.to_f }
  end
  city_values = city_values.select{ |key, value| necessary_data.include?( key ) }
  return city_values
end

.get_places_listObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/place.rb', line 43

def Place.get_places_list
  begin
    doc = Nokogiri::XML(open("http://api.tiempo.com/index.php?api_lang=es&division=102&affiliate_id=fvzoaihj8486"))
    data_cities = doc.xpath( "//data" )
    places = {}
    names = []
    data_cities.each do |data_city|
      places[ data_city.children.children[0].to_s.to_sym_city ] = data_city.children.children[1].to_s
      names << data_city.children.children[0].to_s
    end
    return places, names
  rescue
    return false, false
  end
end

.set_temperatures(place, places) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/place.rb', line 61

def Place.set_temperatures( place, places )
  place_data = Place.verify_and_get_temperatures( place.to_s, places )

  place = ( Place.new( place,
    place_data[ :temperaturamaxima ][0],
    place_data[ :temperaturaminima ][0],
    ( place_data[ :temperaturamaxima ].sum / place_data[ :temperaturamaxima ].length ).round( 2 ),
    ( place_data[ :temperaturaminima ].sum / place_data[ :temperaturaminima ].length ).round( 2 ) ) rescue nil )
end

.verify_and_get_temperatures(place, places_list) ⇒ Object



18
19
20
21
22
# File 'lib/place.rb', line 18

def Place.verify_and_get_temperatures( place, places_list )
  place = place.to_sym_city
  city_values = Place.get_array_from_xml( place, places_list ) if places_list.has_key?( place )
  return city_values
end