number_plate_sg 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b740675607e5e7713a8d8064195bd7baa3922ce7
4
+ data.tar.gz: 4a987fe8f19cd107b8f012ace6ad8abde3b47db2
5
+ SHA512:
6
+ metadata.gz: 7c802bd9efa81260b73ab1495865241c39feb9b206b008075be1c5c7f2c6a799d257ebc4b9e7eecee41f6b9533a5deb574caa163497645b3d45a3d4e7acbb1d6
7
+ data.tar.gz: 14930b996ae3434bf0f957efe2f4a0ee586f5461e9a8a968cff9e723b430deab6b9b66438896bb0b480a7e7541c1ae2c58cf7cd772d9e2a3895f7a7aae6a7fe1
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ - 2.4.0
6
+ - 2.3.0
7
+ - 2.2.0
8
+ - 2.1.0
9
+ - 2.0.0
10
+ before_install: gem install bundler -v 1.13.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in number_plate_sg.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Edgar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # NumberPlateSg
2
+ [![CodeFactor](https://www.codefactor.io/repository/github/ej2015/number_plate_sg/badge)](https://www.codefactor.io/repository/github/ej2015/number_plate_sg)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/eba3926f39c9682e47ed/maintainability)](https://codeclimate.com/github/ej2015/my_age/maintainability)
4
+ [![Build Status](https://travis-ci.org/ej2015/number_plate_sg.svg?branch=master)](https://travis-ci.org/ej2015/number_plate_sg)
5
+
6
+ A little utility to validate a Singpaore number plate or to generate a random number
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'number_plate_sg'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install number_plate_sg
23
+
24
+ ## Usage
25
+ Use the validator
26
+ ```
27
+ ##Initialize validator:
28
+ validator = NumberPlate.validator("SG")
29
+
30
+ ##Check if it is valid:
31
+ validator.is_valid?("EJ81D")
32
+ #=> true
33
+
34
+ validator.is_valid?("EJ81E")
35
+ #=> false
36
+
37
+ ##get the current country code
38
+ validator.country
39
+ #=> "SG"
40
+ ```
41
+ Use the generator
42
+ ```
43
+ ##Initialize generator
44
+ generator = NumberPlate.generator('SG')
45
+
46
+ generator.generate
47
+ #=> SFZ999K
48
+ ```
49
+ ## ActiveModel
50
+
51
+ A `NumberPlateSgValidator` is provided with this gem to be used with ActiveModel.
52
+
53
+ ```
54
+ class Vehicle < ActiveRecord::Base
55
+ validates :plate_number, number_plate_sg: true
56
+ ...
57
+ end
58
+
59
+ ```
60
+
61
+ ## Command Line Interface
62
+ A CLI is provided to check the plate validity
63
+
64
+ ```
65
+ $number_plate_sg validate EJ81D
66
+ $true
67
+
68
+ ```
69
+ A CLI is also provided to generate a random number
70
+
71
+ ```
72
+ $number_plate_sg generate
73
+ $SFZ999K
74
+ ```
75
+
76
+ ## Development
77
+
78
+ 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.
79
+
80
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ej2015/number_plate_sg.
85
+
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
90
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "number_plate_sg"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'number_plate/sg/cli'
3
+ NumberPlate::SG::CLI.start
@@ -0,0 +1,12 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class NumberPlateSgValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+
6
+ val = ::NumberPlate.validator('SG')
7
+
8
+ record.errors.add attribute, (options[:message] || "is not a valid number plate") unless val.is_valid?(value)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module NumberPlate
2
+ module SG
3
+ class BaseGenerator
4
+
5
+ attr_reader :country
6
+
7
+ def generate
8
+ raise 'this method is not defined yet'
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module NumberPlate
2
+ module SG
3
+ class BaseValidator
4
+
5
+ attr_reader :country
6
+
7
+ def is_valid?(registration_number)
8
+ setup(registration_number) if registration_number.present?
9
+ @license.present?
10
+ end
11
+
12
+ private
13
+ def setup(registration_number)
14
+ @license = registration_number.gsub(/\s/, "").upcase
15
+ @letters = @license[/[A-Z]+/]
16
+ @numerals = @license[/\d+/]
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ module NumberPlate
2
+ module SG
3
+ module Checksum
4
+ 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" }
5
+ MULTIPLIER = [9, 4, 5, 4, 3, 2]
6
+
7
+ def checksum
8
+ get_checksum_from_numbers(numbers)
9
+ end
10
+
11
+ def numbers
12
+ numbers = []
13
+ @letters.split(//).each do |alphebat|
14
+ numbers << (alphebat.ord - 64)
15
+ end
16
+
17
+ if numbers.length == 3
18
+ numbers = numbers[1,2]
19
+ elsif numbers.length == 1
20
+ numbers = [1] + numbers
21
+ end
22
+
23
+ numerals = ("000" + @numerals)[-4,4]
24
+ numbers + numerals.split(//).map(&:to_i)
25
+ end
26
+
27
+ def get_checksum_from_numbers(numbers)
28
+ checksum_array = []
29
+ numbers.zip(MULTIPLIER) { |x, y| checksum_array << (x * y) }
30
+ checksum_key = checksum_array.reduce(:+) % 19
31
+ CHECK_SUM[checksum_key]
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ require 'thor'
2
+ require 'number_plate_sg'
3
+
4
+ module NumberPlate
5
+ module SG
6
+ class CLI < Thor
7
+ desc "validate", "validate if a PLATE is valid"
8
+ def validate(plate)
9
+ validator = NumberPlate.validator('SG')
10
+ puts(validator.is_valid? plate)
11
+ end
12
+
13
+ desc "generate", "generate a random plate"
14
+ def generate
15
+ puts NumberPlate.generator('SG').generate
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "base_generator"
2
+ require_relative "checksum"
3
+
4
+ module NumberPlate
5
+ module SG
6
+ class Generator < NumberPlate::SG::BaseGenerator
7
+ include NumberPlate::SG::Checksum
8
+
9
+ def initialize
10
+ @country = "SG"
11
+ end
12
+
13
+ def generate
14
+ letters + numerals + checksum
15
+ end
16
+
17
+ def prefix
18
+ 'S'
19
+ end
20
+
21
+ def letters
22
+ @letters = prefix + random_letters
23
+ end
24
+
25
+ def numerals
26
+ @numerals = rand(1..9999).to_s
27
+ end
28
+
29
+ private
30
+ def random_letters
31
+ (0..rand(2)).map do
32
+ rand(65..90).chr
33
+ end.join
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ require_relative "base_validator"
2
+ require_relative "checksum"
3
+
4
+ module NumberPlate
5
+ module SG
6
+ class Validator < NumberPlate::SG::BaseValidator
7
+ include NumberPlate::SG::Checksum
8
+
9
+ def initialize
10
+ @country = "SG"
11
+ end
12
+
13
+ def is_valid?(registration_number)
14
+ super &&
15
+ valid_length? &&
16
+ valid_pattern? &&
17
+ valid_checksum?
18
+ end
19
+
20
+ def valid_length?
21
+ @license.length <= 8 && @license.length >= 4
22
+ end
23
+
24
+ def valid_pattern?
25
+ 0 == (/^[A-Z]{1,3}[1-9]{1}\d{0,3}[A-Z]$/ =~ @license)
26
+ end
27
+
28
+ def valid_checksum?
29
+ checksum == @license[-1]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module NumberPlateSg
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ require "number_plate/version"
2
+ require "number_plate/sg/validator"
3
+ require "number_plate/sg/generator"
4
+ require "active_support/core_ext/string"
5
+ require "active_model"
6
+ require "number_plate/sg/active_model"
7
+
8
+ module NumberPlate
9
+
10
+ class << self
11
+ def validator(country)
12
+ Object.const_get("NumberPlate::#{country}::Validator").new
13
+ end
14
+
15
+ def generator(country)
16
+ Object.const_get("NumberPlate::#{country}::Generator").new
17
+ end
18
+ end
19
+
20
+
21
+
22
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'number_plate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "number_plate_sg"
8
+ spec.version = NumberPlateSg::VERSION
9
+ spec.authors = ["Edgar"]
10
+ spec.email = ["zorro.ej@gmail.com"]
11
+
12
+ spec.summary = %q{A little utility for Singpaore number plate.}
13
+ spec.description = %q{It can validate a number or generate a random number.}
14
+ spec.homepage = "https://github.com/ej2015/number_plate_sg.git"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "activesupport", ">=4.2"
25
+ spec.add_dependency "activemodel", ">=4.2"
26
+ spec.add_dependency "thor"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.13"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "pry-byebug", "~>3.6.0"
32
+ spec.add_development_dependency "cucumber", "~>3.1.0"
33
+ spec.add_development_dependency "aruba", "~>0.14.0"
34
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: number_plate_sg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Edgar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.13'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.13'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.6.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.6.0
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
139
+ description: It can validate a number or generate a random number.
140
+ email:
141
+ - zorro.ej@gmail.com
142
+ executables:
143
+ - number_plate_sg
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - ".rspec"
149
+ - ".travis.yml"
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - exe/number_plate_sg
157
+ - lib/number_plate/sg/active_model.rb
158
+ - lib/number_plate/sg/base_generator.rb
159
+ - lib/number_plate/sg/base_validator.rb
160
+ - lib/number_plate/sg/checksum.rb
161
+ - lib/number_plate/sg/cli.rb
162
+ - lib/number_plate/sg/generator.rb
163
+ - lib/number_plate/sg/validator.rb
164
+ - lib/number_plate/version.rb
165
+ - lib/number_plate_sg.rb
166
+ - number_plate_sg.gemspec
167
+ homepage: https://github.com/ej2015/number_plate_sg.git
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.6.14
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: A little utility for Singpaore number plate.
191
+ test_files: []