vindetta 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a3900e8e738013d538a9ad3640ed2fce1d2dd0d4
4
- data.tar.gz: fa7efc3df758cf3dc570db2d96866f47b606f14c
3
+ metadata.gz: 8c49fca14fbaabb7d61f5ec8a1407c9d58d49a08
4
+ data.tar.gz: fd70297a6f888678684f4ff4fb740695385badee
5
5
  SHA512:
6
- metadata.gz: 5a5040aa6110a0c377317820296587847c2f681af4b530038f1f196cfc9f612e74f6c7a558fc932df0fc20c9e22a972d7549efb96179cdd13041185b178f2746
7
- data.tar.gz: bac8e88bfc0e74ccd1f52e4909e3f356b684c45ab3f828f603f6567b7913559f5230510bff1fe48a70d490f056a0755e4408f806edab3e31b153e1088b7a47db
6
+ metadata.gz: ec71fe44e68b55b824c9b39de7d169eddc3394367c17e5d2e4c05c89f9bda71121281c1059c53b62650b06c44bafa9f3849c5590a1027a9815db34d7db4e8359
7
+ data.tar.gz: c117237631e813aedda91bbb9a13ea8d95b36e331ca889af12d8d4a7fc9cfa07ae8f560f6b0a3541c1602ee8eca26e3217574d2ec2c1dd13f0c1252a1948f483
@@ -0,0 +1,24 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ module Vindetta
5
+ class Api
6
+ def self.get(vin)
7
+ uri = URI("https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/#{vin}?format=json")
8
+
9
+ http = Net::HTTP.new(uri.host, uri.port)
10
+ http.use_ssl = true
11
+
12
+ request = Net::HTTP::Get.new(uri.request_uri)
13
+ response = http.request(request)
14
+
15
+ parse(http.request(request))
16
+ end
17
+
18
+ private
19
+
20
+ def self.parse(response)
21
+ JSON.parse(response.body)
22
+ end
23
+ end
24
+ end
data/lib/vindetta/cli.rb CHANGED
@@ -30,9 +30,7 @@ module Vindetta
30
30
 
31
31
  exit_now!(I18n.t("required"), 1) if vin.nil?
32
32
 
33
- valid = Vindetta::Validator.run(vin)
34
-
35
- puts valid ? 1 : 0
33
+ puts Vindetta::Validator.run(vin)
36
34
  end
37
35
  end
38
36
 
@@ -1,13 +1,31 @@
1
- require "httparty"
2
1
  require "vindetta/decoder/result"
2
+ require "net/http"
3
+ require "json"
3
4
 
4
5
  module Vindetta
5
6
  class Decoder
6
7
  def self.decode_vin(vin)
7
- result = HTTParty.get("https://vpic.nhtsa.dot.gov/api/vehicles/decodevin/#{vin}?format=json")
8
- json = JSON.parse(result.body)
8
+ Result.new(Api.get(vin)["Results"])
9
+ end
10
+
11
+ def self.plant_code(vin)
12
+ vin[10]
13
+ end
14
+
15
+ def self.check_digit(vin)
16
+ vin[8]
17
+ end
18
+
19
+ def self.wmi(vin)
20
+ vin[0..2]
21
+ end
22
+
23
+ def self.vds(vin)
24
+ vin[3..7]
25
+ end
9
26
 
10
- Result.new(json["Results"])
27
+ def self.year(vin)
28
+ decode_vin(vin).year
11
29
  end
12
30
  end
13
31
  end
@@ -1,3 +1,4 @@
1
1
  en:
2
2
  required: "vin is required"
3
3
  invalid_length: "Invalid Length"
4
+ invalid_check_digit: "Invalid Check Digit"
@@ -1,17 +1,23 @@
1
- require "active_model"
2
-
3
1
  module Vindetta
4
- class Validator < ActiveModel::Validator
2
+ class Validator
5
3
  LENGTH = 17
4
+ CHECK_DIGIT_INDEX = 8
5
+ MAP = "0123456789X".chars
6
+ WEIGHTS = "8765432X098765432".chars
6
7
 
7
8
  def self.run(value)
8
- Vin.new(value).valid?
9
+ return false unless value.length == LENGTH
10
+
11
+ check_digit(value) == value[CHECK_DIGIT_INDEX]
9
12
  end
10
13
 
11
- def validate(record)
12
- record.errors.add(:value, I18n.t("invalid_length")) if record.value.length != LENGTH
14
+ def self.check_digit(value)
15
+ check_digit = value.chars[8]
16
+ calculated = value.chars.map.with_index do |c, i|
17
+ Transliterator::run(c) * MAP.find_index(WEIGHTS[i])
18
+ end.sum
13
19
 
