vindicator 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.
- data/.gitignore +17 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +1 -0
- data/lib/vindicator.rb +39 -0
- data/lib/vindicator/version.rb +3 -0
- data/test/vindicator_test.rb +41 -0
- data/vindicator.gemspec +19 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Johnathan Ludwig
|
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,32 @@
|
|
1
|
+
# VINdicator
|
2
|
+
|
3
|
+
VINdicator is a small gem that will allow you to verify that a VIN is valid based on its check digit.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'vindicator'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install vindicator
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Using a VIN, call valid_vin? and it will return true or false.
|
22
|
+
|
23
|
+
$ Vindicator.valid_vin?('5GZCZ43D13S812715') => true
|
24
|
+
$ Vindicator.valid_vin?('TGZCZ43D13S812715') => false
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/vindicator.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "vindicator/version"
|
2
|
+
|
3
|
+
module Vindicator
|
4
|
+
# This method checks that an auto VIN is valid against its check digit
|
5
|
+
|
6
|
+
# Example
|
7
|
+
# valid_vin?('1GNEC233X9R191831')
|
8
|
+
|
9
|
+
def self.valid_vin?(vin)
|
10
|
+
# I, O, and Q are not valid letters in a VIN. A valid VIN is exactly 17 characters in length
|
11
|
+
vin = vin.to_s.downcase.gsub(/(i|o|q|\W|_)/i,'').split('')
|
12
|
+
return false if vin.size != 17
|
13
|
+
|
14
|
+
# The 9th character is the check digit
|
15
|
+
check_digit = vin[8]
|
16
|
+
|
17
|
+
# Set up the numeric values for alpha characters and the weight for each position in the VIN
|
18
|
+
alpha_values = {'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8,
|
19
|
+
'j' => 1, 'k' => 2, 'l' => 3, 'm' => 4, 'n' => 5, 'p' => 7, 'r' => 9,
|
20
|
+
's' => 2, 't' => 3, 'u' => 4, 'v' => 5, 'w' => 6, 'x' => 7, 'y' => 8, 'z' => 9}
|
21
|
+
weight = [8,7,6,5,4,3,2,10,0,9,8,7,6,5,4,3,2]
|
22
|
+
|
23
|
+
total = 0
|
24
|
+
vin.each_with_index do |num,index|
|
25
|
+
next if index == 8 # skip the check digit
|
26
|
+
if num =~ /\d/
|
27
|
+
total += num.to_i * weight[index]
|
28
|
+
elsif num =~ /[a-z]/
|
29
|
+
total += alpha_values[num] * weight[index]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# The total divided my the remainder should be the same as the check digit.
|
34
|
+
# The check digit should be x if the remainder is 10
|
35
|
+
remainder = total % 11
|
36
|
+
remainder = 'x' if remainder == 10
|
37
|
+
check_digit.to_s == remainder.to_s
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'vindicator'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'minitest/autorun'
|
6
|
+
|
7
|
+
class VindicatorTest < MiniTest::Unit::TestCase
|
8
|
+
describe "when using a valid vin" do
|
9
|
+
it "returns true " do
|
10
|
+
assert Vindicator.valid_vin?('1GNEC233X9R191831'), "valid_vin? returned false when a valid VIN was used"
|
11
|
+
assert Vindicator.valid_vin?('2HNYD2H22AH529160'), "valid_vin? returned false when a valid VIN was used"
|
12
|
+
assert Vindicator.valid_vin?('2HNYD18683H545668'), "valid_vin? returned false when a valid VIN was used"
|
13
|
+
assert Vindicator.valid_vin?('5GZCZ43D13S812715'), "valid_vin? returned false when a valid VIN was used"
|
14
|
+
assert Vindicator.valid_vin?('11111111111111111'), "valid_vin? returned false when a valid VIN was used"
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'strips out non alphanumeric characters' do
|
18
|
+
assert Vindicator.valid_vin?('1GN-EC2_33X!9R1@918#31'), "valid_vin? returned false when a valid VIN was used"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "when using invalid vins" do
|
23
|
+
it 'returns false when vin is not 17 characters in length' do
|
24
|
+
# too long
|
25
|
+
assert !Vindicator.valid_vin?('2HNYD2H22AH529160W'), "valid_vin? returned true when an invalid VIN was used"
|
26
|
+
# too short
|
27
|
+
assert !Vindicator.valid_vin?('2HNYD2H22AH52916'), "valid_vin? returned true when an invalid VIN was used"
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'strips out I, O, and Q' do
|
31
|
+
assert !Vindicator.valid_vin?('2HNYD2H22AH52916O'), "valid_vin? returned true when an invalid VIN was used"
|
32
|
+
assert !Vindicator.valid_vin?('2HNYD2H22AH52916I'), "valid_vin? returned true when an invalid VIN was used"
|
33
|
+
assert !Vindicator.valid_vin?('2HNYD2H22AH52916Q'), "valid_vin? returned true when an invalid VIN was used"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns false when the check digit does not match" do
|
37
|
+
assert !Vindicator.valid_vin?('TGZCZ43D13S812715'), "valid_vin? returned true when an invalid VIN was used"
|
38
|
+
assert !Vindicator.valid_vin?('11111111211111111'), "valid_vin? returned true when an invalid VIN was used"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/vindicator.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vindicator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "vindicator"
|
8
|
+
gem.version = Vindicator::VERSION
|
9
|
+
gem.authors = ["Johnathan Ludwig"]
|
10
|
+
gem.email = ["john@johnathanludwig.com"]
|
11
|
+
gem.description = %q{VINdicator is a small gem that will allow you to verify that a VIN is valid based on its check digit.}
|
12
|
+
gem.summary = %q{VINdicator is a small gem that will allow you to verify that a VIN is valid based on its check digit.}
|
13
|
+
gem.homepage = "https://github.com/JohnathanLudwig/vindicator"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vindicator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Johnathan Ludwig
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: VINdicator is a small gem that will allow you to verify that a VIN is
|
15
|
+
valid based on its check digit.
|
16
|
+
email:
|
17
|
+
- john@johnathanludwig.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/vindicator.rb
|
28
|
+
- lib/vindicator/version.rb
|
29
|
+
- test/vindicator_test.rb
|
30
|
+
- vindicator.gemspec
|
31
|
+
homepage: https://github.com/JohnathanLudwig/vindicator
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.25
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: VINdicator is a small gem that will allow you to verify that a VIN is valid
|
55
|
+
based on its check digit.
|
56
|
+
test_files:
|
57
|
+
- test/vindicator_test.rb
|