Class: Forgery::CreditCard

Inherits:
Forgery
  • Object
show all
Defined in:
lib/forgery/forgery/credit_card.rb

Overview

Generates random credit card numbers.

Constant Summary

CARDS =
Forgery::Extend([
  {:type => 'Visa', :length => 16, :prefixes => %w"4539 4556 4916 4532 4929 40240071 4485 4716 4"},
  {:type => 'MasterCard', :length => 16, :prefixes => %w"51 52 53 54 55"},
  {:type => 'American Express', :length => 15, :prefixes => %w"34 37"},
  {:type => 'Discover', :length => 16, :prefixes => ["6011"]}
])

Constants inherited from Forgery

VERSION

Class Method Summary collapse

Methods inherited from Forgery

Extend, dictionaries, formats, load_from!, load_paths, rails?, rails_root

Class Method Details

.number(options = {}) ⇒ Object

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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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 { |ccard| ccard[: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

.typeObject

Gets a random credit card type

Forgery(:credit_card).type
# => "Visa"


15
16
17
# File 'lib/forgery/forgery/credit_card.rb', line 15

def self.type
  CARDS.random[:type]
end