luhn_validate 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 +7 -0
- data/lib/luhn_validate.rb +50 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c3e71654c289bf40da1ad883e139357757cb482335135e45fe9c034bd8b07a2b
|
4
|
+
data.tar.gz: e3c5b37cc9c16355492b482a9b4b1b3e45f048db51d8935a854ea8cc23fda734
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1932ed330cd915211831e07d673d782dd87b5809c71578515952b1dd7b202ee34794c8397db56b02ca2ed8fefdab7405d55ca2f539e0a392826adea3a3044aa3
|
7
|
+
data.tar.gz: 1f109d5572354b58f16b5ff510ee1281351cc48f870f0edfe0451d2d2d672110facb69ba0e63fc09aa7cf3955e6f6c3d415532ae309939426d2c2b67669f5e82
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Validates card pan by Luhn algorithm
|
4
|
+
class Luhn
|
5
|
+
class CardNotPresentError < StandardError; end
|
6
|
+
class PanLengthError < StandardError; end
|
7
|
+
|
8
|
+
attr_reader :pan_array, :card_size, :valid
|
9
|
+
|
10
|
+
def initialize(pan)
|
11
|
+
@pan_array = pan.split('').map(&:to_i)
|
12
|
+
@card_size = @pan_array.size
|
13
|
+
@valid = valid?(calculate_luhn_score)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.validate(pan)
|
17
|
+
raise CardNotPresentError, 'Card not present' unless pan
|
18
|
+
raise PanLengthError, 'Wrong pan length' unless (14..19).include? pan.split('').size
|
19
|
+
|
20
|
+
new(pan)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def calculate_luhn_score
|
26
|
+
prepared_array = []
|
27
|
+
@pan_array.map.with_index do |pan_part, part_index|
|
28
|
+
prepared_array << calculate_pan_part(pan_part, part_index)
|
29
|
+
end
|
30
|
+
|
31
|
+
prepared_array
|
32
|
+
end
|
33
|
+
|
34
|
+
def pan_part_index_callback
|
35
|
+
(@pan_array.length - 1).odd? ? :odd? : :even?
|
36
|
+
end
|
37
|
+
|
38
|
+
def calculate_pan_part(pan_part, pan_part_index)
|
39
|
+
p_part = (pan_part_index + 1).send(pan_part_index_callback) ? (pan_part * 2) : pan_part
|
40
|
+
reveal_part_number(p_part)
|
41
|
+
end
|
42
|
+
|
43
|
+
def reveal_part_number(pan_part)
|
44
|
+
pan_part > 9 ? (pan_part - 9) : pan_part
|
45
|
+
end
|
46
|
+
|
47
|
+
def valid?(pan_array)
|
48
|
+
(pan_array.sum % 10).zero?
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: luhn_validate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- FuryCow
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Validate Card PAN by Luhn algorithm
|
14
|
+
email: dreamweaver0408@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/luhn_validate.rb
|
20
|
+
homepage: https://rubygems.org/gems/luhn_validate
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubygems_version: 3.0.1
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: Luhn Validate
|
43
|
+
test_files: []
|