phones 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phones.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jarred Sumner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Phones
2
+
3
+ Phones is a barebones phone parsing and formatting library. If the phone is number is outside of the United States, it must include the country code prefixed with a "+"
4
+
5
+ It can parse phone numbers like:
6
+ - (555) 555-5555
7
+ - 555-555-5555
8
+ - 555.555.5555
9
+ - 555-555.5555
10
+ - 19252008843
11
+ - 1-(925)-200-8843
12
+ - +49 69 6900
13
+ - +49696900
14
+ - +19252008843
15
+
16
+ And any combination between those.
17
+
18
+ ## Installation
19
+
20
+ First, install it:
21
+ ```bash
22
+ gem install phones
23
+ ```
24
+
25
+ In your Gemfile:
26
+ ```ruby
27
+ gem 'phones', :require => 'phones'
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ It adds a ```to_phone``` method to ```String```, so you can just do:
33
+ ```ruby
34
+ "1-(925)-200-8843".to_phone
35
+ => +19252008843
36
+ ```
37
+
38
+ Pretty printing phone numbers:
39
+ ```ruby
40
+ "1-(925)-200-8843".to_phone.pretty
41
+ => "(925) 200-8843"
42
+ ```
43
+
44
+ Country code, area code, local code:
45
+ ```ruby
46
+ phone = "1-(925)-200-8843".to_phone
47
+ => +19252008843
48
+ phone.country_code
49
+ => "+1"
50
+ phone.area_code
51
+ => "925"
52
+ phone.local_code
53
+ => "2008843"
54
+ ```
55
+
56
+ Note: ```area_code``` and ```local_code``` is currently only supported for US phone numbers. International phone numbers will have a ```nil``` area_code and ```local_code``` will contain the rest of the phone number. ```country_code``` should work for every country code.
57
+
58
+ It'll return nil if it couldn't detect a valid phone number.
59
+
60
+
61
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,80 @@
1
+ # Supported formats (ignore initial dash):
2
+ # - (555) 555-5555
3
+ # - 555-555-5555
4
+ # - 555.555.5555
5
+ # - 555 555 5555
6
+ # - +1 555 555 5555
7
+ # - +44 844 335 1801
8
+ # - 0844 335 1801
9
+ # - +49696900
10
+ # - +19252008843
11
+ # - +448443351801
12
+
13
+ require 'phones/phone'
14
+ module Phones
15
+ class Parser
16
+ IGNORABLE_CHARACTERS = /[a-zA-Z]|\-|\s|\.|\(|\)|\;|\:|\*|\&|\%|\$|\#|\@|\!/.freeze
17
+ def self.parse(options)
18
+ # Let's figure out what the phone number is
19
+ if options.class == Hash
20
+ @number = options[:phone] || options[:number]
21
+ elsif options.class == String
22
+ @number = options
23
+ else
24
+ raise ArgumentError, "Pass a hash of options including a \"number\" key, or just a phone number"
25
+ end
26
+
27
+ # Now, let's remove all the characters we can ignore
28
+ strip!
29
+
30
+ # "+19252008843" or "19252008843" or "9252008843" are all common
31
+ @phone = try_without_delimiters!
32
+
33
+ @phone
34
+ end
35
+
36
+ def self.number
37
+ @number
38
+ end
39
+
40
+ private
41
+
42
+ def self.try_without_delimiters!
43
+ parts = []
44
+ # Let's find out if there is a country code
45
+ country_code = "+1"
46
+ # It's easiest to tell when it starts with a plus, so let's start with that
47
+ if self.number[0] == "+"
48
+ # Estados Unidos!
49
+ if self.number[1] == '1' && self.number.length == "+19252008843".length
50
+ parts = [self.number[2..4], self.number[5..-1]]
51
+ # 3 Digit country code?
52
+ elsif COUNTRY_CODES.include?(self.number[1..4])
53
+ country_code = self.number[0..4]
54
+ parts = [nil, self.number[5..-1]]
55
+ # 2 Digit country code?
56
+ elsif COUNTRY_CODES.include?(self.number[1..2])
57
+ country_code = self.number[0..2]
58
+ parts = [nil, self.number[4..-1]]
59
+ else
60
+ # IDK, man
61
+ raise ArgumentError, "Can't parse phone number"
62
+ end
63
+ elsif self.number[0] == '1' && self.number.length == "19252008843".length
64
+ parts = [self.number[1..3], self.number[4..-1]]
65
+ elsif self.number.length == "9252008843".length
66
+ parts = [self.number[0..2], self.number[3..-1]]
67
+ end
68
+
69
+ Phones::Phone.new(:country_code => country_code, :area_code => parts[0], :local_code => parts[1])
70
+ rescue
71
+ end
72
+
73
+ def self.strip!
74
+ self.number.gsub!(IGNORABLE_CHARACTERS, '')
75
+ end
76
+
77
+ # Source: http://en.wikipedia.org/wiki/List_of_country_calling_codes
78
+ COUNTRY_CODES = %w(20 210 211 212 213 216 218 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 260 261 262 263 264 265 266 267 268 269 27 290 291 297 298 299 30 31 32 33 34 350 351 352 353 354 355 356 357 358 359 36 370 371 372 373 374 375 376 377 378 379 380 381 382 385 386 387 388 389 40 41 420 421 423 43 44 45 46 47 48 49 500 501 502 503 504 505 506 507 508 509 51 52 53 54 55 56 57 58 590 591 592 593 594 595 596 597 598 599 60 61 62 63 64 65 66 670 672 673 674 675 676 677 678 679 680 681 682 683 685 686 687 688 689 690 691 692 800 808 81 82 84 850 852 853 855 856 86 870 878 880 881 882 883 886 888 90 91 92 93 94 95 960 961 962 963 964 965 966 967 968 970 971 972 973 974 975 976 977 979 98 991 992 993 994 995 996 998).freeze
79
+ end
80
+ end
@@ -0,0 +1,66 @@
1
+ module Phones
2
+ class Phone
3
+ RESTRICTED_CHARACTERS = /\-|\(|\)|\.|\s|[a-zA-Z]/.freeze
4
+ attr_reader :country_code, :area_code, :local_code
5
+
6
+ def initialize(opts = {})
7
+ self.country_code = opts[:country_code]
8
+ self.area_code = opts[:area_code]
9
+ self.local_code = opts[:local_code]
10
+ end
11
+
12
+ def country_code=(val)
13
+ @country_code = val
14
+ validate_country_code!
15
+ end
16
+
17
+ def area_code=(val)
18
+ @area_code = val
19
+ validate_area_code!
20
+ end
21
+
22
+ def local_code=(val)
23
+ if val.class == Array
24
+ @local_code = val.join("")
25
+ else
26
+ @local_code = val
27
+ end
28
+ validate_local_code!
29
+ end
30
+
31
+ def pretty
32
+ if self.united_states?
33
+ "(#{self.area_code}) #{self.local_code[0..2]}-#{self.local_code[3..-1]}"
34
+ else
35
+ self.to_s
36
+ end
37
+ end
38
+
39
+ def to_s
40
+ "#{self.country_code}#{self.area_code}#{self.local_code}"
41
+ end
42
+
43
+ def united_states?
44
+ self.country_code == "+1"
45
+ end
46
+
47
+ private
48
+
49
+ # We know that this isn't a phone number because certain characters don't exist in phone numbers.
50
+ # These validations check for that.
51
+
52
+ def validate_country_code!
53
+ raise ArgumentError, "Country code: #{@country_code} contains restricted characters" if self.country_code =~ RESTRICTED_CHARACTERS && self.country_code.length > 3
54
+ end
55
+
56
+ def validate_area_code!
57
+ raise ArgumentError, "Area code: #{@area_code} contains restricted characters" if self.area_code =~ RESTRICTED_CHARACTERS
58
+ end
59
+
60
+ def validate_local_code!
61
+ raise ArgumentError, "Local code: #{@local_code} contains restricted characters" if self.local_code =~ RESTRICTED_CHARACTERS
62
+ raise ArugmentError, "Local code cannot be blank" if self.local_code.nil? || self.local_code.length == 0
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Phones
2
+ VERSION = "0.0.1"
3
+ end
data/lib/phones.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "phones/version"
2
+ require 'phones/parser'
3
+ require 'phones/phone'
4
+
5
+ module Phones
6
+ end
7
+
8
+ class String
9
+ def to_phone
10
+ Phones::Parser.parse(self)
11
+ end
12
+ end
13
+
14
+ class NilClass
15
+ def to_phone
16
+ nil
17
+ end
18
+ end
data/phones.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'phones/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "phones"
8
+ gem.version = Phones::VERSION
9
+ gem.authors = ["Jarred Sumner"]
10
+ gem.email = ["jarred@jarredsumner.com"]
11
+ gem.description = %q{Phones is a barebones phone parsing and formatting library}
12
+ gem.summary = %q{Phones is a barebones phone parsing and formatting library}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phones
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jarred Sumner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Phones is a barebones phone parsing and formatting library
15
+ email:
16
+ - jarred@jarredsumner.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/phones.rb
27
+ - lib/phones/parser.rb
28
+ - lib/phones/phone.rb
29
+ - lib/phones/version.rb
30
+ - phones.gemspec
31
+ homepage: ''
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.25
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Phones is a barebones phone parsing and formatting library
55
+ test_files: []