teebo 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/teebo/credit_card.rb +44 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0116fd7256c8ebc11aaf32761e425cfe26793979
|
4
|
+
data.tar.gz: 4ad1b311bdd1ca5420f50df2448407245618df95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cf98921da7875e0398d77725c62efb1d41d6d5ca2ef8c336fbd03516504fb607465b3e1d48d5b613b50ca74e4793a150fb4f6e670d8ab24566a2d1c0515a2ad
|
7
|
+
data.tar.gz: 9f02487e929191882ea3ac11c55431f03dc42b944917654b211c90cdeb6790b0a6515dbc8382f75dc4432203b482a8a3263458f7a2bb3606f238ddd56b0bd99a
|
data/lib/teebo/credit_card.rb
CHANGED
@@ -14,7 +14,7 @@ module Teebo
|
|
14
14
|
#
|
15
15
|
# Returns a credit card issuer according to the likelihood that it would be seen in the wild.
|
16
16
|
#
|
17
|
-
def
|
17
|
+
def generate_issuer
|
18
18
|
random_choice = Random.rand
|
19
19
|
full_weight = 0
|
20
20
|
@cc_issuers.each do |issuer|
|
@@ -30,12 +30,53 @@ module Teebo
|
|
30
30
|
#
|
31
31
|
def generate_number(issuer)
|
32
32
|
# TODO: Sample according to realistic distribution - numbers w/long prefixes are prioritized too highly right now.
|
33
|
-
prefix = issuer['iin-prefixes'].sample
|
33
|
+
prefix = issuer['iin-prefixes'].sample.to_s
|
34
34
|
length = issuer['lengths'].sample
|
35
|
-
|
35
|
+
generated = number_to_digits(length - prefix.length)
|
36
|
+
number = prefix + generated
|
37
|
+
if issuer['validation']
|
38
|
+
last_digit_validation(number)
|
39
|
+
end
|
40
|
+
|
36
41
|
end
|
37
42
|
|
43
|
+
#
|
44
|
+
# Gets the sum of the digits of the specified number. Necessary to calculate a credit card's
|
45
|
+
# check digit.
|
46
|
+
#
|
47
|
+
def luhn_sum(number)
|
48
|
+
digits = number.to_s.chars.map(&:to_i)
|
49
|
+
digits.reverse.each_slice(2).map do |x, y|
|
50
|
+
[(x * 2).divmod(10), y || 0]
|
51
|
+
end.flatten.inject(:+)
|
52
|
+
end
|
38
53
|
|
54
|
+
#
|
55
|
+
# A simple implementation of the [Luhn algorithm](http://en.wikipedia.org/wiki/Luhn_algorithm),
|
56
|
+
# which all U.S.-based Credit Card issuers use for the validation check on credit card numbers.
|
57
|
+
#
|
58
|
+
def luhn_algorithm(number)
|
59
|
+
digit_sum = luhn_sum(number)
|
60
|
+
((10 - digit_sum % 10) % 10).to_s
|
61
|
+
end
|
39
62
|
|
63
|
+
#
|
64
|
+
# Uses the Luhn algorithm to replace the last digit in the generated number with the correct
|
65
|
+
# check digit.
|
66
|
+
#
|
67
|
+
def last_digit_validation(number)
|
68
|
+
trimmed_number = number[0..-2]
|
69
|
+
check_digit = luhn_algorithm(trimmed_number)
|
70
|
+
trimmed_number + check_digit
|
71
|
+
end
|
72
|
+
|
73
|
+
#
|
74
|
+
# Generates a random number with the specified number of digits, padding the beginning with
|
75
|
+
# '0' characters, if necessary.
|
76
|
+
#
|
77
|
+
def number_to_digits(digits)
|
78
|
+
digits = digits.to_i
|
79
|
+
rand(10 ** digits).to_s.rjust(digits,'0')
|
80
|
+
end
|
40
81
|
end
|
41
82
|
end
|