Class: LocaSMS::Numbers
- Inherits:
-
Object
- Object
- LocaSMS::Numbers
- Defined in:
- lib/locasms/numbers.rb
Overview
Class that sanitizes and validates a list of mobile’s numbers
Instance Attribute Summary collapse
-
#bad ⇒ Object
readonly
Returns the value of attribute bad.
-
#good ⇒ Object
readonly
Returns the value of attribute good.
Instance Method Summary collapse
-
#bad? ⇒ TrueClass, FalseClass
Checks if there are bad numbers.
-
#evaluate(*numbers) ⇒ Hash
Evaluates a list of numbers and separat them in two groups.
-
#initialize(*numbers) ⇒ Numbers
constructor
Creates a new instance of the class, separating good and bad numbers.
-
#normalize(*numbers) ⇒ Array<String>
Clears all non digits from a mobile’s number and converts into a normalized array.
-
#to_s ⇒ String
Return all good numbers in a string comma joined.
-
#valid_number?(number) ⇒ TrueClass, FalseClass
Validates if a mobile’s number has only digits.
Constructor Details
#initialize(*numbers) ⇒ Numbers
Creates a new instance of the class, separating good and bad numbers
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
#bad ⇒ Object (readonly)
Returns the value of attribute bad.
6 7 8 |
# File 'lib/locasms/numbers.rb', line 6 def bad @bad end |
#good ⇒ Object (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
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
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
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_s ⇒ String
Return 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
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 |