taiwanese_ubn_validator 0.0.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 +7 -0
- data/.gitignore +21 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +20 -0
- data/README.md +37 -0
- data/Rakefile +32 -0
- data/lib/taiwanese_ubn_validator.rb +41 -0
- data/lib/taiwanese_ubn_validator/rails_validator.rb +11 -0
- data/lib/taiwanese_ubn_validator/version.rb +3 -0
- data/spec/helper.rb +21 -0
- data/spec/validate_taiwanese_ubn_spec.rb +57 -0
- data/taiwanese_ubn_validator.gemspec +23 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8ae26e0c6717abf0a792eb847b79bd8a958337e5
|
4
|
+
data.tar.gz: 40cf2bcce51802d81f55c29791df2ecd58e97be0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5e3e37bc411973ac69399de0a59949e511879d6d30c989cfd40f3b1eeade90e7f52fa8ba461cf42b54be2c210060516aeb13971894e9857b8555457b1afdbe33
|
7
|
+
data.tar.gz: 3d61dc9871de32fa873533755bcaae47688cfcfd93f659b195eb57e9280e78ef61a771907f2e23bde687d4ab8de89cd74705a61d5a679e512caecc36cee8743a
|
data/.gitignore
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Gemfile.lock
|
2
|
+
html/
|
3
|
+
pkg/
|
4
|
+
vendor/cache/*.gem
|
5
|
+
*.gem
|
6
|
+
*.rbc
|
7
|
+
.bundle
|
8
|
+
.config
|
9
|
+
.yardoc
|
10
|
+
Gemfile.lock
|
11
|
+
InstalledFiles
|
12
|
+
_yardoc
|
13
|
+
coverage
|
14
|
+
doc/
|
15
|
+
lib/bundler/man
|
16
|
+
rdoc
|
17
|
+
spec/reports
|
18
|
+
test/tmp
|
19
|
+
test/version_tmp
|
20
|
+
tmp
|
21
|
+
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 GoodLife
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Taiwanese UBN Validator
|
2
|
+
|
3
|
+
驗證台灣/中華民國的統一編號
|
4
|
+
|
5
|
+
### 安裝
|
6
|
+
|
7
|
+
Gemfile 裡面寫入:
|
8
|
+
|
9
|
+
gem 'taiwanese_ubn_validator'
|
10
|
+
|
11
|
+
然後在命令列執行
|
12
|
+
|
13
|
+
bundle install
|
14
|
+
|
15
|
+
### 使用方法
|
16
|
+
|
17
|
+
#### 純 Ruby 驗證
|
18
|
+
|
19
|
+
TaiwaneseUbnValidator.valid?('12345678') # 會回傳 false
|
20
|
+
TaiwaneseUbnValidator.valid?('16612907') # 會回傳 true
|
21
|
+
|
22
|
+
#### Rails/ActiveModel Validator
|
23
|
+
|
24
|
+
Rails 專案可用以下方式在 ActiveModel 驗證:
|
25
|
+
|
26
|
+
validates :ubn, 'taiwanese_ubn_validator/rails' => true
|
27
|
+
|
28
|
+
本 gem 只支援 Rails 3.1 以上。
|
29
|
+
|
30
|
+
### 計算法參考
|
31
|
+
|
32
|
+
* http://www.ntbsa.gov.tw/etwmain/download?sid=13a5ed9655d00000c3728d2e3c32f9c1
|
33
|
+
* http://herolin.twbbs.org/entry/is-valid-TW-company-ID/
|
34
|
+
|
35
|
+
### License
|
36
|
+
|
37
|
+
MIT License
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'bundler'
|
7
|
+
rescue LoadError => e
|
8
|
+
warn e.message
|
9
|
+
warn "Run `gem install bundler` to install Bundler."
|
10
|
+
exit -1
|
11
|
+
end
|
12
|
+
|
13
|
+
begin
|
14
|
+
Bundler.setup(:development)
|
15
|
+
rescue Bundler::BundlerError => e
|
16
|
+
warn e.message
|
17
|
+
warn "Run `bundle install` to install missing gems."
|
18
|
+
exit e.status_code
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake'
|
22
|
+
|
23
|
+
require 'rubygems/tasks'
|
24
|
+
Gem::Tasks.new
|
25
|
+
|
26
|
+
require 'rake/testtask'
|
27
|
+
Rake::TestTask.new do |t|
|
28
|
+
t.libs.push "lib"
|
29
|
+
t.libs.push "spec"
|
30
|
+
t.test_files = FileList['spec/**/*_spec.rb']
|
31
|
+
t.verbose = true
|
32
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'taiwanese_ubn_validator/version'
|
2
|
+
|
3
|
+
module TaiwaneseUbnValidator
|
4
|
+
MULTIPLIER = [1,2,1,2,1,2,4,1].freeze
|
5
|
+
|
6
|
+
# @ubn - 8 digits string
|
7
|
+
def self.valid?(ubn)
|
8
|
+
ubn = ubn.to_s
|
9
|
+
|
10
|
+
return false if ubn.length != 8
|
11
|
+
return false if !( ubn =~ /^\d+$/ )
|
12
|
+
|
13
|
+
digits = ubn.chars.map(&:to_i)
|
14
|
+
|
15
|
+
special_sum = calculate_special_sum(digits)
|
16
|
+
|
17
|
+
if special_sum % 10 == 0
|
18
|
+
return true
|
19
|
+
else
|
20
|
+
if digits[6] == 7 && special_sum % 10 == 9
|
21
|
+
return true
|
22
|
+
else
|
23
|
+
return false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# @digits - Array of Integers
|
29
|
+
def self.calculate_special_sum(digits)
|
30
|
+
sum = 0
|
31
|
+
8.times do |i|
|
32
|
+
m = digits[i] * MULTIPLIER[i]
|
33
|
+
sum += m.divmod(10).inject(0, &:+)
|
34
|
+
end
|
35
|
+
sum
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
if defined?(ActiveModel::EachValidator)
|
40
|
+
require 'taiwanese_ubn_validator/rails_validator'
|
41
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TaiwaneseUbnValidator
|
4
|
+
class RailsValidator < ActiveModel::EachValidator
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
if !TaiwaneseUbnValidator.valid?(value)
|
7
|
+
record.errors[attribute] << (options[:message] || "有誤")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
rescue LoadError => e
|
6
|
+
STDERR.puts e.message
|
7
|
+
STDERR.puts "Run `gem install bundler` to install Bundler."
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
Bundler.setup(:default, :development, :test)
|
13
|
+
rescue Bundler::BundlerError => e
|
14
|
+
STDERR.puts e.message
|
15
|
+
STDERR.puts "Run `bundle install` to install missing gems."
|
16
|
+
exit e.status_code
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'minitest/spec'
|
20
|
+
require 'minitest/autorun'
|
21
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'taiwanese_ubn_validator'
|
3
|
+
|
4
|
+
describe TaiwaneseUbnValidator do
|
5
|
+
|
6
|
+
it 'has version' do
|
7
|
+
TaiwaneseUbnValidator.const_get('VERSION').wont_be_empty
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'reject incorrect ubn' do
|
11
|
+
TaiwaneseUbnValidator.valid?('73251200').must_equal false
|
12
|
+
TaiwaneseUbnValidator.valid?('18556773').must_equal false
|
13
|
+
TaiwaneseUbnValidator.valid?('34733521').must_equal false
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'reject incorrect size' do
|
17
|
+
TaiwaneseUbnValidator.valid?('123456789').must_equal false
|
18
|
+
TaiwaneseUbnValidator.valid?('1234567').must_equal false
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'reject input with non-digits' do
|
22
|
+
TaiwaneseUbnValidator.valid?('123AB678').must_equal false
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'validates correct ubn' do
|
26
|
+
TaiwaneseUbnValidator.valid?('98770235').must_equal true
|
27
|
+
|
28
|
+
TaiwaneseUbnValidator.valid?('22102298').must_equal true
|
29
|
+
TaiwaneseUbnValidator.valid?('20927014').must_equal true
|
30
|
+
|
31
|
+
TaiwaneseUbnValidator.valid?('15813981').must_equal true
|
32
|
+
TaiwaneseUbnValidator.valid?('34733524').must_equal true
|
33
|
+
TaiwaneseUbnValidator.valid?('23600358').must_equal true
|
34
|
+
TaiwaneseUbnValidator.valid?('73251209').must_equal true
|
35
|
+
|
36
|
+
TaiwaneseUbnValidator.valid?('03798509').must_equal true
|
37
|
+
TaiwaneseUbnValidator.valid?('97178489').must_equal true
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'validates correct ubn, with the special case that 7th digit is 7' do
|
41
|
+
TaiwaneseUbnValidator.valid?('59066479').must_equal true
|
42
|
+
|
43
|
+
TaiwaneseUbnValidator.valid?('18556774').must_equal true
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'validates numbers that government sector uses' do
|
47
|
+
TaiwaneseUbnValidator.valid?('33506509').must_equal true
|
48
|
+
TaiwaneseUbnValidator.valid?('37302800').must_equal true
|
49
|
+
|
50
|
+
TaiwaneseUbnValidator.valid?('31261465').must_equal true
|
51
|
+
TaiwaneseUbnValidator.valid?('04173812').must_equal true
|
52
|
+
TaiwaneseUbnValidator.valid?('03807654').must_equal true
|
53
|
+
TaiwaneseUbnValidator.valid?('03813207').must_equal true
|
54
|
+
TaiwaneseUbnValidator.valid?('04165708').must_equal true
|
55
|
+
TaiwaneseUbnValidator.valid?('01046996').must_equal true
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.expand_path('../lib/taiwanese_ubn_validator/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'taiwanese_ubn_validator'
|
7
|
+
gem.version = TaiwaneseUbnValidator::VERSION
|
8
|
+
gem.summary = %q{台灣/中華民國之統一編號的檢查驗證}
|
9
|
+
gem.description = gem.summary
|
10
|
+
gem.license = 'MIT'
|
11
|
+
gem.authors = ['GoodLife', 'lulalala']
|
12
|
+
gem.email = 'mark@goodlife.tw'
|
13
|
+
gem.homepage = 'https://github.com/GoodLife/taiwanese_ubn_validator'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(spec)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_development_dependency 'bundler', '~> 1.6'
|
21
|
+
gem.add_development_dependency 'rake', '~> 10.3'
|
22
|
+
gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: taiwanese_ubn_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- GoodLife
|
8
|
+
- lulalala
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-07-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.6'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.6'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.3'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.3'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rubygems-tasks
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0.2'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0.2'
|
56
|
+
description: "台灣/中華民國之統一編號的檢查驗證"
|
57
|
+
email: mark@goodlife.tw
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/taiwanese_ubn_validator.rb
|
68
|
+
- lib/taiwanese_ubn_validator/rails_validator.rb
|
69
|
+
- lib/taiwanese_ubn_validator/version.rb
|
70
|
+
- spec/helper.rb
|
71
|
+
- spec/validate_taiwanese_ubn_spec.rb
|
72
|
+
- taiwanese_ubn_validator.gemspec
|
73
|
+
homepage: https://github.com/GoodLife/taiwanese_ubn_validator
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: "台灣/中華民國之統一編號的檢查驗證"
|
97
|
+
test_files:
|
98
|
+
- spec/helper.rb
|
99
|
+
- spec/validate_taiwanese_ubn_spec.rb
|