ruby-luhn 0.0.3 → 0.1.0
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/luhn.rb +5 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 608bdb2f2e8df3036a8d79522ca3131bac161321e2c8a753271d8d1c233b1a89
|
4
|
+
data.tar.gz: 7d319c1e76b4f694443d4f13566845c8ef6311e35515ce9ad859afd0e0d43835
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f190ff020f56519208184fd7f636b62ad0b715c4f5589a54d99d723d4d692e33d68865dd33ad677ba83d00342af2596e4d7367f799faa5005f02ae34508300b
|
7
|
+
data.tar.gz: a40f8b5387d36a773aa530110641d3b45167d8843fae014ab40a35001359dbbccb6a604cd31734aa75c9c3a2397b027b593960c6c6298a87373764fc777223b2
|
data/lib/luhn.rb
CHANGED
@@ -7,9 +7,10 @@ class Luhn
|
|
7
7
|
#
|
8
8
|
# @param number [String] the number to calculate the checksum
|
9
9
|
# @param base [Integer] the base used to calculate the checksum
|
10
|
+
# @param uppercase [Boolean] if `true` the result will be in upper case
|
10
11
|
# @return [String] the checksum of the given number
|
11
12
|
# @raise [ArgumentError] if the number is not valid in the chosen base
|
12
|
-
def generate(number, base: 10)
|
13
|
+
def generate(number, base: 10, uppercase: false)
|
13
14
|
total = 0
|
14
15
|
|
15
16
|
characters = number.split('')
|
@@ -27,7 +28,8 @@ class Luhn
|
|
27
28
|
end
|
28
29
|
|
29
30
|
# Return the value we'd have to sum to the total to have a number divisible by 36
|
30
|
-
(-total % base).to_s(base)
|
31
|
+
result = (-total % base).to_s(base)
|
32
|
+
uppercase ? result.upcase : result
|
31
33
|
end
|
32
34
|
|
33
35
|
# Checks if the last digit of the number is the checksum of the rest
|
@@ -37,7 +39,7 @@ class Luhn
|
|
37
39
|
# @return [Boolean] the validity of the checksum
|
38
40
|
# @raise [ArgumentError] if the number is not valid in the chosen base
|
39
41
|
def validate(number, base: 10)
|
40
|
-
number[-1] == generate(number[0...-1], base: base)
|
42
|
+
number[-1].downcase == generate(number[0...-1], base: base)
|
41
43
|
end
|
42
44
|
|
43
45
|
private
|