Class: NumberRecognizer

Inherits:
Object
  • Object
show all
Includes:
FormatDsl
Defined in:
lib/number_recognizer.rb,
lib/number_recognizer/format_dsl.rb

Defined Under Namespace

Modules: FormatDsl

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FormatDsl

included

Constructor Details

#initialize(number) ⇒ NumberRecognizer

Returns a new instance of NumberRecognizer.



25
26
27
28
29
30
31
32
# File 'lib/number_recognizer.rb', line 25

def initialize(number)
  @number = number
  @parsed = false
  @corrected = false

  @valid = nil
  @mobile = nil
end

Instance Attribute Details

#countryObject

Input



8
9
10
# File 'lib/number_recognizer.rb', line 8

def country
  @country
end

#country_nameObject

Input



8
9
10
# File 'lib/number_recognizer.rb', line 8

def country_name
  @country_name
end

#local_numberObject

Input



8
9
10
# File 'lib/number_recognizer.rb', line 8

def local_number
  @local_number
end

#numberObject

Input



8
9
10
# File 'lib/number_recognizer.rb', line 8

def number
  @number
end

#old_numberObject

Input



8
9
10
# File 'lib/number_recognizer.rb', line 8

def old_number
  @old_number
end

#prefixObject

Input



8
9
10
# File 'lib/number_recognizer.rb', line 8

def prefix
  @prefix
end

Instance Method Details

#correct(country_bias = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/number_recognizer.rb', line 54

def correct(country_bias=nil)
  @corrected = false
  old_number = number
  case number
  when /^0?9([136]\d{7})$/ #this must come before NL !
    self.number = "3519#{$1}"
  when /^0?7([1-9]\d{7})$/ # 07 can't be followed by a 0 in spain. It's legal in England.
    prefix = pick_biased_country([44,34], country_bias)
    self.number = "#{prefix}7#{$1}"
  when /^0?7(0\d{7})$/ # 07 can't be followed by a 0 in spain. It's legal in England.
    prefix = pick_biased_country([44], country_bias)
    self.number = "#{prefix}7#{$1}"
  when /^0?7(\d{9})$/ # Spanish numbers have 8 digits after the 07. English numbers can also have 9 digits after the 07.
    prefix = pick_biased_country([44], country_bias)
    self.number = "#{prefix}7#{$1}"
  when /^0?6+(\d{8})$/
    prefix = pick_biased_country([31,34], country_bias)
    self.number = "#{prefix}6#{$1}"
  when /^0?4(\d{8})$/
    prefix = pick_biased_country([32,61], country_bias)
    self.number = "#{prefix}4#{$1}"
  else
    # No correction, so no need re-parse
    @corrected = true
    return valid?
  end
  self.old_number = old_number
  @parsed = false
  @corrected = true
  valid?
end

#mobile?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/number_recognizer.rb', line 34

def mobile?
  parse unless @parsed
  @mobile
end

#normalized_numberObject



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

def normalized_number
  parse unless @parsed
  "#{prefix}#{local_number}"
end

#typeObject



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

def type
  return nil unless valid?
  "#{country_name} #{mobile? ? "mobile" : "landline"}"
end

#valid?Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/number_recognizer.rb', line 39

def valid?
  parse unless @parsed
  @valid
end

#valid_or_correct_mobile?(country_bias = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid_or_correct_mobile?(country_bias=nil)
  correct(country_bias)
  valid? && mobile?
end