Module: Phony

Defined in:
lib/phony/dsl.rb,
lib/phony.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/regex.rb,
lib/phony/local_splitters/fixed.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

# Switzerland (simplified): # country('41', fixed(2) >> local())

# Germany. Too big, we use a separate file: # Phony.define do

country '49', match(...) >> split([...]) ||
              one_of([...], :max_length => 5) >> split([...])

end

# Denmark: # country('45', none >> split())

# Hungary: # country('36',

match(/^104|105|107|112/) >> split([3,3]) ||
one_of([1], :max_length => 2) >> split([3,4])

)

Note: Perhaps the DSL should operate directly on country codes.

Defined Under Namespace

Modules: LocalSplitters, NationalSplitters, Vanity Classes: Country, CountryCodes, DSL, NationalCode, NormalizationError, TrunkCode

Constant Summary collapse

EMPTY_STRING =
''

Class Method Summary collapse

Class Method Details

.[](cc) ⇒ Object

Get the Country for the given CC.

Example:

us = Phony['1']
normalized_number = us.normalize number

87
88
89
# File 'lib/phony.rb', line 87

def [] cc
  @codes[cc]
end

.defineObject

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 …


39
40
41
42
43
# File 'lib/phony/dsl.rb', line 39

def self.define
  dsl = DSL.new
  dsl.instance_eval(&Proc.new) if block_given?
  dsl
end

.format(phone_number, options = {}) ⇒ Object Also known as: formatted

Formats a E164 number according to local customs.

Raises:

  • (ArgumentError)

119
120
121
122
# File 'lib/phony.rb', line 119

def format phone_number, options = {}
  raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.format(number)." unless phone_number
  format! phone_number.dup, options
end

.format!(phone_number, options = {}) ⇒ Object Also known as: formatted!


123
124
125
# File 'lib/phony.rb', line 123

def format! phone_number, options = {}
  @codes.format phone_number, options
end

.normalize(phone_number, options = {}) ⇒ Object

Normalizes the given number.

Useful before inserting the number into a database.

Raises:

  • (ArgumentError)

95
96
97
98
# File 'lib/phony.rb', line 95

def normalize phone_number, options = {}
  raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.normalize(number)." unless phone_number
  normalize! phone_number.dup, options
end

.normalize!(phone_number, options = {}) ⇒ Object


99
100
101
102
103
# File 'lib/phony.rb', line 99

def normalize! phone_number, options = {}
  @codes.normalize phone_number, options
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.

Returns:

  • (Boolean)

135
136
137
# File 'lib/phony.rb', line 135

def plausible? number, hints = {}
  @codes.plausible? number, hints
end

.split(phone_number) ⇒ Object

Splits the phone number into pieces according to the country codes.

Raises:

  • (ArgumentError)

107
108
109
110
# File 'lib/phony.rb', line 107

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
end

.split!(phone_number) ⇒ Object


111
112
113
114
115
# File 'lib/phony.rb', line 111

def split! phone_number
  parts = @codes.split phone_number
  parts.delete_at 1
  parts
end

.vanity?(phone_number) ⇒ Boolean

Returns true if there is a character in the number after the first four numbers.

Returns:

  • (Boolean)

142
143
144
# File 'lib/phony.rb', line 142

def vanity? phone_number
  @codes.vanity? phone_number.dup
end

.vanity_to_number(vanity_number) ⇒ Object

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.


149
150
151
# File 'lib/phony.rb', line 149

def vanity_to_number vanity_number
  @codes.vanity_to_number vanity_number.dup
end