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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c88cdee72a10369be227c5e17055b5322411525e2c5cf1dbedfb631141b2bd4f
4
- data.tar.gz: e9b62ef2301e4f2eb7c3deac99c526eb3c33a24edd006289f9f1cbbf10860a66
3
+ metadata.gz: 1ae87b47647670786be04ec29dbb4257da90c3264e1fdb5cb57df74cd6dc8c2b
4
+ data.tar.gz: 6d209a5a775ea4e95f9dce3daa5b9a190fa79904e72aae23532438e42b0c3a6c
5
5
  SHA512:
6
- metadata.gz: 600ef8ee4b9aee759f362d85a290d8b2b0032d3cc9bba66d9bb2c17fe0b163f196a7824b2dfd8846142120c74817421ca73693e76d05337dcc729a647c1f62aa
7
- data.tar.gz: 5da351094afc901f8960a1e054e7f9d27e086268f70dbef70967b47a7038bb5c5d385ab43fa1d005abacc708c9210d3d473aa8ba4ed5908dcd8e80d3ce2d29dd
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).
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Auto-generated by bin-cc build - DO NOT EDIT
4
- # Generated: 2026-01-11T20:57:15.838Z
4
+ # Generated: 2026-01-11T21:09:29.401Z
5
5
 
6
6
  module CreditcardIdentifier
7
7
  # Credit card brand data
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Auto-generated by bin-cc build - DO NOT EDIT
4
- # Generated: 2026-01-11T20:57:15.839Z
4
+ # Generated: 2026-01-11T21:09:29.401Z
5
5
 
6
6
  module CreditcardIdentifier
7
7
  # Credit card brand data (detailed)
@@ -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.0.0'
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: creditcard-identifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Renato Viço