Module: Phony::Vanity

Defined in:
lib/phony/vanity.rb

Overview

Helper module that maps vanity numbers to digit numbers.

Constant Summary collapse

@@vanity_regexp =

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

/\A\d{3}[a-zA-Z]{6,12}\Z/
@@vanity_normalizing_regexp =

Vanity-Normalized.

/^0*|[^\w]/

Class Method Summary collapse

Class Method Details

.mappingObject

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


9
10
11
12
13
14
# File 'lib/phony/vanity.rb', line 9

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

.normalized(number) ⇒ Object


33
34
35
# File 'lib/phony/vanity.rb', line 33

def self.normalized number
  number.gsub @@vanity_normalizing_regexp, ''
end

.replace(number) ⇒ Object

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


18
19
20
# File 'lib/phony/vanity.rb', line 18

def self.replace number
  number.tr(*mapping)
end

.vanity?(number) ⇒ Boolean


26
27
28
# File 'lib/phony/vanity.rb', line 26

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