phoney 0.2.0 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 91ea215316dbde0a1e0ab7c0a8ee480c17021db9264ad5949f20ad87a9fb01c2
4
- data.tar.gz: 8010af03683d901660a83ecd03a2b8fa6ff9e5e4ae19ce50a7c74b04d33f20ab
3
+ metadata.gz: df8b354ee38ecd41a897ac6b3feff7a24ebb76e7d5ae2bf0741befab07e492ef
4
+ data.tar.gz: d0be4c600e60567460ad5bcfbfd2605918d5498198b43016d8ec6b29079f1573
5
5
  SHA512:
6
- metadata.gz: 9803f88389d58b814d942589496dbca4e74b3891066c8501f375e2a6ebc6c5a97dc946f91287f5867d5a8f48a2fce8e31149ad2ca08db3abe3790a4280047d29
7
- data.tar.gz: b8a687dcb6fbf2d00d4f4491aa36eb5d74a7ba45c2ec466ed48fba9000fbd3f3183a16ae3218e06dbcb8fbb9356b8d38f0908d18f1bbeeec9b157a378c5133a4
6
+ metadata.gz: 964df0850ab58f3a62384a0896ec08dca432189deeefdbdcbd97b366a6a4e3d3baab3042a74af515dd66f0e65cbd65d66b981dac6a810440f5163223de22a4a1
7
+ data.tar.gz: c2fd3d4e309e842897c2494491e8ff7a998ecc096ceb4ff4f4f19085c62090760362705764c4d0532f4142e6a95df7901323a70a775774b75d2d285d56a2dbb2
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phoney (0.9.9)
4
+ phoney (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -16,3 +16,6 @@ DEPENDENCIES
16
16
  minitest
17
17
  phoney!
18
18
  rake
19
+
20
+ BUNDLED WITH
21
+ 1.17.1
@@ -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
- pn = Phoney.new("+17041234567")
18
- pn.to_s # "+1 (704) 123-4567"
19
- pn.area_code # "704"
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
- The most common way to create a Phoney object is by parsing from a string:
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
- pn = Phoney.new('+446546546546')
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
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Phoney
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phoney
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Habermann