ruby-luhn 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/luhn.rb +5 -3
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e813da62b03789d2442091a53189d02c1a15c26adca90b3baef13825a2f7ab0e
4
- data.tar.gz: 810eb5b79e84f5301a5536947a3dfe9840150f28a6e6844c72eb5b64eafc670e
3
+ metadata.gz: 608bdb2f2e8df3036a8d79522ca3131bac161321e2c8a753271d8d1c233b1a89
4
+ data.tar.gz: 7d319c1e76b4f694443d4f13566845c8ef6311e35515ce9ad859afd0e0d43835
5
5
  SHA512:
6
- metadata.gz: ef2bbbcb8d7ed4dbc6f347879ccb36c3015218e0d317779ae9d5b00f56f3d7d21472916157907330c2bfeca34f24ee9ade65af9964b0600dbf02b6ad2d303878
7
- data.tar.gz: ec02dc3053fdd3dc708d70602b9823a6178473e88df5214ca81bc9c646f1ab41728c10ada0080f56dd97ba8f6d175ece270356ed1ff8581d2222a49c91c1d1a7
6
+ metadata.gz: 0f190ff020f56519208184fd7f636b62ad0b715c4f5589a54d99d723d4d692e33d68865dd33ad677ba83d00342af2596e4d7367f799faa5005f02ae34508300b
7
+ data.tar.gz: a40f8b5387d36a773aa530110641d3b45167d8843fae014ab40a35001359dbbccb6a604cd31734aa75c9c3a2397b027b593960c6c6298a87373764fc777223b2
@@ -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).upcase
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-luhn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jorge Leites