class LocationCache

Attributes

location[R]

Public Class Methods

new(location, options = {}) click to toggle source
# File lib/meteorologist/location_cache.rb, line 8
def initialize(location, options = {})
  @location = location.downcase
end
write(location, attributes) click to toggle source
# File lib/meteorologist/location_cache.rb, line 4
def self.write(location, attributes)
  new(location).write(attributes)
end

Public Instance Methods

coordinates() click to toggle source
# File lib/meteorologist/location_cache.rb, line 16
def coordinates
  cache[location][:coordinates] if exists?
end
exists?() click to toggle source
# File lib/meteorologist/location_cache.rb, line 12
def exists?
  !cache[location].nil?
end
name() click to toggle source
# File lib/meteorologist/location_cache.rb, line 20
def name
  cache[location][:name] if exists?
end
write(attributes) click to toggle source
# File lib/meteorologist/location_cache.rb, line 24
def write(attributes)
  validate_required_attributes(attributes)

  cache[location] = attributes
  File.open(cache_full_path, 'w+') { |f| f.write(cache.to_yaml) }
end

Private Instance Methods

cache() click to toggle source
# File lib/meteorologist/location_cache.rb, line 34
def cache
  return @cache if defined? @cache
  @cache = YAML.load(cache_contents) || Hash.new
end
cache_contents() click to toggle source
# File lib/meteorologist/location_cache.rb, line 39
def cache_contents
  File.read(cache_full_path)
end
cache_full_path() click to toggle source
# File lib/meteorologist/location_cache.rb, line 43
def cache_full_path
  File.expand_path(ENV['CACHE_PATH'], __FILE__)
end
invalid_attributes() click to toggle source
# File lib/meteorologist/location_cache.rb, line 52
def invalid_attributes
  raise ArgumentError, 'You must include location name and coordinates'
end
validate_required_attributes(attributes) click to toggle source
# File lib/meteorologist/location_cache.rb, line 47
def validate_required_attributes(attributes)
  invalid_attributes unless attributes[:coordinates]
  invalid_attributes unless attributes[:name]
end