Module: ItuCodes
- Defined in:
- lib/itu_codes.rb,
lib/itu_codes/helpers.rb,
lib/itu_codes/version.rb,
lib/itu_codes/constants.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.4.9"
Class Method Summary collapse
- .american?(some_code) ⇒ Boolean
- .canadian?(some_code) ⇒ Boolean
- .compatriots?(some, other) ⇒ Boolean
-
.country_for(number) ⇒ Object
number is a full or partial number.
- .eurasian?(some_code) ⇒ Boolean
-
.find_by_itu_code(code) ⇒ Object
Must pass a valid ITU code.
-
.find_by_name(name) ⇒ Object
Passed name must match exact name specified in ITU spec see: lib/data/assigned_country_codes.yml.
-
.iso2itu(iso_code) ⇒ Object
lookup by ISO 3166 alpha-2 country code e.g.
-
.itu2iso(number) ⇒ Object
number is a full or partial number.
-
.kazakh?(some_code) ⇒ Boolean
Kazakhstan country code will start with either 76 or 77 see: www.itu.int/oth/T020200006F/en www.itu.int/dms_pub/itu-t/oth/02/02/T020200006F0001PDFE.pdf NOTE: apparently, some numbers are still jointly used by Kazakhstan and Russia 7000-0009, 7100-7199, 7200-7299, 7800-7809, 7881-7899 ItuCodes reports these as Russian codes for the time being.
- .north_american?(some_code) ⇒ Boolean
-
.north_american_area_code_for(some_number) ⇒ Object
parse a north american full number and return the area code returns nil if the number is not north american return 1, if can’t find an area code in Constants::NORTH_AMERICAN_AREA_CODES ex: north_american_area_code_for(“1-264-9568543”) => 1264.
-
.parse_code(some_number) ⇒ Object
parse a destination code (probably with area code) to find the ITU code ex: parse_code(1818) => 1.
-
.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.
-
.russian?(some_code) ⇒ Boolean
Russian codes start with 7 and are not codes defined for Kazakhstan.
-
.valid_code?(some_code) ⇒ Boolean
returns true for any valid ITU code valid_code?(1) => true valid_code?(1818) => false.
Class Method Details
.american?(some_code) ⇒ Boolean
154 155 156 |
# File 'lib/itu_codes.rb', line 154 def american?(some_code) [* country_for(some_code) ].include?('United States of America') end |
.canadian?(some_code) ⇒ Boolean
158 159 160 |
# File 'lib/itu_codes.rb', line 158 def canadian?(some_code) [* country_for(some_code) ].include?('Canada') end |
.compatriots?(some, other) ⇒ Boolean
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/itu_codes.rb', line 132 def compatriots?(some, other) both_valid = ! ( parse_code(some).nil? || parse_code(other).nil? ) if north_american?(some) && north_american?(other) both_valid && !([*country_for(some)] & [*country_for(other)]).empty? elsif eurasian?(some) && eurasian?(other) both_valid && !([*country_for(some)] & [*country_for(other)]).empty? else some = parse_code(some) other = parse_code(other) both_valid && some === other end end |
.country_for(number) ⇒ Object
number is a full or partial number
8 9 10 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/itu_codes.rb', line 8 def country_for(number) parsed = parse_code(number) return if parsed.nil? matching_countries = if north_american?(parsed) hsh = north_american_codes.select do |k, v| v.any? do |na_area_code| na_area_code =~ /^#{number[0,4]}/ end end # ruby 1.8 support ItuCodes::Helpers.keys_from_hash(hsh) elsif eurasian?(parsed) temp = [] temp << "Kazakhstan (Republic of)" if kazakh?(number[0,2]) temp << "Russian Federation" if russian?(number[0,2]) temp else country_codes[parsed] end if matching_countries.is_a?(Array) && matching_countries.length === 1 matching_countries.first else matching_countries end end |
.eurasian?(some_code) ⇒ Boolean
150 151 152 |
# File 'lib/itu_codes.rb', line 150 def eurasian?(some_code) some_code[0,1] == '7' end |
.find_by_itu_code(code) ⇒ Object
Must pass a valid ITU code
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/itu_codes.rb', line 39 def find_by_itu_code(code) if valid_code?(code) returner = country_for(code) if returner.size <= 1 returner.first else returner end end end |
.find_by_name(name) ⇒ Object
Passed name must match exact name specified in ITU spec see: lib/data/assigned_country_codes.yml
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/itu_codes.rb', line 53 def find_by_name(name) matching = country_codes.select do |k, v| [*v].include? name end || {} # ruby 1.8 returner = ItuCodes::Helpers.keys_from_hash(matching) if returner.size <= 1 returner.first else returner end end |
.iso2itu(iso_code) ⇒ Object
lookup by ISO 3166 alpha-2 country code e.g. find_by_iso_code(‘US’) see : www.iso.org/iso/country_codes.htm
71 72 73 74 |
# File 'lib/itu_codes.rb', line 71 def iso2itu(iso_code) country_name = Helpers.country_name_lookup(iso_code) find_by_name(country_name) end |
.itu2iso(number) ⇒ Object
number is a full or partial number
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/itu_codes.rb', line 77 def itu2iso(number) name = country_for(number) 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 |
.kazakh?(some_code) ⇒ Boolean
Kazakhstan country code will start with either 76 or 77 see: www.itu.int/oth/T020200006F/en
http://www.itu.int/dms_pub/itu-t/oth/02/02/T020200006F0001PDFE.pdf
NOTE: apparently, some numbers are still jointly used by Kazakhstan and Russia
7000-0009, 7100-7199, 7200-7299, 7800-7809, 7881-7899
ItuCodes reports these as Russian codes for the time being
178 179 180 181 182 183 184 |
# File 'lib/itu_codes.rb', line 178 def kazakh?(some_code) if some_code.length < 2 eurasian?(some_code) else %(77 76).include?(some_code[0,2]) end end |
.north_american?(some_code) ⇒ Boolean
146 147 148 |
# File 'lib/itu_codes.rb', line 146 def north_american?(some_code) some_code[0,1] == '1' end |
.north_american_area_code_for(some_number) ⇒ Object
parse a north american full number and return the area code returns nil if the number is not north american return 1, if can’t find an area code in Constants::NORTH_AMERICAN_AREA_CODES
ex: north_american_area_code_for("1-264-9568543") => 1264
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/itu_codes.rb', line 110 def north_american_area_code_for(some_number) some_number = clean(some_number) itu_code = parse_code(some_number) return nil if itu_code.nil? || !north_american?(itu_code) code = itu_code north_american_codes.each { |_, v| code = some_number[0,4] if v.include?(some_number[0,4]) } return code end |
.parse_code(some_number) ⇒ Object
parse a destination code (probably with area code) to find the ITU code
ex: parse_code(1818) => 1
98 99 100 101 102 103 104 |
# File 'lib/itu_codes.rb', line 98 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
126 127 128 129 130 |
# File 'lib/itu_codes.rb', line 126 def parse_number(some_number) some_number = clean(some_number) country_code = parse_code(some_number) some_number[country_code.length,some_number.length] unless country_code.nil? end |
.russian?(some_code) ⇒ Boolean
Russian codes start with 7 and are not codes defined for Kazakhstan
164 165 166 167 168 169 170 |
# File 'lib/itu_codes.rb', line 164 def russian?(some_code) if some_code.length < 2 eurasian?(some_code) else eurasian?(some_code) && !kazakh?(some_code) end end |
.valid_code?(some_code) ⇒ Boolean
returns true for any valid ITU code valid_code?(1) => true valid_code?(1818) => false
92 93 94 |
# File 'lib/itu_codes.rb', line 92 def valid_code?(some_code) country_codes.has_key?(some_code) end |