Class: Hamweather::Forecast

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

Defined Under Namespace

Classes: Daily, Hourly

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ Forecast

Returns a new instance of Forecast.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hamweather/forecast.rb', line 14

def initialize(location)
  # Construct url from api key, location uri
  # Request url, get xml then parse
  xml_data = self.class.fetch(location.to_uri).gsub!(/\n/,'')
  data = Hpricot.parse(xml_data)
  
  @dailies = {}
  @hourlies = {}
        
  data.at(:wxforecast).children.each do |day| 
    #TODO make ordered hash
    @dailies[day[:date].to_s] = Daily.new(day)
  end
  
  data.at(:wxshortterm).children.each do |hour|
    #TODO make ordered hash
    @hourlies[hour[:time]] = Hourly.new(hour)
    @dailies[hour[:date]].hours[hour[:time]] = Hourly.new(hour)
  end
end

Instance Attribute Details

#dailiesObject

> day_proxy_object.high_farenheit

> hour_proxy_object.high_farenheit



12
13
14
# File 'lib/hamweather/forecast.rb', line 12

def dailies
  @dailies
end

#forecastObject

> day_proxy_object.high_farenheit

> hour_proxy_object.high_farenheit



12
13
14
# File 'lib/hamweather/forecast.rb', line 12

def forecast
  @forecast
end

#hourliesObject

> day_proxy_object.high_farenheit

> hour_proxy_object.high_farenheit



12
13
14
# File 'lib/hamweather/forecast.rb', line 12

def hourlies
  @hourlies
end

Class Method Details

.fetch(location_uri) ⇒ Object

TODO stub out calls to this where possible. TODO / or raise an error.



37
38
39
# File 'lib/hamweather/forecast.rb', line 37

def self.fetch(location_uri)
  Net::HTTP.get URI.parse("http://hwlite.hamweather.net/#{Hamweather.api_key}/#{location_uri}")
end

Instance Method Details

#each_dayObject



41
42
43
44
45
# File 'lib/hamweather/forecast.rb', line 41

def each_day
  @dailies.each_pair do |key, value|
    yield value
  end
end

#each_hourObject



47
48
49
50
51
# File 'lib/hamweather/forecast.rb', line 47

def each_hour
  @hourlies.each_pair do |key, value|
    yield value
  end
end