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_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 generate_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

#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
37
38
39
40
41
# 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.to_s
  length = issuer['lengths'].sample
  generated = number_to_digits(length - prefix.length)
  number = prefix + generated
  if issuer['validation']
    last_digit_validation(number)
  end

end

#last_digit_validation(number) ⇒ Object

Uses the Luhn algorithm to replace the last digit in the generated number with the correct check digit.



67
68
69
70
71
# File 'lib/teebo/credit_card.rb', line 67

def last_digit_validation(number)
  trimmed_number = number[0..-2]
  check_digit = luhn_algorithm(trimmed_number)
  trimmed_number + check_digit
end

#luhn_algorithm(number) ⇒ Object

A simple implementation of the [Luhn algorithm](en.wikipedia.org/wiki/Luhn_algorithm), which all U.S.-based Credit Card issuers use for the validation check on credit card numbers.



58
59
60
61
# File 'lib/teebo/credit_card.rb', line 58

def luhn_algorithm(number)
  digit_sum = luhn_sum(number)
  ((10 - digit_sum % 10) % 10).to_s
end

#luhn_sum(number) ⇒ Object

Gets the sum of the digits of the specified number. Necessary to calculate a credit card’s check digit.



47
48
49
50
51
52
# File 'lib/teebo/credit_card.rb', line 47

def luhn_sum(number)
  digits = number.to_s.chars.map(&:to_i)
  digits.reverse.each_slice(2).map do |x, y|
    [(x * 2).divmod(10), y || 0]
  end.flatten.inject(:+)
end

#number_to_digits(digits) ⇒ Object

Generates a random number with the specified number of digits, padding the beginning with ‘0’ characters, if necessary.



77
78
79
80
# File 'lib/teebo/credit_card.rb', line 77

def number_to_digits(digits)
  digits = digits.to_i
  rand(10 ** digits).to_s.rjust(digits,'0')
end