number_plate_validator 0.1.1 → 0.2.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: 28c143b186ad31f5a75070038a748bb12c031fea
4
- data.tar.gz: 3684c2428985b85bcaf6f91d798218063cac689f
3
+ metadata.gz: a607f35f638f4e293f00a6cdc59c809963ee544b
4
+ data.tar.gz: 77e1816956a297fb6aa53fd685d8c8f4421c5608
5
5
  SHA512:
6
- metadata.gz: 52dcfe935f18eee48c9e190664c8391a9cfce94de70ce024ac20f954f105107e9b147c413897cf7090f3234dd0f1b70dbb66b84241c541a3ad367f94ac090849
7
- data.tar.gz: 0dd935f2db1e1b59d7c7cfa05b7869cc710c4810bde5cb4470652925253ff26163c430eb2da9c7e97c3c609fc6b373e68285f71cd88546ca8ce53c146eeca225
6
+ metadata.gz: d1a13dc0f92365be40cf10b68f63fad00c13b7588c69fe113ba0eff55d0868a41cd1ac050499febe12b57410c7f5e43f4bba694f4b2608cc6774b569c76747e8
7
+ data.tar.gz: '06946cbe93a5cff0c766f655c0331009c873740ee781c35019091d440f59c49bfa7e9632f83a03e95438d05f4b36bbb0495c8d48ee82d55c4211b5ff10aeb159'
data/README.md CHANGED
@@ -1,6 +1,12 @@
1
1
  # NumberPlateValidator
2
2
 
3
- This gem provides a validator for license plate validation. At the moment it only supports Singapore plates with a checksum. But work is ongoing to expand it to special plates and other countries and regions.
3
+ This gem provides a validator for license plate validation. It iss being actively developed to expand to special plates and more countries and regions. Contributions are welcome.
4
+
5
+ ## Country Support
6
+
7
+ Singapore: checksumed plates
8
+ Malaysia: Not exhaustive. Please check the specs for examples of covered cases
9
+
4
10
 
5
11
  ## Installation
6
12
 
@@ -31,6 +37,10 @@ Use standard country code to spcify the country.
31
37
 
32
38
  validator.is_valid?("EJ81E")
33
39
  #=> false
40
+
41
+ ##get the current country code
42
+ validator.country
43
+ #=> "SG"
34
44
  ```
35
45
 
36
46
  ## ActiveModel
@@ -47,6 +57,16 @@ class Vehicle < ActiveRecord::Base
47
57
  end
48
58
 
49
59
  ```
60
+
61
+ ## Command Line Interface
62
+ A CLI is provided to check the plate validity
63
+
64
+ ```
65
+ $number_plate validate -c SG EJ81E
66
+ $true
67
+
68
+ ```
69
+
50
70
  ## Development
51
71
 
52
72
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/bin/number_plate ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'number_plate_validator/cli'
4
+
5
+ NumberPlateValidator::CLI.start
@@ -3,14 +3,18 @@ require "active_support/inflector"
3
3
  require "active_model"
4
4
  require "active_support/core_ext/string"
5
5
  require "number_plate_validator/validator"
6
+ require "number_plate_validator/country_validator_base"
6
7
  require "number_plate_validator/sg_validator"
8
+ require "number_plate_validator/my_validator"
7
9
  require "number_plate_validator/active_model"
8
10
 
9
11
  module NumberPlateValidator
10
12
 
11
13
  class << self
12
14
  def validator(country_code)
13
- Validator.new(country_code)
15
+ validator_class = eval "NumberPlateValidator::#{country_code}Validator"
16
+ validator = validator_class.new
17
+ Validator.new(validator)
14
18
  end
15
19
  end
16
20
 
