Module: Phony
- Defined in:
- lib/phony.rb,
lib/phony/dsl.rb,
lib/phony/config.rb,
lib/phony/vanity.rb,
lib/phony/country.rb,
lib/phony/trunk_code.rb,
lib/phony/country_codes.rb,
lib/phony/national_code.rb,
lib/phony/local_splitters/fixed.rb,
lib/phony/local_splitters/regex.rb,
lib/phony/national_splitters/dsl.rb,
lib/phony/national_splitters/none.rb,
lib/phony/national_splitters/fixed.rb,
lib/phony/national_splitters/regex.rb,
lib/phony/national_splitters/default.rb,
lib/phony/national_splitters/variable.rb
Overview
Phony is the main module and is generally used to process E164 phone numbers directly.
Defined Under Namespace
Modules: LocalSplitters, NationalSplitters, Vanity Classes: Config, Country, CountryCodes, DSL, FormattingError, NationalCode, NormalizationError, SplittingError, TrunkCode
Constant Summary collapse
- EMPTY_STRING =
''
Class Attribute Summary collapse
Class Method Summary collapse
-
.[](cc) ⇒ Country
Get the Country for the given CC.
-
.define(&block) ⇒ Object
For country definitions.
-
.format(phone_number, options = {}) ⇒ Array<String>
(also: formatted)
Formats a normalized E164 number according to a country’s formatting scheme.
-
.format!(phone_number, options = {}) ⇒ Array<String>
(also: formatted!)
A destructive version of #format.
-
.normalize(phone_number, options = {}) ⇒ String
Normalizes the given number into a digits-only String.
-
.normalize!(phone_number, options = {}) ⇒ String
A destructive version of #normalize.
-
.plausible?(number, hints = {}) ⇒ Boolean
Makes a plausibility check.
-
.split(phone_number) ⇒ Array<String>
Splits the phone number into pieces according to the country codes.
-
.split!(phone_number, error_number = nil) ⇒ Array<String>
A destructive version of #split.
-
.vanity?(phone_number) ⇒ Boolean
Returns true if there is a character in the number after the first four numbers.
-
.vanity_to_number(vanity_number) ⇒ String
Converts any character in the vanity_number to its numeric representation.
Class Attribute Details
Class Method Details
.[](cc) ⇒ Country
Get the Country for the given CC.
126 127 128 |
# File 'lib/phony.rb', line 126 def [](cc) @codes[cc] end |
.define(&block) ⇒ Object
For country definitions.
There are two styles: With or without block. Use the block if you have multiple.
Examples: Phony.define do
country ...
end
Phony.define.country …
16 17 18 19 20 |
# File 'lib/phony/dsl.rb', line 16 def self.define(&block) dsl = DSL.new dsl.instance_eval(&block) if block_given? dsl end |
.format(phone_number, options = {}) ⇒ Array<String> Also known as: formatted
Formats a normalized E164 number according to a country’s formatting scheme.
Absolutely needs a normalized E164 number.
238 239 240 241 242 |
# File 'lib/phony.rb', line 238 def format(phone_number, = {}) raise ArgumentError, 'Phone number cannot be nil. Use e.g. number && Phony.format(number).' unless phone_number format! phone_number.dup, end |
.format!(phone_number, options = {}) ⇒ Array<String> Also known as: formatted!
A destructive version of #format.
Formats a normalized E164 number according to a country’s formatting scheme.
Absolutely needs a normalized E164 number.
266 267 268 269 270 |
# File 'lib/phony.rb', line 266 def format!(phone_number, = {}) @codes.format phone_number, rescue raise FormattingError.new end |
.normalize(phone_number, options = {}) ⇒ String
Normalizes the given number into a digits-only String.
Useful before inserting the number into a database.
147 148 149 150 151 |
# File 'lib/phony.rb', line 147 def normalize(phone_number, = {}) raise ArgumentError, 'Phone number cannot be nil. Use e.g. number && Phony.normalize(number).' unless phone_number normalize! phone_number.dup, end |
.normalize!(phone_number, options = {}) ⇒ String
A destructive version of #normalize.
170 171 172 173 174 |
# File 'lib/phony.rb', line 170 def normalize!(phone_number, = {}) @codes.normalize phone_number, rescue raise NormalizationError.new end |
.plausible?(number, hints = {}) ⇒ Boolean
Makes a plausibility check.
If it returns false, it is not plausible. If it returns true, it is unclear whether it is plausible, leaning towards being plausible.
280 281 282 |
# File 'lib/phony.rb', line 280 def plausible?(number, hints = {}) @codes.plausible? number, hints end |
.split(phone_number) ⇒ Array<String>
Splits the phone number into pieces according to the country codes.
Useful for manually processing the CC, NDC, and local pieces.
190 191 192 193 194 |
# File 'lib/phony.rb', line 190 def split(phone_number) raise ArgumentError, 'Phone number cannot be nil. Use e.g. number && Phony.split(number).' unless phone_number split! phone_number.dup, phone_number end |
.split!(phone_number, error_number = nil) ⇒ Array<String>
A destructive version of #split.
210 211 212 213 214 215 |
# File 'lib/phony.rb', line 210 def split!(phone_number, error_number = nil) @codes.split phone_number rescue # NB The error_number (reference) is used because phone_number is destructively handled. raise SplittingError.new(error_number) end |
.vanity?(phone_number) ⇒ Boolean
Returns true if there is a character in the number after the first four numbers.
287 288 289 |
# File 'lib/phony.rb', line 287 def vanity?(phone_number) @codes.vanity? phone_number.dup end |
.vanity_to_number(vanity_number) ⇒ String
Converts any character in the vanity_number to its numeric representation. Does not check if the passed number is a valid vanity_number, simply does replacement.
301 302 303 |
# File 'lib/phony.rb', line 301 def vanity_to_number(vanity_number) @codes.vanity_to_number vanity_number.dup end |