Module: UKPhoneNumbers

Defined in:
lib/uk_phone_numbers.rb,
lib/uk_phone_numbers/version.rb

Constant Summary collapse

PATTERNS =
<<-PATTERNS
  (01###) #####[#]
  (011#) ### ####
  (01#1) ### ####
  (013873) #####
  (015242) #####
  (015394) #####
  (015395) #####
  (015396) #####
  (016973) #####
  (016974) #####
  (016977) ####[#]
  (017683) #####
  (017684) #####
  (017687) #####
  (019467) #####
  (02#) #### ####
  03## ### ####
  05### ######
  0500 ######
  07### ######
  08## ### ###[#]
  09## ### ####
PATTERNS
REGEXPS =
[]
VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.format(number, opts = {}) ⇒ Object



49
50
51
52
# File 'lib/uk_phone_numbers.rb', line 49

def self.format(number, opts = {})
  match = REGEXPS.map { |r| r.match(number) }.reject(&:nil?).first
  match ? match[1..-1].join(opts[:separator] || ' ') : false
end

.pattern_to_regexp(pattern) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/uk_phone_numbers.rb', line 28

def self.pattern_to_regexp(pattern)
  regexp = pattern.dup
  regexp.gsub!(/[()]/, '')
  regexp = regexp.split.map { |p| "(#{p})" }.join
  regexp.gsub!(/\[([^\]]*)\]/, '(?:\1)?')
  regexp.gsub!(/#/, '\d')
  Regexp.new("\\A#{regexp}\\z")
end

.valid?(number) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/uk_phone_numbers.rb', line 45

def self.valid?(number)
  REGEXPS.select { |r| r.match(number) }.any?
end