@@ -0,0 +1,15 @@
1
+ require 'thor'
2
+ require 'number_plate_validator'
3
+
4
+ module NumberPlateValidator
5
+ class CLI < Thor
6
+ desc "validate -c CC PLATE", "validate if a PLATE is valid in the country CC"
7
+ method_option :country, aliases: "-c", desc: "standard country code in capital letters", required: true
8
+ def validate(plate)
9
+ validator = NumberPlateValidator.validator(options[:country])
10
+ puts(validator.is_valid? plate)
11
+ end
12
+
13
+ end
14
+ end
15
+
@@ -0,0 +1,12 @@
1
+ module NumberPlateValidator
2
+ class CountryValidatorBase
3
+
4
+ attr_reader :country
5
+
6
+ def is_valid?(registration_number)
7
+ @license = registration_number.gsub(/\s/, "").upcase if registration_number.present?
8
+ @license.present?
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,29 @@
1
+ module NumberPlateValidator
2
+ class MYValidator < CountryValidatorBase
3
+
4
+ PATTERNS = [
5
+ /^[A,B,C,D,F,J,K,M,N,P,R,T,V][A-Y]{0,2}([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$/,
6
+ /^W[A-Y]{0,2}([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[A-Y]{0,1}$/,
7
+ /^KV([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[A-Y]$/,
8
+ /^Q[A,K,B,C,L,M,P,R,S,T][A-Y]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[A-Y]$/,
9
+ /^S[A,B,D,G,K,L,S,T,U][^QSZ]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[^QSZ]$/,
10
+ /^H[^FGHIOUVXYZ][A-Y]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$/,
11
+ /^(1[0-2]|1[4-9]|[2-9][0-9]|10[1,2,3,4,5,7]|11[0,1])-\d{2}-DC$/,
12
+ /^(1[0-2]|1[4-9]|[2-9][0-9]|10[1,2,3,4,5,7]|11[0,1])-\d{2}-CC$/,
13
+ /^(1[0-2]|1[4-9]|[2-9][0-9]|10[1,2,3,4,5,7]|11[0,1])-\d{2}-UN$/,
14
+ /^Z[A-D,L,U,Z]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$/,
15
+ /^E[D,J]([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])$/,
16
+ /^LIMO([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])[W,B]$/
17
+ ]
18
+
19
+ def initialize
20
+ @country = "MY"
21
+ end
22
+
23
+ def is_valid?(registration_number)
24
+ return false unless super
25
+ PATTERNS.any? { |pattern| @license =~ pattern }
26
+ end
27
+
28
+ end
29
+ end
@@ -1,20 +1,22 @@
1
1
  module NumberPlateValidator
2
- class SGValidator
2
+ class SGValidator < CountryValidatorBase
3
3
  CHECK_SUM = { 0 => "A", 1 => "Z", 2 => "Y", 3 => "X", 4 => "U", 5 => "T", 6 => "S", 7 => "R", 8 => "P", 9 => "M", 10 => "L", 11 => "K", 12 => "J", 13 => "H", 14 => "G", 15 => "E", 16 => "D", 17 => "C", 18 => "B" }
4
4
  MULTIPLIER = [9, 4, 5, 4, 3, 2]
5
5
 
6
- attr_reader :license
6
+ def initialize
7
+ @country = "SG"
8
+ end
7
9
 
8
10
  def is_valid?(registration_number)
9
- @license = registration_number.gsub(/\s/, "").upcase if registration_number.present?
10
- return false if license.blank? || license.length > 8 || license.length < 4
11
- test = /^[A-Z]{1,3}[1-9]{1}\d{0,3}[A-Z]$/ =~ license
11
+ return false unless super
12
+ return false if @license.length > 8 || @license.length < 4
13
+ test = /^[A-Z]{1,3}[1-9]{1}\d{0,3}[A-Z]$/ =~ @license
12
14
  return false unless test == 0
13
- letters = license[/[A-Z]+/]
14
- numerals = license[/\d+/]
15
+ letters = @license[/[A-Z]+/]
16
+ numerals = @license[/\d+/]
15
17
 
16
18
  checksum = get_checksum(letters, numerals)
17
- checksum == license[-1]
19
+ checksum == @license[-1]
18
20
  end
19
21
 
20
22
  def get_checksum(letters, numerals)
@@ -34,8 +36,7 @@ module NumberPlateValidator
34
36
  numbers = [1] + numbers
35
37
  end
36
38
 
37
- numerals = "000" + numerals
38
- numerals = numerals[-4,4]
39
+ numerals = ("000" + numerals)[-4,4]
39
40
  numbers + numerals.split(//).map(&:to_i)
40
41
  end
41
42
 
@@ -1,12 +1,10 @@
1
1
  module NumberPlateValidator
2
2
  class Validator
3
3
 
4
- attr_reader :region, :validator
4
+ attr_reader :validator
5
5
 
6
- def initialize(region)
7
- @region = region
8
- validator_class = eval "NumberPlateValidator::#{region}Validator"
9
- @validator = validator_class.new
6
+ def initialize(validator)
7
+ @validator = validator
10
8
  end
11
9
 
12
10
  def is_valid?(registration_number)
@@ -1,3 +1,3 @@
1
1
  module NumberPlateValidator
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["zorro.ej@gmail.com"]
11
11
 
12
12
  spec.summary = %q{This gem validates vehicle number/license plates. }
13
- spec.description = %q{This gem allows you to validate a license plate number from a chosen country/region. It also provides a validator for ActiveModel. It currently only supports Singapore.}
13
+ spec.description = %q{This gem allows you to validate a license plate number from a chosen country/region. It also provides a validator for ActiveModel and a CLI. It currently only supports Singapore and Malaysia (not exhaustive.}
14
14
  spec.homepage = "https://github.com/ej2015/number_plate_validator.git"
15
15
  spec.license = "MIT"
16
16
 
@@ -20,11 +20,16 @@ Gem::Specification.new do |spec|
20
20
  spec.bindir = "exe"
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
+
24
+ spec.required_ruby_version = '>=2.3.0'
23
25
 
24
- spec.add_dependency "activesupport", "~>4.2"
25
- spec.add_dependency "activemodel", "~>4.2"
26
+ spec.add_dependency "activesupport", ">=4.2"
27
+ spec.add_dependency "activemodel", ">=4.2"
28
+ spec.add_dependency "thor"
26
29
  spec.add_development_dependency "bundler", "~> 1.13"
27
30
  spec.add_development_dependency "rake", "~> 10.0"
28
31
  spec.add_development_dependency "rspec", "~> 3.7"
29
32
  spec.add_development_dependency "pry", "~>0.11"
33
+ spec.add_development_dependency "cucumber", '~>3.1.0'
34
+ spec.add_development_dependency "aruba", '~>0.14.0'
30
35
  end
metadata CHANGED
@@ -1,43 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: number_plate_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edgar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-16 00:00:00.000000000 Z
11
+ date: 2018-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '4.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '4.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,9 +108,37 @@ dependencies:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0.11'
111
+ - !ruby/object:Gem::Dependency
112
+ name: cucumber
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 3.1.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 3.1.0
125
+ - !ruby/object:Gem::Dependency
126
+ name: aruba
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 0.14.0
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 0.14.0
97
139
  description: This gem allows you to validate a license plate number from a chosen
98
- country/region. It also provides a validator for ActiveModel. It currently only
99
- supports Singapore.
140
+ country/region. It also provides a validator for ActiveModel and a CLI. It currently
141
+ only supports Singapore and Malaysia (not exhaustive.
100
142
  email:
101
143
  - zorro.ej@gmail.com
102
144
  executables: []
@@ -111,9 +153,13 @@ files:
111
153
  - README.md
112
154
  - Rakefile
113
155
  - bin/console
156
+ - bin/number_plate
114
157
  - bin/setup
115
158
  - lib/number_plate_validator.rb
116
159
  - lib/number_plate_validator/active_model.rb
160
+ - lib/number_plate_validator/cli.rb
161
+ - lib/number_plate_validator/country_validator_base.rb
162
+ - lib/number_plate_validator/my_validator.rb
117
163
  - lib/number_plate_validator/sg_validator.rb
118
164
  - lib/number_plate_validator/validator.rb
119
165
  - lib/number_plate_validator/version.rb
@@ -130,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
176
  requirements:
131
177
  - - ">="
132
178
  - !ruby/object:Gem::Version
133
- version: '0'
179
+ version: 2.3.0
134
180
  required_rubygems_version: !ruby/object:Gem::Requirement
135
181
  requirements:
136
182
  - - ">="