globalphone_dbgen 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Sam Stephenson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,163 @@
1
+ # GlobalPhone
2
+
3
+ GlobalPhone parses, validates, and formats local and international phone numbers according to the [E.164 standard](http://en.wikipedia.org/wiki/E.164).
4
+
5
+ **Store and display phone numbers in your app.** Accept phone number input in national or international format. Convert phone numbers to international strings (`+13125551212`) for storage and retrieval. Present numbers in national format (`(312) 555-1212`) in your UI.
6
+
7
+ **Designed with the future in mind.** GlobalPhone uses format specifications from Google's open-source [libphonenumber](http://code.google.com/p/libphonenumber/) database. No need to upgrade the library when a new phone format is introduced—just generate a new copy of the database and check it into your app.
8
+
9
+ **Pure Ruby. No dependencies.** GlobalPhone is designed for Ruby 1.9.3 and up. (Works in 1.8.7, too—just bring your own `json` gem.)
10
+
11
+ ## Installation
12
+
13
+ 1. Add the `global_phone` gem to your app. For example, using Bundler:
14
+
15
+ $ echo "gem 'global_phone'" >> Gemfile
16
+ $ bundle install
17
+
18
+ 2. Use `global_phone_dbgen` to convert Google's libphonenumber `PhoneNumberMetaData.xml` file into a JSON database for GlobalPhone.
19
+
20
+ $ gem install global_phone_dbgen
21
+ $ global_phone_dbgen > db/global_phone.json
22
+
23
+ 3. Tell GlobalPhone where to find the database. For example, in a Rails app, create an initializer in `config/initializers/global_phone.rb`:
24
+
25
+ ```ruby
26
+ require 'global_phone'
27
+ GlobalPhone.db_path = Rails.root.join('db/global_phone.json')
28
+ ```
29
+
30
+ ## Examples
31
+
32
+ Parse an international number string into a `GlobalPhone::Number` object:
33
+
34
+ ```ruby
35
+ number = GlobalPhone.parse('+1-312-555-1212')
36
+ # => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=1 name=US> national_string="3125551212">
37
+ ```
38
+
39
+ Query the country code and likely territory name of the number:
40
+
41
+ ```ruby
42
+ number.country_code
43
+ # => "1"
44
+
45
+ number.territory.name
46
+ # => "US"
47
+ ```
48
+
49
+ Present the number in national and international formats:
50
+
51
+ ```ruby
52
+ number.national_format
53
+ # => "(312) 555-1212"
54
+
55
+ number.international_format
56
+ # => "+1 312-555-1212"
57
+ ```
58
+
59
+ Is the number valid? (Note: this is not definitive. For example, the number here is "valid" by format, but there are no US numbers that start with 555. The `valid?` method may return false positives, but *should not* return false negatives unless the database is out of date.)
60
+
61
+ ```ruby
62
+ number.valid?
63
+ # => true
64
+ ```
65
+
66
+ Get the number's normalized E.164 international string:
67
+
68
+ ```ruby
69
+ number.international_string
70
+ # => "+13125551212"
71
+ ```
72
+
73
+ Parse a number in national format for a given territory:
74
+
75
+ ```ruby
76
+ number = GlobalPhone.parse("(0) 20-7031-3000", :gb)
77
+ # => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=44 name=GB> national_string="2070313000">
78
+ ```
79
+
80
+ Parse an international number using a territory's international dialing prefix:
81
+
82
+ ```ruby
83
+ number = GlobalPhone.parse("00 1 3125551212", :gb)
84
+ # => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=1 name=US> national_string="3125551212">
85
+ ```
86
+
87
+ Set the default territory to Great Britain (territory names are [ISO 3166-1 Alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) codes):
88
+
89
+ ```ruby
90
+ GlobalPhone.default_territory_name = :gb
91
+ # => :gb
92
+
93
+ GlobalPhone.parse("(0) 20-7031-3000")
94
+ # => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=44 name=GB> national_string="2070313000">
95
+ ```
96
+
97
+ Shortcuts for validating a phone number:
98
+
99
+ ```ruby
100
+ GlobalPhone.validate("+1 312-555-1212")
101
+ # => true
102
+
103
+ GlobalPhone.validate("+442070313000")
104
+ # => true
105
+
106
+ GlobalPhone.validate("(0) 20-7031-3000")
107
+ # => false
108
+
109
+ GlobalPhone.validate("(0) 20-7031-3000", :gb)
110
+ # => true
111
+ ```
112
+
113
+ Shortcuts for normalizing a phone number in E.164 format:
114
+
115
+ ```ruby
116
+ GlobalPhone.normalize("(312) 555-1212")
117
+ # => "+13125551212"
118
+
119
+ GlobalPhone.normalize("+442070313000")
120
+ # => "+442070313000"
121
+
122
+ GlobalPhone.normalize("(0) 20-7031-3000")
123
+ # => nil
124
+
125
+ GlobalPhone.normalize("(0) 20-7031-3000", :gb)
126
+ # => "+442070313000"
127
+ ```
128
+
129
+ ## Caveats
130
+
131
+ GlobalPhone currently does not parse emergency numbers or SMS short code numbers.
132
+
133
+ Validation is not definitive and may return false positives, but *should not* return false negatives unless the database is out of date.
134
+
135
+ Territory heuristics are imprecise. Parsing a number will usually result in the territory being set to the primary territory of the region. For example, Canadian numbers will be parsed with a territory of `US`. (In most cases this does not matter, but if your application needs to perform geolocation using phone numbers, GlobalPhone may not be a good fit.)
136
+
137
+ ## Development
138
+
139
+ The GlobalPhone source code is [hosted on GitHub](https://github.com/sstephenson/global_phone). You can check out a copy of the latest code using Git:
140
+
141
+ $ git clone https://github.com/sstephenson/global_phone.git
142
+
143
+ If you've found a bug or have a question, please open an issue on the [issue tracker](https://github.com/sstephenson/global_phone/issues). Or, clone the GlobalPhone repository, write a failing test case, fix the bug, and submit a pull request.
144
+
145
+ GlobalPhone is heavily inspired by Andreas Gal's [PhoneNumber.js](https://github.com/andreasgal/PhoneNumber.js) library.
146
+
147
+ ### Version History
148
+
149
+ **1.0.1** (May 29, 2013)
150
+
151
+ * GlobalPhone::Number#to_s returns the E.164 international string.
152
+ * Ensure GlobalPhone::Number always returns strings for #national_format, #international_format, and #international_string, regardless of validity.
153
+ * Relax format restrictions to more loosely match available national number patterns.
154
+
155
+ **1.0.0** (May 28, 2013)
156
+
157
+ * Initial public release.
158
+
159
+ ### License
160
+
161
+ Copyright &copy; 2013 Sam Stephenson
162
+
163
+ Released under the MIT license. See [`LICENSE`](LICENSE) for details.
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ require 'global_phone/database_generator'
3
+ require 'open-uri'
4
+
5
+ REMOTE_URL = "http://libphonenumber.googlecode.com/svn/trunk/resources/PhoneNumberMetadata.xml"
6
+
7
+ def usage
8
+ warn "Usage: #$0 [--compact] [--test] [<filename> | <url>]"
9
+ end
10
+
11
+ def help
12
+ warn <<-EOS
13
+
14
+ Generates a database for the Ruby GlobalNumber library in JSON format
15
+ and writes it to standard output.
16
+
17
+ Specify either a local path or URL pointing to a copy of Google's
18
+ libphonenumber PhoneNumberMetaData.xml file.
19
+
20
+ Omit the filename argument to download and use the latest version of
21
+ Google's database from:
22
+ #{REMOTE_URL}
23
+
24
+ Options:
25
+ --compact Strip all whitespace from the JSON output
26
+ --test Generate example phone number fixtures for smoke tests
27
+
28
+ EOS
29
+ end
30
+
31
+ path = REMOTE_URL
32
+ method = :record_data
33
+ compact = false
34
+
35
+ while arg = ARGV.shift
36
+ case arg
37
+ when "-c", "--compact"
38
+ compact = true
39
+ when "-t", "--test"
40
+ method = :test_cases
41
+ when "-h", "--help"
42
+ usage
43
+ help
44
+ exit
45
+ when /^-/
46
+ warn "#$0: unknown option `#{arg}'"
47
+ usage
48
+ exit 1
49
+ else
50
+ path = arg
51
+ break
52
+ end
53
+ end
54
+
55
+ generator = GlobalPhone::DatabaseGenerator.load(open(path).read)
56
+ result = generator.send(method)
57
+
58
+ if compact
59
+ puts JSON.generate(result)
60
+ else
61
+ puts JSON.pretty_generate(result)
62
+ end
@@ -0,0 +1,149 @@
1
+ require 'json'
2
+ require 'nokogiri'
3
+
4
+ module GlobalPhone
5
+ class DatabaseGenerator
6
+ VERSION = '1.0.0'
7
+
8
+ def self.load_file(filename)
9
+ load(File.read(filename))
10
+ end
11
+
12
+ def self.load(xml)
13
+ new(Nokogiri.XML(xml))
14
+ end
15
+
16
+ attr_reader :doc
17
+
18
+ def initialize(doc)
19
+ @doc = doc
20
+ end
21
+
22
+ def test_cases
23
+ @test_cases ||= territory_nodes.map do |node|
24
+ example_numbers_for_territory_node(node)
25
+ end.flatten(1)
26
+ end
27
+
28
+ def record_data
29
+ @record_data ||= territory_nodes_by_region.map do |country_code, territory_nodes|
30
+ truncate(compile_region(territory_nodes, country_code))
31
+ end
32
+ end
33
+
34
+ def inspect
35
+ "#<#{self.class.name} (#{doc.search("*").size} elements)>"
36
+ end
37
+
38
+ protected
39
+ def territory_nodes
40
+ doc.search("territory")
41
+ end
42
+
43
+ def territory_nodes_by_region
44
+ territory_nodes.group_by { |node| node["countryCode"] }
45
+ end
46
+
47
+ def territory_name(node)
48
+ node["id"]
49
+ end
50
+
51
+ def example_numbers_for_territory_node(node)
52
+ name = territory_name(node)
53
+ return [] if name == "001"
54
+ node.search(example_numbers_selector).map { |node| [node.text, name] }
55
+ end
56
+
57
+ def example_numbers_selector
58
+ "./*[not(" + example_number_types_to_exclude.map do |type|
59
+ "self::#{type}"
60
+ end.join(" or ") + ")]/exampleNumber"
61
+ end
62
+
63
+ def example_number_types_to_exclude
64
+ %w( emergency shortCode )
65
+ end
66
+
67
+ def compile_region(territory_nodes, country_code)
68
+ territories, main_territory_node = compile_territories(territory_nodes)
69
+ formats = compile_formats(territory_nodes)
70
+
71
+ [
72
+ country_code,
73
+ formats,
74
+ territories,
75
+ main_territory_node["internationalPrefix"],
76
+ main_territory_node["nationalPrefix"],
77
+ squish(main_territory_node["nationalPrefixForParsing"]),
78
+ squish(main_territory_node["nationalPrefixTransformRule"])
79
+ ]
80
+ end
81
+
82
+ def compile_territories(territory_nodes)
83
+ territories = []
84
+ main_territory_node = territory_nodes.first
85
+
86
+ territory_nodes.each do |node|
87
+ territory = truncate(compile_territory(node))
88
+ if node["mainCountryForCode"]
89
+ main_territory_node = node
90
+ territories.unshift(territory)
91
+ else
92
+ territories.push(territory)
93
+ end
94
+ end
95
+
96
+ [territories, main_territory_node]
97
+ end
98
+
99
+ def compile_territory(node)
100
+ [
101
+ territory_name(node),
102
+ pattern(node, "generalDesc possibleNumberPattern"),
103
+ pattern(node, "generalDesc nationalNumberPattern"),
104
+ squish(node["nationalPrefixFormattingRule"])
105
+ ]
106
+ end
107
+
108
+ def compile_formats(territory_nodes)
109
+ format_nodes_for(territory_nodes).map do |node|
110
+ truncate(compile_format(node))
111
+ end
112
+ end
113
+
114
+ def compile_format(node)
115
+ [
116
+ node["pattern"],
117
+ text_or_nil(node, "format"),
118
+ pattern(node, "leadingDigits"),
119
+ node["nationalPrefixFormattingRule"],
120
+ text_or_nil(node, "intlFormat")
121
+ ]
122
+ end
123
+
124
+ def format_nodes_for(territory_nodes)
125
+ territory_nodes.map do |node|
126
+ node.search("availableFormats numberFormat").to_a
127
+ end.flatten
128
+ end
129
+
130
+ def truncate(array)
131
+ array.dup.tap do |result|
132
+ result.pop while result.any? && result.last.nil?
133
+ end
134
+ end
135
+
136
+ def squish(string)
137
+ string.gsub(/\s+/, "") if string
138
+ end
139
+
140
+ def pattern(node, selector)
141
+ squish(text_or_nil(node, selector))
142
+ end
143
+
144
+ def text_or_nil(node, selector)
145
+ nodes = node.search(selector)
146
+ nodes.empty? ? nil : nodes.text
147
+ end
148
+ end
149
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: globalphone_dbgen
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Stephenson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.5'
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: '1.5'
30
+ description: Provides a global_phone_dbgen command to generate databases for the GlobalPhone
31
+ library.
32
+ email:
33
+ - sstephenson@gmail.com
34
+ executables:
35
+ - global_phone_dbgen
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - README.md
40
+ - LICENSE
41
+ - lib/global_phone/database_generator.rb
42
+ - bin/global_phone_dbgen
43
+ homepage: https://github.com/Lundalogik/globalphone_fork
44
+ licenses:
45
+ - MIT
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.24
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Generate databases for use with the GlobalPhone library
68
+ test_files: []