14
- # TODO: Check format
20
+ MAP[calculated % 11]
15
21
  end
16
22
  end
17
23
  end
@@ -1,3 +1,3 @@
1
1
  module Vindetta
2
- VERSION = "0.15.0".freeze
2
+ VERSION = "0.16.0".freeze
3
3
  end
data/lib/vindetta.rb CHANGED
@@ -3,12 +3,9 @@ require "vindetta/errors"
3
3
  require "vindetta/generator"
4
4
  require "vindetta/transliterator"
5
5
  require "vindetta/validator"
6
+ require "vindetta/api"
6
7
  require "vindetta/decoder"
7
8
  require "vindetta/version"
8
- require "vindetta/vin"
9
- require "vindetta/vin/wmi"
10
- require "vindetta/vin/vds"
11
- require "vindetta/vin/vis"
12
9
 
13
10
  module Vindetta
14
11
  def self.generate(options = {})
data/vindetta.gemspec CHANGED
@@ -22,11 +22,8 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ["lib"]
24
24
 
25
- spec.add_dependency "activemodel", "~> 5.1"
26
- spec.add_dependency "activesupport", "~> 5.1"
27
25
  spec.add_dependency "gli", "~> 2.16"
28
26
  spec.add_dependency "i18n", "~> 0.9.1"
29
- spec.add_dependency "httparty", "~> 0.15.6"
30
27
 
31
28
  spec.add_development_dependency "bundler", "~> 1.14"
32
29
  spec.add_development_dependency "factory_bot", "~> 4.8.2"
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vindetta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.16.0
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-22 00:00:00.000000000 Z
11
+ date: 2017-12-27 00:00:00.000000000 Z
12
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: '5.1'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '5.1'
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '5.1'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '5.1'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: gli
43
15
  requirement: !ruby/object:Gem::Requirement
@@ -66,20 +38,6 @@ dependencies:
66
38
  - - "~>"
67
39
  - !ruby/object:Gem::Version
68
40
  version: 0.9.1
69
- - !ruby/object:Gem::Dependency
70
- name: httparty
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 0.15.6
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 0.15.6
83
41
  - !ruby/object:Gem::Dependency
84
42
  name: bundler
85
43
  requirement: !ruby/object:Gem::Requirement
@@ -227,6 +185,7 @@ files:
227
185
  - bin/setup
228
186
  - exe/vindetta
229
187
  - lib/vindetta.rb
188
+ - lib/vindetta/api.rb
230
189
  - lib/vindetta/cli.rb
231
190
  - lib/vindetta/data/vis.yaml
232
191
  - lib/vindetta/data/wmi.yaml
@@ -239,7 +198,6 @@ files:
239
198
  - lib/vindetta/transliterator.rb
240
199
  - lib/vindetta/validator.rb
241
200
  - lib/vindetta/version.rb
242
- - lib/vindetta/vin.rb
243
201
  - lib/vindetta/vin/vds.rb
244
202
  - lib/vindetta/vin/vis.rb
245
203
  - lib/vindetta/vin/wmi.rb
data/lib/vindetta/vin.rb DELETED
@@ -1,76 +0,0 @@
1
- require "active_support"
2
- require "active_model"
3
-
4
- module Vindetta
5
- class Vin
6
- include ActiveModel::Validations
7
- include Enumerable
8
-
9
- def each(&block)
10
- @value.chars(&block)
11
- end
12
-
13
- def [](index)
14
- each[index]
15
- end
16
-
17
- attr_reader :value
18
-
19
- validates_with Validator
20
-
21
- def initialize(value)
22
- @value = value
23
- end
24
-
25
- alias eql? ==
26
-
27
- def ==(other)
28
- self.class == other.class && value == other.value
29
- end
30
-
31
- def check_digit
32
- value[8]
33
- end
34
-
35
- def year
36
- vis.year
37
- end
38
-
39
- def region
40
- wmi.region
41
- end
42
-
43
- ##
44
- # World Manufacturer Identity
45
- #
46
- def wmi
47
- @wmi ||= Wmi.new(self)
48
- end
49
-
50
- # #
51
- # Vehicle Descriptor Section
52
- #
53
- def vds
54
- @vds ||= Vds.new(self)
55
- end
56
-
57
- ##
58
- # Vehicle Identifier Section
59
- #
60
- def vis
61
- @vis ||= Vis.new(self)
62
- end
63
-
64
- def model_year
65
- value[9]
66
- end
67
-
68
- def manufacturing_plant
69
- value[10]
70
- end
71
-
72
- def serial_number
73
- value[11..16]
74
- end
75
- end
76
- end