vindicate 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +28 -0
- data/Rakefile +12 -0
- data/lib/vindicate.rb +7 -0
- data/lib/vindicate/version.rb +3 -0
- data/lib/vindicate/vin_validator.rb +39 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 03ce9a107d41f1ce17ef72ea17d17c8b8729d224fc319560f2afc8260a7fac19
|
4
|
+
data.tar.gz: f391872f4571ce8fae810e93545d8b3029f73dbf147133debecaf9cdb36dd3bd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c9a3d9d32c09ef8dc74ff52532aa415ad194e9127b27ee7565f578c4b8091c45272220916f650145a9e7bb3c9fbef7b881d37f30237f02e622034d9d50198b90
|
7
|
+
data.tar.gz: 6502bab3715a63dc21bb6c3fe3458326831e050bbbd15cbda2ff7edbd530d7540f7233e72bcccf22473f3d757c12ad1656fd5ec76739a28c312cb23d6ef4dd6d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 stevensona
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Vindicate
|
2
|
+
Provides Vehicle Identification Number (VIN) number format for ActiveModel. Checks length, characters, and checksum calculation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
```ruby
|
6
|
+
#app/models/vin.rb
|
7
|
+
validates :number, vin: true # validates 'number' attribute is proper vin format
|
8
|
+
```
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'vindicate'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
```bash
|
19
|
+
$ bundle
|
20
|
+
```
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
```bash
|
24
|
+
$ gem install vindicate
|
25
|
+
```
|
26
|
+
|
27
|
+
## License
|
28
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'bundler/gem_tasks'
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new('spec')
|
11
|
+
|
12
|
+
task default: :spec
|
data/lib/vindicate.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class VinValidator < ActiveModel::EachValidator
|
2
|
+
VALID_CHARS = /[A-HJ-NPR-Z0-9]{17}/i
|
3
|
+
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
if value.length < 17
|
6
|
+
record.errors[attribute] << 'is too short'
|
7
|
+
return
|
8
|
+
end
|
9
|
+
if value.length > 17
|
10
|
+
record.errors[attribute] << 'is too long'
|
11
|
+
return
|
12
|
+
end
|
13
|
+
unless value =~ VALID_CHARS
|
14
|
+
record.errors[attribute] << 'contains invalid characters'
|
15
|
+
return
|
16
|
+
end
|
17
|
+
record.errors[attribute] << 'is invalid' unless valid?(value)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def transliterate(c)
|
23
|
+
'0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ'.chars.find_index(c) % 10
|
24
|
+
end
|
25
|
+
|
26
|
+
def calculate_check_digit(vin)
|
27
|
+
map = '0123456789X'.split('')
|
28
|
+
weights = '8765432X098765432'
|
29
|
+
sum = vin.split('').each_with_index.reduce(0) do |m, (c, i)|
|
30
|
+
m + transliterate(c) * map.find_index(weights[i])
|
31
|
+
end
|
32
|
+
map[sum % 11]
|
33
|
+
end
|
34
|
+
|
35
|
+
def valid?(vin)
|
36
|
+
calculate_check_digit(vin) == vin[8]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vindicate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- stevensona
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: shoulda-matchers
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
description: Provides VIN number validator for ActiveModel
|
56
|
+
email:
|
57
|
+
- adamstevenson121@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- MIT-LICENSE
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- lib/vindicate.rb
|
66
|
+
- lib/vindicate/version.rb
|
67
|
+
- lib/vindicate/vin_validator.rb
|
68
|
+
homepage: https://github.com/stevensona/vindicate
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.7.6
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: VIN Number validator for ActiveModel
|
92
|
+
test_files: []
|