Class: Noaaweather

Inherits:
CachedGeocode show all
Defined in:
lib/noaa-weather.rb

Constant Summary collapse

VERSION =
'0.1.5'

Instance Attribute Summary collapse

Attributes inherited from CachedGeocode

#address, #geocoding

Instance Method Summary collapse

Methods inherited from CachedGeocode

#geocode

Constructor Details

#initialize(site, as_address = false) ⇒ Noaaweather

Returns a new instance of Noaaweather.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/noaa-weather.rb', line 61

def initialize(site,as_address=false)
  
  if as_address
    self.address = site
    _z = geocode
    self.lattitude = _z[:lat]
    self.longitude = _z[:lon]
    @site = _z[:site]
  else    
    self.lattitude = 41.190856
    self.longitude = -81.446962
    @site = site
  end

  @logger = Logger.new(STDOUT)
  self.start_time = Time.now()
  @is_cached = false
end

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



59
60
61
# File 'lib/noaa-weather.rb', line 59

def conditions
  @conditions
end

#lattitudeObject

Returns the value of attribute lattitude.



59
60
61
# File 'lib/noaa-weather.rb', line 59

def lattitude
  @lattitude
end

#longitudeObject

Returns the value of attribute longitude.



59
60
61
# File 'lib/noaa-weather.rb', line 59

def longitude
  @longitude
end

#start_timeObject

Returns the value of attribute start_time.



59
60
61
# File 'lib/noaa-weather.rb', line 59

def start_time
  @start_time
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/noaa-weather.rb', line 121

def cached?
  @is_cached
end

#current_conditions(ashash = false) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/noaa-weather.rb', line 89

def current_conditions(ashash=false)

  self.forecast if self.conditions.nil?
  if Time.now.strftime("%H").to_i < 6 or Time.now.strftime("%H").to_i > 18
    if ashash
      self.forecast['Tonight']
    else
      @ccond ||= sprintf("%s, High of %i, Low of %i and %s",'Tonight',self.forecast['Tonight'][:high],
                         self.forecast['Tonight'][:low], self.forecast['Tonight'][:conditions])
    end
  elsif Time.now.strftime("%H").to_i < 18
    if ashash
      self.forecast['Today']
    else
      @ccond ||= sprintf("%s, High of %i, Low of %i and %s",'Today',self.forecast['Today'][:high],
                         self.forecast['Today'][:low], self.forecast['Today'][:conditions])
    end
  else
    if ashash
      self.forecast['Today']
    else
      @ccond ||= sprintf("%s, High of %i, Low of %i and %s",'Today',self.forecast['Today'][:high],
                         self.forecast['Today'][:low], self.forecast['Today'][:conditions])
    end        
  end
end

#current_iconObject



116
117
118
119
# File 'lib/noaa-weather.rb', line 116

def current_icon
  self.fetch unless @fetched
  @cicon ||= @doc.find('/dwml/data/parameters/conditions-icon/icon-link')[0].content
end

#current_tempObject



80
81
82
83
# File 'lib/noaa-weather.rb', line 80

def current_temp
  self.fetch unless @fetched
  @ctemp ||= @doc.find('/dwml/data/parameters/temperature/value')[0].content.to_i
end

#fetchObject



125
126
127
# File 'lib/noaa-weather.rb', line 125

def fetch
  @doc = fetch_from_noaa
end

#forecastObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/noaa-weather.rb', line 129

def forecast
  @forecast = fetch_forecast_from_noaa
  @forecast.find('/dwml/data/time-layout')
  _when = @forecast.find('/dwml/data/time-layout')[2].find('start-valid-time').collect {|x| x['period-name'] }[0..5]
  _daily_maxt = @forecast.find('/dwml/data/parameters/temperature')[0].find('value').collect {|x| x.content }[0..2]
  _daily_mint = @forecast.find('/dwml/data/parameters/temperature')[1].find('value').collect {|x| x.content }[0..2]
  _weather = @forecast.find('/dwml/data/parameters/weather/weather-conditions').collect {|x| x['weather-summary'] }[0..5]
  count = 0
  sub_count = 0
  _dat = {}
  while count < 6
    _dat[_when[count]] = {:high => _daily_maxt[sub_count.floor], :low => _daily_mint[sub_count.floor], :conditions => _weather[count] }
    count += 1
    sub_count += 0.5
  end
  self.conditions = _dat
end

#locationObject



85
86
87
# File 'lib/noaa-weather.rb', line 85

def location
  @site.to_s
end