Module: ItuCodes

Defined in:
lib/itu_codes.rb,
lib/itu_codes/helpers.rb,
lib/itu_codes/version.rb,
lib/itu_codes/constants.rb

Overview

TODO: add ability to search for common names (i.e. ‘USA’ or ‘United States of America’ for ‘United States’)

Defined Under Namespace

Modules: Constants, Helpers

Constant Summary collapse

VERSION =
"0.4.2"

Class Method Summary collapse

Class Method Details

.american?(some_code) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
# File 'lib/itu_codes.rb', line 96

def american?(some_code)
  # for non-US North American codes, parse_code will return a 4 digit code
  # for US, '1' will be returned
  countries = lookup(some_code[0,4]) || []
  countries.include?('United States of America')
end

.canadian?(some_code) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/itu_codes.rb', line 103

def canadian?(some_code)
  countries = lookup(some_code[0,4])
  north_american?(some_code) && (countries.include?('Canada'))
end

.compatriots?(some, other) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/itu_codes.rb', line 84

def compatriots?(some, other)
  both_valid = ! ( parse_code(some).nil? or parse_code(other).nil? )

  if north_american?(some) && north_american?(other)
    both_valid && !(lookup(some) & lookup(other)).empty?
  else
    some  = parse_code(some)
    other = parse_code(other)
    both_valid && lookup(some) == lookup(other)
  end
end

.find_by_itu_code(code) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/itu_codes.rb', line 12

def find_by_itu_code(code)
  returner = lookup(code)

  if returner.size <= 1
    returner.first
  else
    returner
  end
end

.find_by_name(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/itu_codes.rb', line 22

def find_by_name(name)
  matching = country_codes.select do |k, v|
    [*v].include? name
  end || {}

  returner = matching.keys

  if returner.size <= 1
    returner.first
  else
    returner
  end
end

.iso2itu(iso_code) ⇒ Object

lookup by ISO 3166 country code e.g. iso2itu(‘US’) see : www.iso.org/iso/country_codes.htm



39
40
41
42
# File 'lib/itu_codes.rb', line 39

def iso2itu(iso_code)
  country_name = Helpers.country_name_lookup(iso_code)
  find_by_name(country_name)
end

.itu2iso(itu) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/itu_codes.rb', line 44

def itu2iso(itu)
  name = find_by_itu_code(itu)

  if name.is_a?(String)
    Helpers.country_code_lookup(name)
  elsif name.is_a?(Array)
    name.map do |s|
      Helpers.country_code_lookup(s)
    end
  end
end

.north_american?(some_code) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/itu_codes.rb', line 63

def north_american?(some_code)
  some_code[0,1] == '1'
end

.parse_code(some_number) ⇒ Object

parse a destination code (probably with area code) to find the ITU code

ex:  parse_code(1818) =>  1


69
70
71
72
73
74
75
# File 'lib/itu_codes.rb', line 69

def parse_code(some_number)
  some_number = clean(some_number)
  sub_index = (1..some_number.length).find do |len|
    valid_code? some_number[0,len]
  end
  some_number[0,sub_index] unless sub_index.nil?
end

.parse_number(some_number) ⇒ Object

parse a destination code (probably with area code) to find the number without the ITU code

ex:  parse_number(18184443322) => 8184443322


79
80
81
82
# File 'lib/itu_codes.rb', line 79

def parse_number(some_number)
  country_code = parse_code(some_number)
  some_number.gsub(/^#{country_code}/) unless country_code.nil?
end

.valid_code?(some_code) ⇒ Boolean

returns true for any valid ITU code valid_code?(1) => true valid_code?(1818) => false

Returns:

  • (Boolean)


59
60
61
# File 'lib/itu_codes.rb', line 59

def valid_code?(some_code)
  country_codes.has_key?(some_code)
end