Sha256: fd9849d301387f1ed006e50c5102ec594f3f902e5fdec3003ccc0092c92fbfe0

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'credy/version'
require 'credy/string'
require 'credy/rules'
require 'credy/check'

module Credy

  def self.root
    File.expand_path '../..', __FILE__
  end

  class CreditCard

    # Generate a credit card number
    def self.generate(options = {})

      # Include global rules (not based on country)
      include_global_rules = options[:country].nil?
      rule = Rules.filter(options, include_global_rules).sample

      return nil unless rule

      length = rule[:length].is_a?(Array) ? rule[:length].sample : rule[:length]
      number = rule[:prefix] 

      # Generates n-1 digits
      (length - number.length - 1).times do
        number = number + rand(10).to_s
      end

      # Generates the last digit according to luhn algorithm
      l = nil
      digits = (0..9).to_a.map(&:to_s)
      begin
        l = digits.delete digits.sample
      end while !Check.luhn number+l

      number = number+l

      {
        number: number,
        type: rule[:type],
        country: rule[:country]
      }
    end

    # Returns information about a number
    def self.infos(number)
      rules = Rules.flatten(true).select do |rule|
        valid = true

        # Check number of digits
        lengths = rule[:length].is_a?(Array) ? rule[:length] : [rule[:length]]
        valid = false unless lengths.include? number.length

        # Check prefix
        valid = false unless !(number =~ Regexp.new("^#{rule[:prefix]}")).nil?
        valid
      end

      if rules
        rules[0]
      else
        nil
      end
    end

    # Validates a number
    def self.validate(number)
      criterii = {}
      criterii[:luhn] = Check.luhn number
      criterii[:type] = !!self.infos(number)

      valid = criterii.all? { |_, v| v == true }

      {
        valid: valid,
        details: criterii
      }
    end
  
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
credy-0.1.1 lib/credy.rb