Class: ValidZipCodes

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

Constant Summary collapse

SUPPORTED_COUNTRIES =
["DK", "SE", "FO", "GL", "NO"]

Instance Method Summary collapse

Constructor Details

#initialize(load_all = false) ⇒ ValidZipCodes

Constructor



8
9
10
11
12
13
14
# File 'lib/valid_zip_codes.rb', line 8

def initialize load_all = false
  if load_all
    load_all_countries 
  else
    @zips = []
  end
end

Instance Method Details

#get_city_name(country, zip) ⇒ Object

Get city name by zip



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/valid_zip_codes.rb', line 32

def get_city_name(country, zip)

  if supports_country?(country)
    # Load country if not already loaded
    load_country(country) if !country_loaded?(country)

    # Query for zip
    sr = country_hash(country).select{|z| z["zip"] == zip.to_s}
    sr.count > 0 ? sr.first["city"] : raise("Zip not found")
  else
    raise "Country not supported"
  end

end

#is_valid_zip?(country, zip) ⇒ Boolean

Check if zip is valid

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/valid_zip_codes.rb', line 17

def is_valid_zip?(country, zip)
  
  if supports_country?(country)
    # Load country if not already loaded
    load_country(country) if !country_loaded?(country)

    # Query for zip
    country_hash(country).select{|z| z["zip"] == zip.to_s}.count > 0
  else
    raise "Country not supported"
  end

end

#supports_country?(country) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/valid_zip_codes.rb', line 47

def supports_country?(country)
  SUPPORTED_COUNTRIES.include?(country)
end