luhnAlgorithmCheck 0.0.1

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 +7 -0
  2. data/lib/luhnAlgorithmCheck.rb +55 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef328c4564251ed341598e18162ee2e3fbd6fb2c87e8693132c638f589022022
4
+ data.tar.gz: 53cfd3a2509df39ac69b9deb458ea2688b4c87bf6f65021286c1ede45ae8c28b
5
+ SHA512:
6
+ metadata.gz: a25bbb8a9b7abe253b40bb2caa39d6e81a2aeffd9989053133e35c808f290e036e0e17949435a4c6efef408f84b46b2a8ac9dfa7f25d40af729b3199209e68de
7
+ data.tar.gz: 1591d6bc78aac87f0898a2ba510aa76ba4f4b05c2e59dbaee8518c6f36aa50843d3eaed58c2c049db03f45ac9d55c4e8d94ffa6d682983d956b15767303dbbbc
@@ -0,0 +1,55 @@
1
+ require 'luhnAlgorithmCheck/version.rb'
2
+
3
+ # Reference for below code taken from https://exercism.io/tracks/ruby/exercises/luhn/solutions/d9add5655baa476fb4f97bfa91645112
4
+ class LuhnAlgorithm
5
+ attr_reader :num
6
+
7
+ def initialize(num)
8
+ @num = num
9
+ end
10
+
11
+ def addends
12
+ raw_addends.map do |digit|
13
+ digit < 10 ? digit : digit - 9
14
+ end
15
+ end
16
+
17
+ def checksum
18
+ addends.inject(:+)
19
+ end
20
+
21
+ def valid?
22
+ checksum % 10 == 0
23
+ end
24
+
25
+ def self.create(num)
26
+ [*0..9].each do |digit|
27
+ possible = Luhn.new("#{num}#{digit}".to_i)
28
+ return possible.num if possible.valid?
29
+ end
30
+ end
31
+
32
+ # This function is not part of original logic referred from https://exercism.io/tracks/ruby/exercises/luhn/solutions/d9add5655baa476fb4f97bfa91645112
33
+ # This function is surplus logic referred from http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
34
+ def luhn_valid?(str)
35
+ str.scan(/\d/).reverse #using str.to_i.digits fails for cases with leading zeros
36
+ .each_slice(2)
37
+ .sum { |i, k = 0| i.to_i + ((k.to_i)*2).digits.sum }
38
+ .modulo(10).zero?
39
+ end
40
+
41
+ # This function is not part of original logic referred from https://exercism.io/tracks/ruby/exercises/luhn/solutions/d9add5655baa476fb4f97bfa91645112
42
+ # This function is surplus logic referred from https://medium.com/@akshaymohite/luhns-algorithm-to-validate-credit-debit-card-numbers-1952e6c7a9d0
43
+ def luhn_algo_valid
44
+ number.reverse.split("").each_slice(2) do |x,y|
45
+ sum += x.to_i + (2*y.to_i).divmod(10).sum
46
+ end
47
+ end
48
+
49
+ private
50
+ def raw_addends
51
+ num.to_s.reverse.chars.map.with_index do |digit, index|
52
+ index.odd? ? digit.to_i * 2 : digit.to_i
53
+ end.reverse
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: luhnAlgorithmCheck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Priyesh Shah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-11-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Verifies if the Credit/Debit Card is a valid card using Luhn's Algorithm.
14
+ Returns True if yes, False if no
15
+ email: shahpriyesh2005@yahoo.co.in
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/luhnAlgorithmCheck.rb
21
+ homepage: http://rubygems.org/gems/luhnAlgorithmCheck
22
+ licenses: []
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.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Verifies if the Credit/Debit Card is a valid card using Luhn's Algorithm
43
+ test_files: []