itu_codes 0.3.6 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,13 +1,15 @@
1
- require 'carmen'
1
+ require 'yaml'
2
2
 
3
3
  module ItuCodes
4
4
  module Helpers
5
+ ISO2ITU = YAML.load_file( File.expand_path(File.dirname(__FILE__)) + '/../data/iso_code_to_itu_country_names.yml')
6
+
5
7
  def self.country_code_lookup(country_name)
6
- Carmen::country_code(country_name)
8
+ ISO2ITU.key(country_name)
7
9
  end
8
10
 
9
11
  def self.country_name_lookup(iso_code)
10
- Carmen::country_name(iso_code)
12
+ ISO2ITU[iso_code]
11
13
  end
12
14
  end
13
15
  end
@@ -1,3 +1,3 @@
1
1
  module ItuCodes
2
- VERSION = "0.3.6"
2
+ VERSION = "0.4.1"
3
3
  end
data/lib/itu_codes.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # to-do: deal with headache codes:
2
2
  # 1 --> Non-US need to be separated by area code
3
3
  # 7 --> Kazakhstan!
4
- require 'carmen'
5
4
 
6
5
  require 'itu_codes/constants'
7
6
  require 'itu_codes/helpers'
@@ -10,123 +9,110 @@ require 'itu_codes/helpers'
10
9
 
11
10
  module ItuCodes
12
11
  class << self
12
+ def find_by_itu_code(code)
13
+ returner = lookup(code)
13
14
 
14
- def list
15
- Constants::ASSIGNED_COUNTRY_CODES
16
- end
17
-
18
- def lookup(code)
19
- cleaned = clean(code)
20
- list.find_all{ |hsh| hsh.keys.first == cleaned }.map do |k|
21
- k.values.first
22
- end.uniq
23
- end
24
-
25
- def find (code_or_name)
26
- if code_or_name.to_s.match /[^0-9]/ # non-numeric string passed
27
- find_by_name(code_or_name)
15
+ if returner.size <= 1
16
+ returner.first
28
17
  else
29
- cleaned = self.clean(code_or_name)
30
- find_by_itu_code(cleaned)
18
+ returner
31
19
  end
32
20
  end
33
21
 
34
- def find_by_itu_code(code)
35
- if american?(code) and code.length > 1
36
- returner = 'United States'
22
+ def find_by_name(name)
23
+ matching = country_codes.select do |k, v|
24
+ [*v].include? name
25
+ end || {}
26
+
27
+ returner = matching.keys
28
+
29
+ if returner.size <= 1
30
+ returner.first
37
31
  else
38
- returner = list.find_all do |record|
39
- record.has_key? code
40
- end.map { |k| k.values.first }.uniq
32
+ returner
41
33
  end
42
- returner.size <= 1 ? returner.shift : returner
43
34
  end
44
35
 
45
- def find_by_name(name)
46
- returner = list.find_all do |record|
47
- record.has_value? name
48
- end.map{ |k| k.keys.first }.uniq
49
- returner.size <= 1 ? returner.shift : returner
50
- end
51
-
52
- # ie. find_by_country_iso_code('US')
36
+ # lookup by ISO 3166 country code
37
+ # e.g. iso2itu('US')
38
+ # see : http://www.iso.org/iso/country_codes.htm
53
39
  def iso2itu(iso_code)
54
- [find_by_name( Helpers.country_name_lookup(iso_code) )].flatten.reject do |c|
55
- north_american?(c) and c.length > 1
56
- end.shift rescue nil
57
- end
58
-
59
- # this is tricky for North American destinations: '1' can be the US, Canada or another country
60
- def itu2iso(itu_code)
61
- lookup(itu_code).map do |country|
62
- Helpers.country_code_lookup(country)
63
- end.compact
40
+ country_name = Helpers.country_name_lookup(iso_code)
41
+ find_by_name(country_name)
64
42
  end
65
43
 
66
44
  # returns true for any valid ITU code
67
45
  # valid_code?(1) => true
68
46
  # valid_code?(1818) => false
69
47
  def valid_code?(some_code)
70
- if north_american?(some_code)
71
- some_code.length == 1
72
- else
73
- self.list.any? { |hsh| hsh.has_key?(some_code) }
74
- end
75
- end
76
-
77
- def north_america
78
- find_by_itu_code '1'
48
+ country_codes.has_key?(some_code)
79
49
  end
80
50
 
81
51
  def north_american?(some_code)
82
- some_code.to_s[0,1] == '1'
52
+ some_code[0,1] == '1'
83
53
  end
84
54
 
