bancomer36 0.0.1
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.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/bancomer36.gemspec +19 -0
- data/lib/bancomer36.rb +102 -0
- data/lib/bancomer36/version.rb +3 -0
- data/spec/bancomer36_spec.rb +55 -0
- metadata +66 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Hector Sansores
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Bancomer36
|
2
|
+
|
3
|
+
Calculates check digit according to Bancomer 36 algorithm
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'bancomer36'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bancomer36
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bancomer36.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/bancomer36/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Hector Sansores"]
|
6
|
+
gem.email = ["hector.sansores@virtus.com.mx"]
|
7
|
+
gem.description = %q{Bancomer 36 check digit}
|
8
|
+
gem.summary = %q{Calculates check digit according to Bancomer 36 algorithm}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "bancomer36"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Bancomer36::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rspec', '~> 2.10'
|
19
|
+
end
|
data/lib/bancomer36.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require "bancomer36/version"
|
2
|
+
|
3
|
+
module Bancomer36
|
4
|
+
class CheckDigit
|
5
|
+
|
6
|
+
# Replaces letters in input string with numbers
|
7
|
+
# input is a string containing letters from A to Z and numbers from 0 to 9
|
8
|
+
def letter_sub(input)
|
9
|
+
hash = {
|
10
|
+
'A' => '1', 'B' => '2', 'C' => '3', 'D' => '4', 'E' => '5', 'F' => '6',
|
11
|
+
'G' => '7', 'H' => '8', 'I' => '9', 'J' => '1', 'K' => '2', 'L' => '3',
|
12
|
+
'M' => '4', 'N' => '5', 'O' => '6', 'P' => '7', 'Q' => '8', 'R' => '9',
|
13
|
+
'S' => '1', 'T' => '2', 'U' => '3', 'V' => '4', 'W' => '5', 'X' => '6',
|
14
|
+
'Y' => '7', 'Z' => '8', '0' => '0', '1' => '1', '2' => '2', '3' => '3',
|
15
|
+
'4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9'
|
16
|
+
}
|
17
|
+
output = ""
|
18
|
+
input.each_char { |c| output << hash[c] }
|
19
|
+
output
|
20
|
+
end
|
21
|
+
|
22
|
+
# Converts a string containing numbers to an array of integers
|
23
|
+
def to_int_array(input)
|
24
|
+
input.chars.to_a.collect { |x| x.to_i }
|
25
|
+
end
|
26
|
+
|
27
|
+
# Multiplies by 2 the elements in even positions (0, 2, 4...)
|
28
|
+
# input is a array of integers with values ranging from 0 to 9
|
29
|
+
def multiply_by_2(input)
|
30
|
+
output = []
|
31
|
+
input.reverse!
|
32
|
+
input.each_with_index do |value, index|
|
33
|
+
if index % 2 == 0
|
34
|
+
output << value * 2
|
35
|
+
else
|
36
|
+
output << value
|
37
|
+
end
|
38
|
+
end
|
39
|
+
output.reverse!
|
40
|
+
end
|
41
|
+
|
42
|
+
# Sums units and tens for each element greater than 9
|
43
|
+
# input is a array of integers with values ranging from 0 to 18
|
44
|
+
def sums_units_and_tens(input)
|
45
|
+
hash = {
|
46
|
+
10 => 1, 11 => 2, 12 => 3, 13 => 4, 14 => 5,
|
47
|
+
15 => 6, 16 => 7, 17 => 8, 18 => 9
|
48
|
+
}
|
49
|
+
output = []
|
50
|
+
input.each do |x|
|
51
|
+
if x > 9
|
52
|
+
output << hash[x]
|
53
|
+
else
|
54
|
+
output << x
|
55
|
+
end
|
56
|
+
end
|
57
|
+
output
|
58
|
+
end
|
59
|
+
|
60
|
+
# Sums all elements
|
61
|
+
# input is a array of integers with values ranging from 0 to 9
|
62
|
+
def sums_all_elements(input)
|
63
|
+
input.inject(:+)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Substracts from the next multiple of ten
|
67
|
+
# input is an integer
|
68
|
+
def substracts_from_next_ten(input)
|
69
|
+
output = 10 - input % 10
|
70
|
+
output = 0 if output == 10
|
71
|
+
output
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns the check digit
|
75
|
+
# input is a string containing letters from A to Z and numbers from 0 to 9
|
76
|
+
def check_digit(input)
|
77
|
+
input = letter_sub(input)
|
78
|
+
input = to_int_array(input)
|
79
|
+
input = multiply_by_2(input)
|
80
|
+
input = sums_units_and_tens(input)
|
81
|
+
input = sums_all_elements(input)
|
82
|
+
input = substracts_from_next_ten(input)
|
83
|
+
input
|
84
|
+
end
|
85
|
+
|
86
|
+
# Appends the check digit
|
87
|
+
# input is a string containing letters from A to Z and numbers from 0 to 9
|
88
|
+
def append_check_digit(input)
|
89
|
+
input + check_digit(input).to_s
|
90
|
+
end
|
91
|
+
|
92
|
+
# Validates the check digit
|
93
|
+
# input is a string containing letters from A to Z and numbers from 0 to 9
|
94
|
+
# and a check digit at the end
|
95
|
+
def check_digit_valid?(input)
|
96
|
+
digit = input[-1].to_i
|
97
|
+
input = input[0..input.length-2]
|
98
|
+
input = input
|
99
|
+
check_digit(input) == digit
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'bancomer36'
|
2
|
+
|
3
|
+
describe Bancomer36::CheckDigit do
|
4
|
+
before :each do
|
5
|
+
@cd = Bancomer36::CheckDigit.new
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'internal methods' do
|
9
|
+
it 'replaces letters with numbers' do
|
10
|
+
@cd.letter_sub('ABCDEFGHI').should == '123456789'
|
11
|
+
@cd.letter_sub('JKLMNOPQR').should == '123456789'
|
12
|
+
@cd.letter_sub('STUVWXYZ' ).should == '12345678'
|
13
|
+
end
|
14
|
+
it 'leaves numbers unchanged' do
|
15
|
+
@cd.letter_sub('0123456789').should == '0123456789'
|
16
|
+
end
|
17
|
+
it 'converts a string to an array of ints' do
|
18
|
+
@cd.to_int_array('0123456789').should == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
19
|
+
end
|
20
|
+
it 'multiplies by 2 each other element starting from the right' do
|
21
|
+
input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
22
|
+
@cd.multiply_by_2(input).should == [0, 2, 2, 6, 4, 10, 6, 14, 8, 18]
|
23
|
+
end
|
24
|
+
it 'sums units and tens for each element greater than 9' do
|
25
|
+
input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
|
26
|
+
output = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
27
|
+
@cd.sums_units_and_tens(input).should == output
|
28
|
+
end
|
29
|
+
it 'sums all the elements' do
|
30
|
+
input = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
31
|
+
@cd.sums_all_elements(input).should == 45
|
32
|
+
end
|
33
|
+
it 'substracts from the next multiple of ten' do
|
34
|
+
@cd.substracts_from_next_ten(45).should == 5
|
35
|
+
@cd.substracts_from_next_ten(30).should == 0
|
36
|
+
end
|
37
|
+
end
|
38
|
+
context 'user methods' do
|
39
|
+
it 'returns the check digit' do
|
40
|
+
@cd.check_digit('ABCDEF01235').should == 0
|
41
|
+
@cd.check_digit('010A55001017').should == 3
|
42
|
+
@cd.check_digit('54AE05MPHAC9').should == 3
|
43
|
+
@cd.check_digit('180S60003011').should == 8
|
44
|
+
@cd.check_digit('30FF69000045').should == 8
|
45
|
+
@cd.check_digit('AZ00035RG00002L87').should == 1
|
46
|
+
end
|
47
|
+
it 'appends the check digit' do
|
48
|
+
@cd.append_check_digit('ABCDEF01235').should == 'ABCDEF012350'
|
49
|
+
end
|
50
|
+
it 'validates the check digit' do
|
51
|
+
@cd.check_digit_valid?('ABCDEF012350').should be_true
|
52
|
+
@cd.check_digit_valid?('ABCDEF012359').should be_false
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bancomer36
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Hector Sansores
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-30 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &17213080 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.10'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *17213080
|
25
|
+
description: Bancomer 36 check digit
|
26
|
+
email:
|
27
|
+
- hector.sansores@virtus.com.mx
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- bancomer36.gemspec
|
38
|
+
- lib/bancomer36.rb
|
39
|
+
- lib/bancomer36/version.rb
|
40
|
+
- spec/bancomer36_spec.rb
|
41
|
+
homepage: ''
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.15
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Calculates check digit according to Bancomer 36 algorithm
|
65
|
+
test_files:
|
66
|
+
- spec/bancomer36_spec.rb
|