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
11 12 13 14 |
# File 'lib/locasms/numbers.rb', line 11 def initialize(*numbers) evaluated = evaluate(numbers) @good, @bad = evaluated[:good], evaluated[:bad] end |
Instance Attribute Details
#bad ⇒ Object (readonly)
Returns the value of attribute bad.
5 6 7 |
# File 'lib/locasms/numbers.rb', line 5 def bad @bad end |
#good ⇒ Object (readonly)
Returns the value of attribute good.
5 6 7 |
# File 'lib/locasms/numbers.rb', line 5 def good @good end |
Instance Method Details
#bad? ⇒ TrueClass, FalseClass
Checks if there are bad numbers
19 20 21 |
# File 'lib/locasms/numbers.rb', line 19 def bad? not 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
65 66 67 68 69 70 71 |
# File 'lib/locasms/numbers.rb', line 65 def evaluate(*numbers) normalize(numbers).reduce({good: [], bad: []}) do |hash, number| bucket = valid_number?(number) ? :good : :bad hash[bucket] << number hash end end |
#normalize(*numbers) ⇒ Array<String>
Clears all non digits from a mobile’s number and converts into a normalized array
36 37 38 39 40 41 |
# File 'lib/locasms/numbers.rb', line 36 def normalize(*numbers) numbers = numbers.join(',') .split(',') .map{|number| number.gsub(/[^0-9a-zA-Z]/, '') } .delete_if{|number| number.empty? } end |
#to_s ⇒ String
Return all good numbers in a string comma joined
75 76 77 |
# File 'lib/locasms/numbers.rb', line 75 def to_s (good || []).join(',') end |
#valid_number?(number) ⇒ TrueClass, FalseClass
Validates if a mobile’s number has only digits
46 47 48 49 |
# File 'lib/locasms/numbers.rb', line 46 def valid_number?(number) return false if number.nil? or number =~ /[^0-9a-zA-Z]/ [10, 11].include? number.size end |