85
55
  # parse a destination code (probably with area code) to find the ITU code
86
56
  # ex: parse_code(1818) => 1
87
- def parse_code(some_code)
88
- some_code = clean(some_code)
89
- sub_index = (1..some_code.length).find do |len|
90
- valid_code? some_code[0,len]
57
+ def parse_code(some_number)
58
+ some_number = clean(some_number)
59
+ sub_index = (1..some_number.length).find do |len|
60
+ valid_code? some_number[0,len]
91
61
  end
92
- some_code[0,sub_index] unless sub_index.nil?
62
+ some_number[0,sub_index] unless sub_index.nil?
93
63
  end
94
64
 
95
65
  # parse a destination code (probably with area code) to find the number without the ITU code
96
66
  # ex: parse_number(18184443322) => 8184443322
97
- def parse_number(some_code)
98
- some_code = clean(some_code)
99
- sub_index = (1..some_code.length).find do |len|
100
- valid_code? some_code[0,len]
101
- end
102
- some_code[sub_index,some_code.length - sub_index] unless sub_index.nil?
67
+ def parse_number(some_number)
68
+ country_code = parse_code(some_number)
69
+ some_number.gsub(/^#{country_code}/) unless country_code.nil?
103
70
  end
104
71
 
105
72
  def compatriots?(some, other)
106
73
  both_valid = ! ( parse_code(some).nil? or parse_code(other).nil? )
107
- both_valid and ( parse_code(some) == parse_code(other) ) rescue nil
108
- end
109
74
 
110
- # def american?(some_code)
111
- # # for non-US North American codes, parse_code will return a 4 digit code
112
- # # for US, '1' will be returned
113
- # some_code = some_code.to_s
114
- # countries = lookup(some_code[0,4])
115
- # north_american?(some_code) and (countries.include?('United States') or countries.empty?)
116
- # end
75
+ if north_american?(some) && north_american?(other)
76
+ both_valid && !(lookup(some) & lookup(other)).empty?
77
+ else
78
+ some = parse_code(some)
79
+ other = parse_code(other)
80
+ both_valid && lookup(some) == lookup(other)
81
+ end
82
+ end
117
83
 
118
84
  def american?(some_code)
119
85
  # for non-US North American codes, parse_code will return a 4 digit code
120
86
  # for US, '1' will be returned
121
- some_code = some_code.to_s
122
- countries = lookup(some_code[0,4])
123
- north_american?(some_code) and (countries.include?('United States'))
87
+ countries = lookup(some_code[0,4]) || []
88
+ countries.include?('United States of America')
124
89
  end
125
90
 
126
91
  def canadian?(some_code)
127
- some_code = some_code.to_s
128
92
  countries = lookup(some_code[0,4])
129
- north_american?(some_code) and (countries.include?('Canada'))
93
+ north_american?(some_code) && (countries.include?('Canada'))
94
+ end
95
+
96
+
97
+ private
98
+
99
+ def north_american_codes
100
+ Constants::NORTH_AMERICAN_AREA_CODES
101
+ end
102
+
103
+ def country_codes
104
+ Constants::ASSIGNED_COUNTRY_CODES
105
+ end
106
+
107
+ def lookup(code)
108
+ cleaned = clean(code)
109
+ matching_countries = country_codes[cleaned] || []
110
+
111
+ matching_north_americans = north_american_codes.select do |k, v|
112
+ [*v].include?(code)
113
+ end || {}
114
+
115
+ [*matching_countries] + matching_north_americans.keys.compact
130
116
  end
131
117
 
132
118
  # converts input to string, then strips any non-numeric characters
@@ -134,5 +120,8 @@ module ItuCodes
134
120
  input.to_s.gsub(/[^0-9]/, '')
135
121
  end
136
122
 
123
+ def north_america
124
+ find_by_itu_code '1'
125
+ end
137
126
  end
138
127
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itu_codes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,40 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-01 00:00:00.000000000 Z
12
+ date: 2013-03-17 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: i18n
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: carmen
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :runtime
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
14
  - !ruby/object:Gem::Dependency
47
15
  name: rake
48
16
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +35,9 @@ executables: []
67
35
  extensions: []
68
36
  extra_rdoc_files: []
69
37
  files:
38
+ - lib/data/assigned_country_codes.yml
39
+ - lib/data/iso_code_to_itu_country_names.yml
40
+ - lib/data/north_american_area_codes.yml
70
41
  - lib/itu_codes/constants.rb
71
42
  - lib/itu_codes/helpers.rb
72
43
  - lib/itu_codes/version.rb