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/calling.rb,
lib/iso_country_codes/iso_4217.rb,
lib/iso_country_codes/iso_3166_1.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Code, UnknownCodeError

Class Method Summary collapse

Class Method Details

.allObject



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

def all
  Code.all
end

.find(code) ⇒ Object

Raises:



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

def find(code)
  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
  end

  raise UnknownCodeError, "No ISO 3166-1 code could be found for '#{code}'." if instance.nil?

  instance
end

.for_select(*args) ⇒ Object



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

def for_select(*args)
  Code.for_select(*args)
end

.search_by_calling(code) ⇒ Object

Alias of search_by_calling_code



54
55
56
# File 'lib/iso_country_codes/iso_country_codes.rb', line 54

def search_by_calling(code) # Alias of search_by_calling_code
  search_by_calling_code code
end

.search_by_calling_code(code) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
# File 'lib/iso_country_codes/iso_country_codes.rb', line 45

def search_by_calling_code(code)
  code = "+#{code.to_i}" # Normalize code
  instances = all.select { |c| c.calling == code }

  raise UnknownCodeError, "No ISO 3166-1 codes could be found searching with calling code '#{code}'." if instances.empty?

  instances
end

.search_by_currency(code) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/iso_country_codes/iso_country_codes.rb', line 58

def search_by_currency(code)
  code = code.to_str.upcase
  instances = all.select { |c|
    c.currencies.select { |currency|
      currency != code
    }.empty?
  }

  raise UnknownCodeError, "No ISO 3166-1 codes could be found searching with currency '#{code}'." if instances.empty?

  instances
end

.search_by_name(str) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
# File 'lib/iso_country_codes/iso_country_codes.rb', line 35

def search_by_name(str)
  instances = all.select { |c| c.name.upcase == str.upcase }
  instances = all.select { |c| c.name.match(/^#{str}/i) } if instances.empty?
  instances = all.select { |c| c.name.match(/#{str}/i) } if instances.empty?

  raise UnknownCodeError, "No ISO 3166-1 codes could be found searching with name '#{str}'." if instances.empty?

  instances
end