Class: IsoCountryCodes

Inherits:
Object
  • Object
show all
Defined in:
lib/iso_country_codes/iso_country_codes.rb,
lib/iso_country_codes/code.rb,
lib/iso_country_codes/iso_4217.rb,
lib/iso_country_codes/iso_3166_1.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Code, UnknownCodeError

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.allObject



7
8
9
# File 'lib/iso_country_codes/iso_country_codes.rb', line 7

def all
  Code.all
end

.find(code, opts = {}) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/iso_country_codes/iso_country_codes.rb', line 11

def find(code, opts={})
  code     = code.to_s.upcase
  instance = nil

  if code.match(/^\d{2}$/)
    code = "0#{code}" # Make numeric codes three digits
  end

  if code.match(/^\d{3}$/)
    instance = all.select { |c| c.numeric == code }.first
  elsif code.match(/^[A-Z]{2}$/)
    instance = all.select { |c| c.alpha2 == code }.first
  elsif code.match(/^[A-Z]{3}$/)
    instance = all.select { |c| c.alpha3 == code }.first
  else
    instance = all.select { |c| c.name.upcase == code }.first
    if opts[:fuzzy]
      instance = all.select { |c| c.name.match(/^#{code}/i) }.first if instance.nil?
      instance = all.select { |c| c.name.match(/#{code}/i) }.first if instance.nil?
    end
  end

  raise UnknownCodeError, "ISO 3166-1 code '#{code}' does not exist." if instance.nil?

  instance
end