creditcard-identifier 2.0.1 → 2.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/README.md +18 -0
- data/lib/creditcard_identifier/brands.rb +1 -1
- data/lib/creditcard_identifier/brands_detailed.rb +1 -1
- data/lib/creditcard_identifier.rb +37 -1
- 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: 1ae87b47647670786be04ec29dbb4257da90c3264e1fdb5cb57df74cd6dc8c2b
|
|
4
|
+
data.tar.gz: 6d209a5a775ea4e95f9dce3daa5b9a190fa79904e72aae23532438e42b0c3a6c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 613ec2e5e2e481e1fabb607c8f27b520ed0ab1d8fb8aa55cdf45c7069e1654afc22599a4bcf1db125db888daa8352277db00ecb92bb6765f8b9499b25cca013e
|
|
7
|
+
data.tar.gz: 8a60d7b35b49a581f47a514ca12f04436206d132d2dd9c315f1a6f204d7c345556e4a077d621b8a0e00c8476a7de7b4448e6f3c65267770f72e30cbe3339fe45
|
data/README.md
CHANGED
|
@@ -89,6 +89,14 @@ puts detailed
|
|
|
89
89
|
brands = validator.list_brands
|
|
90
90
|
puts brands
|
|
91
91
|
# ['amex', 'aura', 'banesecard', 'diners', 'discover', 'elo', 'hipercard', 'jcb', 'maestro', 'mastercard', 'unionpay', 'visa']
|
|
92
|
+
|
|
93
|
+
# Validate card number using Luhn algorithm
|
|
94
|
+
is_valid = CreditcardIdentifier.luhn('4012001037141112')
|
|
95
|
+
puts is_valid # true
|
|
96
|
+
|
|
97
|
+
# Or using validator instance
|
|
98
|
+
is_valid = validator.luhn('4012001037141112')
|
|
99
|
+
puts is_valid # true
|
|
92
100
|
```
|
|
93
101
|
|
|
94
102
|
## API
|
|
@@ -163,6 +171,16 @@ List all supported brands.
|
|
|
163
171
|
|
|
164
172
|
**Returns:** (Array<String>) List of brand names
|
|
165
173
|
|
|
174
|
+
#### `luhn(number)`
|
|
175
|
+
Validate a credit card number using the Luhn algorithm.
|
|
176
|
+
|
|
177
|
+
**Parameters:**
|
|
178
|
+
- `number` (String): Credit card number (digits only)
|
|
179
|
+
|
|
180
|
+
**Returns:** (Boolean) true if valid according to Luhn algorithm
|
|
181
|
+
|
|
182
|
+
**Raises:** TypeError if input is not a string
|
|
183
|
+
|
|
166
184
|
## Data Source
|
|
167
185
|
|
|
168
186
|
This library uses the BIN data from the [bin-cc project](https://github.com/renatovico/bin-cc).
|
|
@@ -8,7 +8,34 @@ require_relative 'creditcard_identifier/brands_detailed'
|
|
|
8
8
|
#
|
|
9
9
|
# This module provides credit card validation using bin-cc data.
|
|
10
10
|
module CreditcardIdentifier
|
|
11
|
-
VERSION = '2.
|
|
11
|
+
VERSION = '2.1.0'
|
|
12
|
+
|
|
13
|
+
# Luhn lookup table for doubling digits
|
|
14
|
+
LUHN_LOOKUP = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9].freeze
|
|
15
|
+
|
|
16
|
+
##
|
|
17
|
+
# Validate a credit card number using the Luhn algorithm
|
|
18
|
+
#
|
|
19
|
+
# @param number [String] Credit card number (digits only)
|
|
20
|
+
# @return [Boolean] true if valid according to Luhn algorithm
|
|
21
|
+
# @raise [TypeError] if number is not a string
|
|
22
|
+
def self.luhn(number)
|
|
23
|
+
raise TypeError, 'Expected string input' unless number.is_a?(String)
|
|
24
|
+
return false if number.empty?
|
|
25
|
+
|
|
26
|
+
total = 0
|
|
27
|
+
x2 = true
|
|
28
|
+
|
|
29
|
+
(number.length - 1).downto(0) do |i|
|
|
30
|
+
value = number[i].ord - 48
|
|
31
|
+
return false if value < 0 || value > 9
|
|
32
|
+
|
|
33
|
+
x2 = !x2
|
|
34
|
+
total += x2 ? LUHN_LOOKUP[value] : value
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
(total % 10).zero?
|
|
38
|
+
end
|
|
12
39
|
|
|
13
40
|
##
|
|
14
41
|
# Credit card validator using bin-cc data.
|
|
@@ -132,6 +159,15 @@ module CreditcardIdentifier
|
|
|
132
159
|
def list_brands
|
|
133
160
|
@brands.map { |b| b[:name] }
|
|
134
161
|
end
|
|
162
|
+
|
|
163
|
+
##
|
|
164
|
+
# Validate a credit card number using the Luhn algorithm
|
|
165
|
+
#
|
|
166
|
+
# @param number [String] Credit card number (digits only)
|
|
167
|
+
# @return [Boolean] true if valid according to Luhn algorithm
|
|
168
|
+
def luhn(number)
|
|
169
|
+
CreditcardIdentifier.luhn(number)
|
|
170
|
+
end
|
|
135
171
|
end
|
|
136
172
|
|
|
137
173
|
# Module-level convenience methods
|