Module: Phoney::Vanity

Defined in:
lib/phoney/vanity.rb

Overview

Helper module that maps vanity numbers to digit numbers.

Constant Summary collapse

VANITY_REGEXP =
/\A\d{3}[a-zA-Z]{6,12}\Z/
VANITY_NORMALIZING_REGEXP =
/^0*|[^\d\w]/

Class Method Summary collapse

Class Method Details

.mappingObject

Returns a char to number mapping string for the String#tr method.



8
9
10
11
12
13
# File 'lib/phoney/vanity.rb', line 8

def self.mapping
  @@mapping ||= [
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.freeze,
    '2223334445556667777888999922233344455566677778889999'.freeze
  ]
end

.normalized(number) ⇒ Object

Vanity-Normalized.



27
28
29
# File 'lib/phoney/vanity.rb', line 27

def self.normalized number
  number.gsub VANITY_NORMALIZING_REGEXP, ''
end

.replace(number) ⇒ Object

Replaces (and normalizes) vanity characters of passed number with correct digits.



16
17
18
# File 'lib/phoney/vanity.rb', line 16

def self.replace number
  number.tr *mapping
end

.vanity?(number) ⇒ Boolean

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/phoney/vanity.rb', line 22

def self.vanity? number
  !(normalized(number) =~ VANITY_REGEXP).nil?
end