Module: CreditCardGenerator
- Defined in:
- lib/credit_card_generator/version.rb,
lib/credit_card_generator.rb
Overview
lib/credit_card_generator/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Class Method Summary collapse
- .generate(prefix, length, quantity) ⇒ Object
- .generate_card_number(prefix, length) ⇒ Object
- .luhn_checksum(card_number) ⇒ Object
Class Method Details
.generate(prefix, length, quantity) ⇒ Object
28 29 30 |
# File 'lib/credit_card_generator.rb', line 28 def self.generate(prefix, length, quantity) (1..quantity).map { generate_card_number(prefix, length) } end |
.generate_card_number(prefix, length) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/credit_card_generator.rb', line 17 def self.generate_card_number(prefix, length) card_number = prefix.dup while card_number.length < (length - 1) card_number += rand(0..9).to_s end checksum_digit = (10 - luhn_checksum(card_number + '0')) % 10 card_number + checksum_digit.to_s end |
.luhn_checksum(card_number) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/credit_card_generator.rb', line 2 def self.luhn_checksum(card_number) digits = card_number.chars.map(&:to_i) checksum = 0 digits.reverse.each_with_index do |digit, index| if index.odd? digit *= 2 digit -= 9 if digit > 9 end checksum += digit end checksum % 10 end |