Class: WeatherBug::HashMethods

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

Constant Summary collapse

WB_MAPPING =
{
  :zip_code => 'ZipCode',
  :city_code => 'CityCode',
  :postal_code => 'PostalCode',
  :include_pws => 'IncludePWS',
  :latitude => 'Latitude',
  :longitude => 'Longitude'
}

Class Method Summary collapse

Class Method Details

.convert_symbols(hash) ⇒ Object



31
32
33
34
35
36
# File 'lib/weatherbug/hash_methods.rb', line 31

def self.convert_symbols(hash)
  hash.inject({}) do |rhash, (original_key, value)|
    rhash[WB_MAPPING.fetch(original_key, original_key)] = value
    rhash
  end
end

.valid_keys(hash, allowed) ⇒ Object

Raise an argument error if we get invalid keys at all

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/weatherbug/hash_methods.rb', line 15

def self.valid_keys(hash, allowed)
  invalids = nil
  hash.keys.each { |key| (invalids ||= []; invalids << key) unless allowed.include?(key) }
  raise ArgumentError.new("Invalid options: #{invalids.join(', ')}") unless invalids.nil?
end

.valid_needs(hash, key, key_for) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
# File 'lib/weatherbug/hash_methods.rb', line 27

def self.valid_needs(hash, key, key_for)
  raise ArgumentError.new("You must supply option #{key} to use #{key_for}") unless hash.has_key?(key)
end

.valid_one_only(hash, conflictions) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/weatherbug/hash_methods.rb', line 21

def self.valid_one_only(hash, conflictions)
  conflicts = []
  conflictions.each { |c| conflicts << c if hash.keys.include?(c) }
  raise ArgumentError.new("Cannot combine options: #{conflicts.join(', ')}") if conflicts.size > 1
end