Parent

Methods

Forgery::CreditCard

Generates random credit card numbers.

Constants

CARDS

Public Class Methods

number(options={}) click to toggle source

Gets a random credit card number

Forgery(:credit_card).number
# => "4539750423451972"

Forgery(:credit_card).number(:type => 'Visa', :length => 13)
# => "4556180133982"

Supported Options

:type

The credit card type. Defaults to a random selection.

:length

The length of the credit card number. Defaults to the length for the selected card type.

:prefixes

The allowed prefixes for the card number. Defaults to prefixes for the selected card type.

# File lib/forgery/forgery/credit_card.rb, line 34
def self.number(options={})
  # find a card by type specified, or select a card randomly
  card = if options[:type]
    CARDS.find { |card| card[:type] == options[:type] }.clone
  else
    CARDS.random.clone
  end
  # merge the remaining options
  card.merge!(options)
  # start the number with a prefix for this card
  number = Forgery::Extend(card[:prefixes]).random
  # fill in the rest of the number with random digits, leave one space for the check digit
  number << Forgery::Extend("#" * (card[:length] - number.length - 1)).to_numbers
  # add the check digit
  number += check_digit(number)
end
type() click to toggle source

Gets a random credit card type

Forgery(:credit_card).type
# => "Visa"
# File lib/forgery/forgery/credit_card.rb, line 15
def self.type
  CARDS.random[:type]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.