vindetta 0.17.0 → 0.17.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4f38d44cf4f767c61e1075d998785e2b3db2b24
4
- data.tar.gz: 5e5a641a4794a50b8189d6c735a07e0f3a8853bb
3
+ metadata.gz: dc052c66bde259587a11d28f3b5639872d5e1dc1
4
+ data.tar.gz: 52340e3f20255fe4aee6533b6f30038ef970b6b9
5
5
  SHA512:
6
- metadata.gz: 1893fdec6e3f16ada0960216a114a292088bfaa125e1b2cc9887ab46ae822eb3d3564d84e48a21f75e51bb5545aee80c48f0de49f3259d25714c200cfe906c79
7
- data.tar.gz: 0d110c315e1ec7fcece6031acd4f9c964567e05b2069db0ca81a944eb33106153135a1da77ce74d3a782c8a54a1bd586fc43a20f2cd4ce7c95e13a2260c6dcea
6
+ metadata.gz: 14306fe4f6bfd2dade7f8e922953336d3fe809908d02fdeb52207b3f476122653197d549d3899d7d63282a083fdbf87a234435ddb2aaaf0b232d0515b6eaf228
7
+ data.tar.gz: 9bb861898cb7ba53e2642712c07cc466ce14e91271f99797b6b3665de0fd840adea5b947faaf9d47586a7db5b40cd5925cb4b4976052e78f2deb88b17044e110
data/lib/vindetta.rb CHANGED
@@ -5,6 +5,7 @@ require "vindetta/transliterator"
5
5
  require "vindetta/validator"
6
6
  require "vindetta/api"
7
7
  require "vindetta/decoder"
8
+ require "vindetta/calculator"
8
9
  require "vindetta/version"
9
10
 
10
11
  module Vindetta
@@ -0,0 +1,15 @@
1
+ module Vindetta
2
+ class Calculator
3
+ MAP = %w(0 1 2 3 4 5 6 7 8 9 X)
4
+ WEIGHTS = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2]
5
+
6
+ def self.check_digit(vin)
7
+ sum = Transliterator
8
+ .vin(vin)
9
+ .zip(WEIGHTS)
10
+ .reduce(0) {|sum, (a, b)| sum + (a * b) }
11
+
12
+ MAP[sum % 11]
13
+ end
14
+ end
15
+ end
@@ -4,6 +4,8 @@ require "json"
4
4
 
5
5
  module Vindetta
6
6
  class Decoder
7
+ CHECK_DIGIT_INDEX = 8
8
+
7
9
  def self.vin(vin)
8
10
  Result.new(Api.get(vin)["Results"])
9
11
  end
@@ -13,7 +15,7 @@ module Vindetta
13
15
  end
14
16
 
15
17
  def self.check_digit(vin)
16
- vin[8]
18
+ vin[CHECK_DIGIT_INDEX]
17
19
  end
18
20
 
19
21
  def self.wmi(vin)
@@ -24,7 +26,7 @@ module Vindetta
24
26
  defaults = { :check_digit => true }
25
27
  options = defaults.merge(options)
26
28
 
27
- vin[3..8].tap do |vds|
29
+ vin[3..CHECK_DIGIT_INDEX].tap do |vds|
28
30
  vds.chop! unless options[:check_digit]
29
31
  end
30
32
  end
@@ -2,6 +2,10 @@ module Vindetta
2
2
  class Transliterator
3
3
  MAPPING = "0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ".split("").freeze
4
4
 
5
+ def self.vin(vin)
6
+ vin.chars.map { |c| run(c) }
7
+ end
8
+
5
9
  def self.run(character)
6
10
  index = MAPPING.find_index(character)
7
11
  raise Vindetta::InvalidCharacter, character unless index
@@ -1,25 +1,15 @@
1
1
  module Vindetta
2
2
  class Validator
3
- LENGTH = 17
4
- MAP = "0123456789X".chars
5
- WEIGHTS = "8765432X98765432".chars
6
-
7
3
  def self.vin(vin)
8
- return false unless vin.length == LENGTH
4
+ length(vin) && check_digit(vin)
5
+ end
9
6
 
10
- check_digit(vin) == Decoder.check_digit(vin)
7
+ def self.length(vin)
8
+ vin.length == 17
11
9
  end
12
10
 
13
11
  def self.check_digit(vin)
14
- wmi = Decoder.wmi(vin).chars
15
- vds = Decoder.vds(vin, :check_digit => false).chars
16
- vis = Decoder.vis(vin).chars
17
-
18
- calculated = [wmi, vds, vis].flatten.map.with_index do |c, i|
19
- Transliterator::run(c) * MAP.find_index(WEIGHTS[i])
20
- end.sum
21
-
22
- MAP[calculated % 11]
12
+ Calculator.check_digit(vin) == Decoder.check_digit(vin)
23
13
  end
24
14
  end
25
15
  end
@@ -1,3 +1,3 @@
1
1
  module Vindetta
2
- VERSION = "0.17.0".freeze
2
+ VERSION = "0.17.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vindetta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Decot
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-29 00:00:00.000000000 Z
11
+ date: 2017-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gli
@@ -200,6 +200,7 @@ files:
200
200
  - exe/vindetta
201
201
  - lib/vindetta.rb
202
202
  - lib/vindetta/api.rb
203
+ - lib/vindetta/calculator.rb
203
204
  - lib/vindetta/cli.rb
204
205
  - lib/vindetta/data/vis.yaml
205
206
  - lib/vindetta/data/wmi.yaml