Class: LocaSMS::Numbers

Inherits:
Object
  • Object
show all
Defined in:
lib/locasms/numbers.rb

Overview

Class that sanitizes and validates a list of mobile’s numbers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*numbers) ⇒ Numbers

Creates a new instance of the class, separating good and bad numbers

Parameters:

  • numbers (Array<String>)

    given mobile numbers

See Also:



12
13
14
15
16
# File 'lib/locasms/numbers.rb', line 12

def initialize(*numbers)
  evaluated = evaluate(numbers)
  @good = evaluated[:good]
  @bad = evaluated[:bad]
end

Instance Attribute Details

#badObject (readonly)

Returns the value of attribute bad.



6
7
8
# File 'lib/locasms/numbers.rb', line 6

def bad
  @bad
end

#goodObject (readonly)

Returns the value of attribute good.



6
7
8
# File 'lib/locasms/numbers.rb', line 6

def good
  @good
end

Instance Method Details

#bad?TrueClass, FalseClass

Checks if there are bad numbers

Returns:

  • (TrueClass, FalseClass)

    true if there are bad numbers

See Also:



21
22
23
# File 'lib/locasms/numbers.rb', line 21

def bad?
  !bad.empty?
end

#evaluate(*numbers) ⇒ Hash

Evaluates a list of numbers and separat them in two groups. The good ones and the bad ones. for the bad ones

Examples:

With only good numbers

Numbers.new.evaluate('4199998888','11777770000')
#=> {good: ['4199998888','11777770000'], bad: []}

With only bad numbers

Numbers.new.evaluate('5551212')
#=> {good: [], bad: ['5551212']}

With good and bad numbers mixed

Numbers.new.evaluate('4199998888','11777770000','5551212')
#=> {good: ['4199998888','11777770000'], bad: ['5551212']}

Parameters:

  • numbers (Array<String>)

    given numbers to evaluate

Returns:

  • (Hash)

    containing two values. :good with the good numbers and :bad



69
70
71
72
73
74
# File 'lib/locasms/numbers.rb', line 69

def evaluate(*numbers)
  normalize(numbers).each_with_object({ good: [], bad: [] }) do |number, hash|
    bucket = valid_number?(number) ? :good : :bad
    hash[bucket] << number
  end
end

#normalize(*numbers) ⇒ Array<String>

Clears all non digits from a mobile’s number and converts into a normalized array

Examples:

numbers = Numbers.new
numbers.normalize '+55 (41) 8888-9999'
# => ['554188889999']
numbers.normalize '8888-9999', '7777-0000'
# => ['88889999','77770000']
numbers.normalize '8888-9999,7777-0000'
# => ['88889999','77770000']
numbers.normalize '8888-9999', ['AA', '6666-9999', '7777-0000'], '3333-4444,555-9999'
# => ['88889999','AA','66669999','77770000','33334444','5559999']

Parameters:

  • numbers (Array<String>)

    list of mobile numbers to be clean

Returns:

  • (Array<String>)

    a normalized list of mobile numbers



38
39
40
41
42
43
44
# File 'lib/locasms/numbers.rb', line 38

def normalize(*numbers)
  numbers
    .join(',')
    .split(',')
    .map { |number| number.gsub(/[^0-9a-zA-Z]/, '') }
    .delete_if(&:empty?)
end

#to_sString

Return all good numbers in a string comma joined

Returns:

  • (String)

    all good numbers in a string comma joined



78
79
80
# File 'lib/locasms/numbers.rb', line 78

def to_s
  (good || []).join(',')
end

#valid_number?(number) ⇒ TrueClass, FalseClass

Validates if a mobile’s number has only digits

Parameters:

  • number (String)

    given number to be validated

Returns:

  • (TrueClass, FalseClass)

    true if the number is valid



49
50
51
52
53
# File 'lib/locasms/numbers.rb', line 49

def valid_number?(number)
  return false if number.nil? || number =~ (/[^0-9a-zA-Z]/)

  [10, 11].include? number.size
end