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
96
97
98
99
100
101
|
# File 'lib/itu_codes.rb', line 96
def american?(some_code)
countries = lookup(some_code[0,4]) || []
countries.include?('United States of America')
end
|
.canadian?(some_code) ⇒ 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
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
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
|
.north_american?(some_code) ⇒ 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
59
60
61
|
# File 'lib/itu_codes.rb', line 59
def valid_code?(some_code)
country_codes.has_key?(some_code)
end
|