module UmbrellioUtils::Checks

Constants

CONSONANTS_REGEX
EMAIL_REGEXP
VOWELS_REGEX

Public Instance Methods

between?(checked_value, boundary_values, convert_sym: :to_f) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 57
def between?(checked_value, boundary_values, convert_sym: :to_f)
  checked_value.public_send(convert_sym).between?(*boundary_values.first(2).map(&convert_sym))
end
int_array?(value, size_range = 1..Float::INFINITY) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 61
def int_array?(value, size_range = 1..Float::INFINITY)
  value.all? { |value| value.to_i.positive? } && value.size.in?(size_range)
end
secure_compare(src, dest) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 11
def secure_compare(src, dest)
  ActiveSupport::SecurityUtils.secure_compare(
    ::Digest::SHA256.hexdigest(src),
    ::Digest::SHA256.hexdigest(dest),
  )
end
valid_card?(number) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 18
def valid_card?(number)
  numbers = number.to_s.chars.map(&:to_i)

  modified_numbers = numbers.reverse.map.with_index do |number, index|
    if index.odd?
      number *= 2
      number -= 9 if number > 9
    end

    number
  end

  (modified_numbers.sum % 10).zero?
end
valid_card_cvv?(cvv) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 48
def valid_card_cvv?(cvv)
  cvv = cvv.to_s.scan(/\d/).join
  cvv.size.between?(3, 4)
end
valid_card_holder?(holder) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 37
def valid_card_holder?(holder)
  words = holder.to_s.split
  return if words.count != 2
  return if words.any? { |x| x.match?(/(.+)(\1)(\1)/) }
  return unless words.all? { |x| x.size >= 2 }
  return unless words.all? { |x| x.match?(/\A[A-Z]+\z/) }
  return unless words.all? { |x| x.match?(VOWELS_REGEX) && x.match?(CONSONANTS_REGEX) }

  true
end
valid_email?(email) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 33
def valid_email?(email)
  email.to_s =~ EMAIL_REGEXP
end
valid_limit?(limit) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 65
def valid_limit?(limit)
  int_array?(limit, 2..2) && limit.reduce(:<=)
end
valid_phone?(phone) click to toggle source
# File lib/umbrellio_utils/checks.rb, line 53
def valid_phone?(phone)
  Phonelib.valid?(phone)
end