Class: Teebo::CreditCard

Inherits:
TeeboGenerator show all
Defined in:
lib/teebo/credit_card.rb

Overview

Helper class for generating credit card numbers. Categorizes cards according to their card issuer, while attempting to maintain a realistic distribution between the issuers. Also ensures that the credit card numbers are actually valid.

Instance Method Summary collapse

Constructor Details

#initializeCreditCard

Returns a new instance of CreditCard.



9
10
11
12
# File 'lib/teebo/credit_card.rb', line 9

def initialize
  super
  @cc_issuers = @yaml_mapping['credit-card-issuers']
end

Instance Method Details

#generate_number(issuer) ⇒ Object

Generates a credit card number according to the pattern specified in the ‘issuer’ passed in.



31
32
33
34
35
36
# File 'lib/teebo/credit_card.rb', line 31

def generate_number(issuer)
  # TODO: Sample according to realistic distribution - numbers w/long prefixes are prioritized too highly right now.
  prefix = issuer['iin-prefixes'].sample
  length = issuer['lengths'].sample
  puts prefix, length
end

#get_issuerObject

Returns a credit card issuer according to the likelihood that it would be seen in the wild.



17
18
19
20
21
22
23
24
25
26
# File 'lib/teebo/credit_card.rb', line 17

def get_issuer
  random_choice = Random.rand
  full_weight = 0
  @cc_issuers.each do |issuer|
    full_weight += issuer['probability']
    if random_choice < full_weight
      return issuer
    end
  end
end