Sha256: 916e024159f47e2558db43c44e659f76ee4b7e5c29166a503cd1fd5723f905d5

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require "uri/mailto"

module UmbrellioUtils
  module Checks
    extend self

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

    def secure_compare(src, dest)
      ActiveSupport::SecurityUtils.secure_compare(
        ::Digest::SHA256.hexdigest(src),
        ::Digest::SHA256.hexdigest(dest),
      )
    end

    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

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

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

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

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

    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

    def int_array?(value, size_range = 1..Float::INFINITY)
      value.all? { |value| value.to_i.positive? } && value.size.in?(size_range)
    end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
umbrellio-utils-1.5.4 lib/umbrellio_utils/checks.rb
umbrellio-utils-1.5.3 lib/umbrellio_utils/checks.rb
umbrellio-utils-1.5.2 lib/umbrellio_utils/checks.rb