phoney 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -1
- data/README.rdoc +6 -55
- data/lib/phoney.rb +7 -68
- data/lib/phoney/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df8b354ee38ecd41a897ac6b3feff7a24ebb76e7d5ae2bf0741befab07e492ef
|
4
|
+
data.tar.gz: d0be4c600e60567460ad5bcfbfd2605918d5498198b43016d8ec6b29079f1573
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 964df0850ab58f3a62384a0896ec08dca432189deeefdbdcbd97b366a6a4e3d3baab3042a74af515dd66f0e65cbd65d66b981dac6a810440f5163223de22a4a1
|
7
|
+
data.tar.gz: c2fd3d4e309e842897c2494491e8ff7a998ecc096ceb4ff4f4f19085c62090760362705764c4d0532f4142e6a95df7901323a70a775774b75d2d285d56a2dbb2
|
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -14,66 +14,17 @@ Source:: <tt>git clone git://github.com/habermann24/phoney.git</tt>
|
|
14
14
|
|
15
15
|
require 'phoney'
|
16
16
|
# region defaults to US
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
pn.country_code # "1"
|
21
|
-
pn.number # "1234567"
|
22
|
-
|
17
|
+
Phoney.format("+17041234567")
|
18
|
+
# => "+1 (704) 123-4567"
|
19
|
+
|
23
20
|
* Deals with many specific region formatting rules (e.g. DE)
|
24
21
|
|
25
22
|
require 'phoney'
|
26
|
-
|
27
|
-
Phoney.region = :de
|
28
|
-
|
29
|
-
pn = Phoney.new("04105456789")
|
30
|
-
pn.to_s # "+49 4105 456789"
|
31
|
-
pn.area_code # "4105"
|
32
|
-
pn.country_code # "49"
|
33
|
-
pn.number # "456789"
|
34
|
-
|
35
|
-
== Creating Phoney instances
|
36
|
-
|
37
|
-
Phoney gives you a Phoney class that wraps all the logic of phone number parsing and representation.
|
38
|
-
The default region phoney uses for formatting is the US-region format. So if you want to parse phone numbers from a different country, you have to set Phoney.region or pass the region code every time!
|
39
|
-
|
40
|
-
Phoney.region = :us
|
41
23
|
|
42
|
-
|
43
|
-
Phoney.new("7041231234") # uses region :us
|
44
|
-
Phoney.new("01805708090", :de) # uses region :de
|
45
|
-
|
46
|
-
Or instead of parsing a string, you can provide a hash for the first parameter:
|
47
|
-
Phoney.new(:number => "1231234", :area_code => "704", :country_code => "1")
|
48
|
-
|
49
|
-
# falls back to US region, so also uses "1" for <tt>country_code</tt>
|
50
|
-
Phoney.new(:number => "1231234", :area_code => "704")
|
51
|
-
|
52
|
-
== Formatting
|
53
|
-
|
54
|
-
Formating is done via the <tt>format</tt> method. The method accepts a <tt>Symbol</tt> or a <tt>String</tt>.
|
55
|
-
|
56
|
-
When given a string, it interpolates the string with the following fields:
|
57
|
-
|
58
|
-
* %c - country_code (385)
|
59
|
-
* %a - area_code (91)
|
60
|
-
* %n - number (5125486)
|
24
|
+
Phoney.region = :de
|
61
25
|
|
62
|
-
|
63
|
-
|
64
|
-
pn.to_s # => "+44 65 4654 6546"
|
65
|
-
pn.format("%a/%n") # => "64/46546546"
|
66
|
-
pn.format("+ %c (%a) %n") # => "+ 44 (65) 46546546"
|
67
|
-
|
68
|
-
Usually you will just want Phoney to figure out how to format the number correctly.
|
69
|
-
When given a symbol you can let the parser guess the best format and pass in one of the following auto-formatting symbols:
|
70
|
-
pn.format(:default) # => "+44 65 4654 6546"
|
71
|
-
|
72
|
-
# :national omits the country code and assumes a representation within the region
|
73
|
-
pn.format(:national) # => "65 4654 6546"
|
74
|
-
|
75
|
-
# :local even omits the area code and assumes a default area, so be careful!
|
76
|
-
pn.format(:local) # => "46 5465 46"
|
26
|
+
Phoney.format("04105456789")
|
27
|
+
# => "04105 456789"
|
77
28
|
|
78
29
|
== TODOs
|
79
30
|
|
data/lib/phoney.rb
CHANGED
@@ -8,8 +8,12 @@ module Phoney
|
|
8
8
|
PLACEHOLDER_CHAR = '#'
|
9
9
|
DIGITS = '0123456789'
|
10
10
|
NUMPAD_CHARS = '+#*'+DIGITS
|
11
|
-
|
11
|
+
|
12
12
|
class << self
|
13
|
+
def format(input, options = {})
|
14
|
+
Phoney::Parser.parse(input, options)
|
15
|
+
end
|
16
|
+
|
13
17
|
def region
|
14
18
|
@region ||= Region[:us]
|
15
19
|
end
|
@@ -29,77 +33,12 @@ module Phoney
|
|
29
33
|
def area_code=(area_code)
|
30
34
|
@area_code = area_code
|
31
35
|
end
|
32
|
-
|
36
|
+
|
33
37
|
def version
|
34
38
|
VERSION::STRING
|
35
39
|
end
|
36
40
|
end
|
37
|
-
|
38
|
-
def initialize(params, region_code=nil)
|
39
|
-
region = Region.find(region_code)
|
40
|
-
country_code = region.country_code.to_s if region
|
41
|
-
|
42
|
-
if params.is_a?(String)
|
43
|
-
params = Parser.parse_to_parts(params, region_code)
|
44
|
-
end
|
45
|
-
|
46
|
-
self.number = params[:number].to_s
|
47
|
-
# The rare case when some digits are in front of the area code
|
48
|
-
self.prefix_code = params[:prefix_code].to_s
|
49
|
-
# Can be empty, because some special numbers just don't have an area code (e.g. 911)
|
50
|
-
self.area_code = params[:area_code].to_s || self.class.area_code
|
51
|
-
self.country_code = params[:country_code].to_s || country_code || self.class.country_code
|
52
|
-
|
53
|
-
raise "Must enter number" if(self.number.nil? || self.number.empty?)
|
54
|
-
raise "Must enter country code or set default country code" if(self.country_code.nil? || self.country_code.empty?)
|
55
|
-
end
|
56
|
-
|
57
|
-
# Does this number belong to the default country code?
|
58
|
-
def has_default_country_code?
|
59
|
-
country_code.to_s == self.class.country_code.to_s
|
60
|
-
end
|
61
|
-
|
62
|
-
# Does this number belong to the default area code?
|
63
|
-
def has_default_area_code?
|
64
|
-
(!area_code.to_s.empty? && area_code.to_s == self.class.area_code.to_s)
|
65
|
-
end
|
66
|
-
|
67
|
-
# Formats the phone number.
|
68
|
-
# If the method argument is a String, it is used as a format string, with the following fields being interpolated:
|
69
|
-
#
|
70
|
-
# * %c - country_code (385)
|
71
|
-
# * %a - area_code (91)
|
72
|
-
# * %n - number (5125486)
|
73
|
-
#
|
74
|
-
# If the method argument is a Symbol, we use one of the default formattings and let the parser do the rest.
|
75
|
-
def format(fmt)
|
76
|
-
if fmt.is_a?(Symbol)
|
77
|
-
case fmt
|
78
|
-
when :default
|
79
|
-
Parser::parse("+#{country_code} #{prefix_code}#{area_code} #{number}", country_code)
|
80
|
-
when :national
|
81
|
-
Parser::parse("#{area_code} #{number}", country_code)
|
82
|
-
when :local
|
83
|
-
STDERR.puts "Warning: Using local format without setting a default area code!?" if Phoney.area_code.nil?
|
84
|
-
Parser::parse(number, country_code)
|
85
|
-
else
|
86
|
-
raise "The format #{fmt} doesn't exist'"
|
87
|
-
end
|
88
|
-
else
|
89
|
-
format_number(fmt)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
# The default format is the canonical format: "+{country_code} {area_code} {number}"
|
94
|
-
def to_s
|
95
|
-
format(:default)
|
96
|
-
end
|
97
|
-
|
98
|
-
private
|
99
|
-
def format_number(fmt)
|
100
|
-
fmt.gsub("%c", country_code || "").gsub("%a", area_code || "").gsub("%n", number || "")
|
101
|
-
end
|
102
41
|
end
|
103
42
|
|
104
43
|
# Load our region file when we require the library
|
105
|
-
Phoney::Region.load
|
44
|
+
Phoney::Region.load
|
data/lib/phoney/version.rb
CHANGED