Module: Phoney
- Defined in:
- lib/phoney.rb,
lib/phoney/rules.rb,
lib/phoney/parser.rb,
lib/phoney/region.rb,
lib/phoney/vanity.rb,
lib/phoney/version.rb,
lib/phoney/formatter.rb
Defined Under Namespace
Modules: Formatter, Parser, Vanity Classes: Region, Rule, RuleGroup
Constant Summary collapse
- PLACEHOLDER_CHAR =
'#'- DIGITS =
'0123456789'- NUMPAD_CHARS =
'+#*'+DIGITS
- VERSION =
"0.2.0"
Class Method Summary collapse
- .area_code ⇒ Object
- .area_code=(area_code) ⇒ Object
- .country_code ⇒ Object
- .region ⇒ Object
- .region=(region) ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
-
#format(fmt) ⇒ Object
Formats the phone number.
-
#has_default_area_code? ⇒ Boolean
Does this number belong to the default area code?.
-
#has_default_country_code? ⇒ Boolean
Does this number belong to the default country code?.
- #initialize(params, region_code = nil) ⇒ Object
-
#to_s ⇒ Object
The default format is the canonical format: “+Phoney.country_code Phoney.area_code number”.
Class Method Details
.area_code ⇒ Object
25 26 27 |
# File 'lib/phoney.rb', line 25 def area_code @area_code ||= nil end |
.area_code=(area_code) ⇒ Object
29 30 31 |
# File 'lib/phoney.rb', line 29 def area_code=(area_code) @area_code = area_code end |
.country_code ⇒ Object
21 22 23 |
# File 'lib/phoney.rb', line 21 def country_code @country_code ||= region.country_code.to_s end |
.region ⇒ Object
13 14 15 |
# File 'lib/phoney.rb', line 13 def region @region ||= Region[:us] end |
.region=(region) ⇒ Object
17 18 19 |
# File 'lib/phoney.rb', line 17 def region=(region) @region = Region[region.to_s.to_sym] end |
.version ⇒ Object
33 34 35 |
# File 'lib/phoney.rb', line 33 def version VERSION::STRING end |
Instance Method Details
#format(fmt) ⇒ Object
Formats the phone number. If the method argument is a String, it is used as a format string, with the following fields being interpolated:
-
%c - country_code (385)
-
%a - area_code (91)
-
%n - number (5125486)
If the method argument is a Symbol, we use one of the default formattings and let the parser do the rest.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/phoney.rb', line 75 def format(fmt) if fmt.is_a?(Symbol) case fmt when :default Parser::parse("+#{country_code} #{prefix_code}#{area_code} #{number}", country_code) when :national Parser::parse("#{area_code} #{number}", country_code) when :local STDERR.puts "Warning: Using local format without setting a default area code!?" if Phoney.area_code.nil? Parser::parse(number, country_code) else raise "The format #{fmt} doesn't exist'" end else format_number(fmt) end end |
#has_default_area_code? ⇒ Boolean
Does this number belong to the default area code?
63 64 65 |
# File 'lib/phoney.rb', line 63 def has_default_area_code? (!area_code.to_s.empty? && area_code.to_s == self.class.area_code.to_s) end |
#has_default_country_code? ⇒ Boolean
Does this number belong to the default country code?
58 59 60 |
# File 'lib/phoney.rb', line 58 def has_default_country_code? country_code.to_s == self.class.country_code.to_s end |
#initialize(params, region_code = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/phoney.rb', line 38 def initialize(params, region_code=nil) region = Region.find(region_code) country_code = region.country_code.to_s if region if params.is_a?(String) params = Parser.parse_to_parts(params, region_code) end self.number = params[:number].to_s # The rare case when some digits are in front of the area code self.prefix_code = params[:prefix_code].to_s # Can be empty, because some special numbers just don't have an area code (e.g. 911) self.area_code = params[:area_code].to_s || self.class.area_code self.country_code = params[:country_code].to_s || country_code || self.class.country_code raise "Must enter number" if(self.number.nil? || self.number.empty?) raise "Must enter country code or set default country code" if(self.country_code.nil? || self.country_code.empty?) end |
#to_s ⇒ Object
The default format is the canonical format: “+country_code area_code number”
94 95 96 |
# File 'lib/phoney.rb', line 94 def to_s format(:default) end |