usi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ cache:
5
+ - bundler
6
+ - apt
7
+ bundler_args: -j4 --quiet
8
+ script:
9
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in usi.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 John D'Agostino
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # USI
2
+
3
+ [![Code Climate](https://codeclimate.com/github/jobready/usi.png)](https://codeclimate.com/github/jobready/usi)
4
+ [![Build Status](https://travis-ci.org/jobready/usi.svg)](https://travis-ci.org/jobready/usi)
5
+
6
+ A gem for validating USI (Unique Student Identifiers) for VET Students in Australia
7
+
8
+ From the Department of Industry's website -
9
+
10
+ USI is effectively an account or reference number made up of numbers and letters. The USI will allow all of an individual’s training records, entered in the national vocational education and training (VET) data collection, to be linked.
11
+ The USI will be available online and at no cost to the student. This USI will stay with the student for life and be recorded with any nationally recognised VET course that is undertaken from when the USI comes into effect.
12
+
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'usi'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install usi
27
+
28
+ ## Usage
29
+
30
+ ```ruby
31
+ require 'usi'
32
+
33
+ Usi::Validator.new('223456789N').valid?
34
+
35
+ ```
36
+
37
+ in Rails 3+
38
+
39
+ ```ruby
40
+ class Student < ActiveRecord::Base
41
+ validates :identifier, usi: true
42
+ end
43
+ ```
44
+
45
+ in Rails 2.3
46
+
47
+ ```ruby
48
+ class Student < ActiveRecord::Base
49
+ validate :must_be_valid_format_usi
50
+
51
+ def must_be_valid_format_usi
52
+ errors.add_to_base('USI format invalid') unless Usi::Validator.new(usi).valid?
53
+ end
54
+ end
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( http://github.com/jobready/usi/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
64
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,7 @@
1
+ module Usi
2
+ end
3
+
4
+ require 'usi/version'
5
+ require 'usi/luhn_check'
6
+ require 'usi/validator'
7
+ require 'usi_validator' if defined?(Rails) && defined?(ActiveModel)
@@ -0,0 +1,37 @@
1
+ class Usi::LuhnCheck
2
+ attr_reader :base_n, :valid_characters
3
+
4
+ def initialize(valid_characters)
5
+ @valid_characters = valid_characters
6
+ @base_n = valid_characters.length
7
+ end
8
+
9
+ def generate(string)
10
+ current_factor = 2
11
+ results = string.reverse.each_char.map do |character|
12
+ code_point = valid_characters.index(character)
13
+ addend = calculate_addend(code_point, current_factor)
14
+ current_factor = alternate_factor(current_factor)
15
+ express_addend_in_base_n(addend)
16
+ end
17
+
18
+ sum = results.inject(0, &:+)
19
+ valid_characters[find_check_character_code_point(sum)]
20
+ end
21
+
22
+ def alternate_factor(factor)
23
+ factor == 2 ? 1 : 2
24
+ end
25
+
26
+ def calculate_addend(code_point, factor)
27
+ code_point * factor
28
+ end
29
+
30
+ def express_addend_in_base_n(addend)
31
+ (addend / base_n) + (addend % base_n)
32
+ end
33
+
34
+ def find_check_character_code_point(sum)
35
+ (base_n - (sum % base_n)) % base_n
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ class Usi::Validator
2
+ attr_accessor :identifier
3
+
4
+ VALID_CHARACTERS = ['2', '3', '4', '5', '6', '7', '8', '9',
5
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
6
+ 'J', 'K','L', 'M', 'N', 'P', 'Q', 'R',
7
+ 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'].freeze
8
+
9
+ def initialize(identifier)
10
+ @identifier = identifier
11
+ end
12
+
13
+ def valid?
14
+ if ten_valid_characters?
15
+ identifier.split('').last == checker.generate(identifier[0..8])
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ def checker
22
+ @checker ||= Usi::LuhnCheck.new(VALID_CHARACTERS)
23
+ end
24
+
25
+ private
26
+
27
+ def ten_valid_characters?
28
+ identifier =~ /\A[#{VALID_CHARACTERS.join}]{10}\z/
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Usi
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,6 @@
1
+ class UsiValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ result = Usi::Validator.new(value).valid?
4
+ record.errors[attribute] << 'is not a valid USI (Unique Student Identifier)' unless result
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'coveralls'
5
+ Coveralls.wear!
6
+
7
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
8
+ SimpleCov::Formatter::HTMLFormatter,
9
+ Coveralls::SimpleCov::Formatter,
10
+ ]
11
+
12
+ SimpleCov.configure do
13
+ add_filter '/spec/'
14
+ end
15
+
16
+ require 'bundler/setup'
17
+ Bundler.require(:default, :development)
18
+
19
+ RSpec.configure do |config|
20
+ config.treat_symbols_as_metadata_keys_with_true_values = true
21
+ config.run_all_when_everything_filtered = true
22
+ config.filter_run :focus
23
+ config.filter_run_excluding perf: true
24
+ config.order = 'random'
25
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Usi::Validator do
4
+ let(:result) { Usi::Validator.new(usi) }
5
+
6
+ context 'when USI is not a valid Luhn mod N string' do
7
+ let(:usi) { '223456789X' }
8
+ specify { expect(result).to_not be_valid }
9
+ end
10
+
11
+ context 'given the valid 10 digit Luhn mod N string' do
12
+ ['223456789N',
13
+ 'H2289JJ33U'].each do |x|
14
+ let(:usi) { x }
15
+
16
+ it "#{x.inspect} should be valid" do
17
+ expect(result).to be_valid
18
+ end
19
+ end
20
+ end
21
+
22
+ context 'when USI is a valid Luhn mod N string but not length 10' do
23
+ ['3495289R',
24
+ '34952895876V'].each do |x|
25
+ let(:usi) { x }
26
+
27
+ it "#{x.inspect} should not be valid" do
28
+ expect(result).to_not be_valid
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'usi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'usi'
8
+ spec.version = Usi::VERSION
9
+ spec.authors = ["John D'Agostino"]
10
+ spec.email = ['john.dagostino@gmail.com']
11
+ spec.summary = %q{Validate Australian Government USI}
12
+ spec.description = %q{A gem for validating Australian Government Unique Student Identifiers (USI)}
13
+ spec.homepage = 'https://github.com/jobready/usi'
14
+ spec.license = 'MIT'
15
+
16
+ spec.required_rubygems_version = '>= 1.3.6'
17
+
18
+ spec.add_development_dependency 'bundler', '~> 1.0'
19
+ spec.add_development_dependency 'rspec', '~> 2.14'
20
+ spec.add_development_dependency 'cane', '~> 2.6'
21
+ spec.add_development_dependency 'byebug', '~> 2.7'
22
+ spec.add_development_dependency 'rake', '~> 10.1'
23
+ spec.add_development_dependency 'coveralls', '~> 0'
24
+
25
+ spec.files = `git ls-files -z`.split("\x0")
26
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
28
+ spec.require_paths = ['lib']
29
+ end
30
+
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: usi
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - John D'Agostino
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2016-07-05 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 15
29
+ segments:
30
+ - 1
31
+ - 0
32
+ version: "1.0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 31
44
+ segments:
45
+ - 2
46
+ - 14
47
+ version: "2.14"
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: cane
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 15
59
+ segments:
60
+ - 2
61
+ - 6
62
+ version: "2.6"
63
+ type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: byebug
67
+ prerelease: false
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ~>
72
+ - !ruby/object:Gem::Version
73
+ hash: 13
74
+ segments:
75
+ - 2
76
+ - 7
77
+ version: "2.7"
78
+ type: :development
79
+ version_requirements: *id004
80
+ - !ruby/object:Gem::Dependency
81
+ name: rake
82
+ prerelease: false
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ~>
87
+ - !ruby/object:Gem::Version
88
+ hash: 33
89
+ segments:
90
+ - 10
91
+ - 1
92
+ version: "10.1"
93
+ type: :development
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: coveralls
97
+ prerelease: false
98
+ requirement: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ type: :development
108
+ version_requirements: *id006
109
+ description: A gem for validating Australian Government Unique Student Identifiers (USI)
110
+ email:
111
+ - john.dagostino@gmail.com
112
+ executables: []
113
+
114
+ extensions: []
115
+
116
+ extra_rdoc_files: []
117
+
118
+ files:
119
+ - .gitignore
120
+ - .travis.yml
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/usi.rb
126
+ - lib/usi/luhn_check.rb
127
+ - lib/usi/validator.rb
128
+ - lib/usi/version.rb
129
+ - lib/usi_validator.rb
130
+ - spec/spec_helper.rb
131
+ - spec/validator_spec.rb
132
+ - usi.gemspec
133
+ homepage: https://github.com/jobready/usi
134
+ licenses:
135
+ - MIT
136
+ post_install_message:
137
+ rdoc_options: []
138
+
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ hash: 3
147
+ segments:
148
+ - 0
149
+ version: "0"
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ hash: 23
156
+ segments:
157
+ - 1
158
+ - 3
159
+ - 6
160
+ version: 1.3.6
161
+ requirements: []
162
+
163
+ rubyforge_project:
164
+ rubygems_version: 1.8.15
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: Validate Australian Government USI
168
+ test_files:
169
+ - spec/spec_helper.rb
170
+ - spec/validator_spec.rb