Sha256: de111cd5337709eac9b60df2308d027cb607793d96387e9c216767239ead14dd

Contents?: true

Size: 1.51 KB

Versions: 18

Compression:

Stored size: 1.51 KB

Contents

# == CreditCardValidations Factory
# Generates card number that passes validation
#
# #random
#   CreditCardValidations::Factory.random
# #or particular brand
#   CreditCardValidations::Factory.random(:maestro) # "6010430241237266856"
#
#
#
module CreditCardValidations
  class Factory
    class << self
      def random(brand = nil)
        brand = Detector.brands.keys.sample if brand.nil?
        if Detector.brands[brand].nil?
          raise Error.new('Unsupported brand')
        end
        generate(Detector.brands[brand][:rules].sample)
      end

      def generate(rule)
        number(rule[:prefixes].sample, rule[:length].sample, rule.fetch(:options, {})[:skip_luhn])
      end

      def number(prefix, length, skip_luhn = false)
        number = prefix.dup
        1.upto(length - (prefix.length + 1)) do
          number << "#{rand(9)}"
        end
        #if skip luhn
        if skip_luhn
          number += "#{rand(9)}"
        else
          number += last_digit(number).to_s
        end
        number
      end

      #extracted from darkcoding-credit-card

      def last_digit(number)
        # Calculate sum
        sum, pos = 0, 0
        length = number.length + 1

        reversed_number = number.reverse
        while pos < length do
          odd = reversed_number[pos].to_i * 2
          odd -= 9 if odd > 9

          sum += odd

          sum += reversed_number[pos + 1].to_i if pos != (length - 2)

          pos += 2
        end

        (((sum / 10).floor + 1) * 10 - sum) % 10
      end

    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
credit_card_validations-7.0.0 lib/credit_card_validations/factory.rb
credit_card_validations-6.3.0 lib/credit_card_validations/factory.rb
credit_card_validations-6.1.0 lib/credit_card_validations/factory.rb
credit_card_validations-6.0.0 lib/credit_card_validations/factory.rb
credit_card_validations-5.0.0 lib/credit_card_validations/factory.rb
credit_card_validations-4.1.0 lib/credit_card_validations/factory.rb
credit_card_validations-4.0.0 lib/credit_card_validations/factory.rb
credit_card_validations-3.5.1 lib/credit_card_validations/factory.rb
credit_card_validations-3.5.0 lib/credit_card_validations/factory.rb
cw_credit_card_validations-3.4.2 lib/credit_card_validations/factory.rb
cw_credit_card_validations-3.4.1 lib/credit_card_validations/factory.rb
cw_credit_card_validations-3.4.0 lib/credit_card_validations/factory.rb
credit_card_validations-3.4.0 lib/credit_card_validations/factory.rb
credit_card_validations-3.3.0 lib/credit_card_validations/factory.rb
credit_card_validations-3.2.2 lib/credit_card_validations/factory.rb
credit_card_validations-3.2.1 lib/credit_card_validations/factory.rb
credit_card_validations-3.1.0 lib/credit_card_validations/factory.rb
credit_card_validations-3.0.0 lib/credit_card_validations/factory.rb