Module: UmbrellioUtils::Checks

Extended by:
Checks
Included in:
Checks
Defined in:
lib/umbrellio_utils/checks.rb

Constant Summary collapse

HOLDER_NAME_REGEXP =
/\A([A-Za-z0-9.'-]+ ?)+\z/

Instance Method Summary collapse

Instance Method Details

#between?(checked_value, boundary_values, convert_sym: :to_f) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/umbrellio_utils/checks.rb', line 50

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) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/umbrellio_utils/checks.rb', line 54

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) ⇒ Object



11
12
13
14
15
16
# 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) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/umbrellio_utils/checks.rb', line 41

def valid_card_cvv?(cvv)
  cvv = cvv.to_s.scan(/\d/).join
  cvv.size.between?(3, 4)
end

#valid_card_holder?(holder) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/umbrellio_utils/checks.rb', line 37

def valid_card_holder?(holder)
  holder.to_s.match?(HOLDER_NAME_REGEXP)
end

#valid_email?(email) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/umbrellio_utils/checks.rb', line 33

def valid_email?(email)
  email.to_s.match?(URI::MailTo::EMAIL_REGEXP)
end

#valid_limit?(limit) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/umbrellio_utils/checks.rb', line 58

def valid_limit?(limit)
  int_array?(limit, 2..2) && limit.reduce(:<=)
end

#valid_phone?(phone) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/umbrellio_utils/checks.rb', line 46

def valid_phone?(phone)
  Phonelib.valid?(phone)
end