itu_codes 0.4.8 → 0.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -33,7 +33,7 @@ Some complicating factors when dealing with calling codes:
33
33
  # ItuCodes.iso2itu(iso_2_letter_country_code)
34
34
  # ItuCodes.itu2iso(exact_itu_code)
35
35
  # ItuCodes.compatriots?(full_or_partial_number1, full_or_partial_number2)
36
-
36
+ # ItuCodes.north_american_area_code_for
37
37
 
38
38
  # Examples
39
39
 
@@ -71,15 +71,24 @@ ItuCodes.valid_code? '7'
71
71
  ItuCodes.parse_code '18185558888'
72
72
  # => 1
73
73
 
74
+ # non-numeric characters are ignored
75
+ ItuCodes.parse_code '1 (818) 555-8888'
76
+ # => "1"
77
+
74
78
  ItuCodes.parse_code '822'
75
79
  # => "82"
76
80
 
77
81
  ItuCodes.parse_code '4'
78
82
  # => nil
79
83
 
84
+
80
85
  ItuCodes.parse_number '18185558888'
81
86
  # => "8185558888"
82
87
 
88
+ # non-numeric characters are ignored
89
+ ItuCodes.parse_number '1 (818) 555-8888'
90
+ # => "8185558888"
91
+
83
92
  # Convert from and to ISO 2-letter country codes:
84
93
  ItuCodes.iso2itu('US')
85
94
  # => "1"
@@ -113,6 +122,13 @@ ItuCodes.compatriots? '1984', '1985'
113
122
  ItuCodes.compatriots? '1264', '1818'
114
123
  # => false
115
124
 
125
+ # parse the area code for North American numbers:
126
+ ItuCodes.north_american_area_code_for '18185551234'
127
+ # => '1818'
128
+
129
+ # returns nil if the passed number is not North American
130
+ ItuCodes.north_american_area_code_for '332233'
131
+ # => nil
116
132
  ```
117
133
 
118
134
  [1]: http://www.itu.int/pub/T-SP-E.164D-11-2011
@@ -9,13 +9,16 @@ module ItuCodes
9
9
  parsed = parse_code(number)
10
10
 
11
11
  return if parsed.nil?
12
-
12
+
13
13
  matching_countries = if north_american?(parsed)
14
- north_american_codes.select do |k, v|
14
+ hsh = north_american_codes.select do |k, v|
15
15
  v.any? do |na_area_code|
16
16
  na_area_code =~ /^#{number[0,4]}/
17
17
  end
18
- end.keys
18
+ end
19
+
20
+ # ruby 1.8 support
21
+ ItuCodes::Helpers.keys_from_hash(hsh)
19
22
  elsif eurasian?(parsed)
20
23
  temp = []
21
24
  temp << "Kazakhstan (Republic of)" if kazakh?(number[0,2])
@@ -52,7 +55,8 @@ module ItuCodes
52
55
  [*v].include? name
53
56
  end || {}
54
57
 
55
- returner = matching.keys
58
+ # ruby 1.8
59
+ returner = ItuCodes::Helpers.keys_from_hash(matching)
56
60
 
57
61
  if returner.size <= 1
58
62
  returner.first
@@ -6,7 +6,8 @@ module ItuCodes
6
6
 
7
7
  # country name is the one used by ITU, not ISO
8
8
  def self.country_code_lookup(country_name)
9
- codes = ISO2ITU.select{|k,v| v === country_name}.keys
9
+ hsh = ISO2ITU.select{|k,v| v === country_name}
10
+ codes = ItuCodes::Helpers.keys_from_hash(hsh)
10
11
 
11
12
  if codes.empty?
12
13
  nil
@@ -20,5 +21,14 @@ module ItuCodes
20
21
  def self.country_name_lookup(iso_code)
21
22
  ISO2ITU[iso_code]
22
23
  end
24
+
25
+ # ruby 1.8 support, since Hash#select returns an array
26
+ def self.keys_from_hash(hsh_or_array)
27
+ if hsh_or_array.is_a?(Array)
28
+ Hash[hsh_or_array]
29
+ else
30
+ hsh_or_array
31
+ end.keys
32
+ end
23
33
  end
24
- end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module ItuCodes
2
- VERSION = "0.4.8"
3
- end
2
+ VERSION = "0.4.9"
3
+ 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.4.8
4
+ version: 0.4.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-19 00:00:00.000000000 Z
12
+ date: 2013-10-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  version: 1.3.7
65
65
  requirements: []
66
66
  rubyforge_project:
67
- rubygems_version: 1.8.24
67
+ rubygems_version: 1.8.23
68
68
  signing_key:
69
69
  specification_version: 3
70
70
  summary: Helpers for dealing with world calling codes.