Class: Regentanz::GoogleWeather

Inherits:
Object
  • Object
show all
Includes:
Astronomy, Callbacks
Defined in:
lib/regentanz/google_weather.rb

Constant Summary

Constants included from Callbacks

Callbacks::CALLBACKS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

included

Constructor Details

#initialize(*args) ⇒ GoogleWeather

Creates an object and queries the weather API.

Parameters

  • options A hash

Available options

  • location String to pass to Google’s weather API (mandatory)

  • cache_id enables caching with a unique identifier to locate a cached file

  • geodata a hash with keys :lat and :lng, required for sunset/-rise calculations

  • lang Desired language for the returned results (Defaults to “de”)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/regentanz/google_weather.rb', line 23

def initialize(*args)
  options = args.extract_options!
  @options  = options.symbolize_keys
  @parser = Parser::GoogleWeather.new
  self.location = args.first || options[:location]
  self.lang = options[:lang]

  # Activate caching
  if Regentanz.configuration.cache_backend
    @cache    = Regentanz.configuration.cache_backend.new if Regentanz.configuration.cache_backend
    @cache_id = options[:cache_id] || Regentanz.configuration.cache_backend.sanitize_key(@location)
  end

  @geodata  = options[:geodata] if options[:geodata] and options[:geodata][:lat] and options[:geodata][:lng]
  get_weather() unless Regentanz.configuration.do_not_get_weather
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



11
12
13
# File 'lib/regentanz/google_weather.rb', line 11

def cache
  @cache
end

#cache_idObject

Returns the value of attribute cache_id.



10
11
12
# File 'lib/regentanz/google_weather.rb', line 10

def cache_id
  @cache_id
end

#currentObject (readonly)

Returns the value of attribute current.



11
12
13
# File 'lib/regentanz/google_weather.rb', line 11

def current
  @current
end

#forecastObject (readonly)

Returns the value of attribute forecast.



11
12
13
# File 'lib/regentanz/google_weather.rb', line 11

def forecast
  @forecast
end

#langObject

Returns the value of attribute lang.



11
12
13
# File 'lib/regentanz/google_weather.rb', line 11

def lang
  @lang
end

#locationObject

Returns the value of attribute location.



10
11
12
# File 'lib/regentanz/google_weather.rb', line 10

def location
  @location
end

#parserObject (readonly)

Returns the value of attribute parser.



11
12
13
# File 'lib/regentanz/google_weather.rb', line 11

def parser
  @parser
end

#xmlObject (readonly)

Returns the value of attribute xml.



11
12
13
# File 'lib/regentanz/google_weather.rb', line 11

def xml
  @xml
end

Instance Method Details

#get_weather!Object

Loads weather data from known data sources (ie. cache or external API)



41
# File 'lib/regentanz/google_weather.rb', line 41

def get_weather!; get_weather(); end

#present?Boolean

Provide an accessor to see if we actually got weather info at all.

Returns:

  • (Boolean)


49
50
51
# File 'lib/regentanz/google_weather.rb', line 49

def present?
  current.present? or forecast.present?
end

#sunriseObject



53
54
55
# File 'lib/regentanz/google_weather.rb', line 53

def sunrise
  @sunrise ||= @geodata.blank? ? nil : sun_rise_set(:sunrise, @geodata[:lat], @geodata[:lng])
end

#sunsetObject



57
58
59
# File 'lib/regentanz/google_weather.rb', line 57

def sunset
  @sunset  ||= @geodata.blank? ? nil : sun_rise_set(:sunset, @geodata[:lat], @geodata[:lng])
end

#waiting_for_retry?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/regentanz/google_weather.rb', line 61

def waiting_for_retry?
  @cache && @cache.waiting_for_retry?
end