tw_ubn 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5d25165a4b4dd2fb26703aecfce05d6bd659f12ab1047c0c49779932fa3b4ab1
4
+ data.tar.gz: 25e41bcf66447518a3898e8e6846b7e8c3e0573eaa5aa9fbf97d4c33307917c8
5
+ SHA512:
6
+ metadata.gz: a10c097ab4338ff17b0dad2ac495b356d3297d0482f50b6f5acbc2c3655333c66994d1dfb60dea1e79f59e812e05ba4dce2ab7c1e69eac067aaf4b4b1517b7be
7
+ data.tar.gz: 0c26ebd34db46e72ae99ddc3b8789a9eb3072ec1b18a8adc68fd3a29e61cca93084f580b0f58fa2dfadf09346a8914582505fe8f5e56a18fa72d065875f970ae
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ SuggestExtensions: false
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in tw_ubn.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Guanting Chen
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,70 @@
1
+ # Taiwan UBN Validator (2021)
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/tw_ubn.svg)](https://badge.fury.io/rb/tw_ubn)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/2e20efaaac6115c6df87/maintainability)](https://codeclimate.com/github/guanting112/tw_ubn/maintainability)
5
+
6
+ ![example workflow](https://github.com/guanting112/tw_ubn/actions/workflows/main.yml/badge.svg)
7
+
8
+ 此為針對 台灣營利事業統一編號 (UBN) 所開發的驗證套件。
9
+
10
+ 您可以使用它進行驗證,確認使用者提供的公司統一編號是否有效。
11
+
12
+ * 本套件適用於電商平台發票資料、企業相關表單欄位 輔助驗證。
13
+ * 可減少因消費者統編填錯,與客服溝通/重開的來往成本。
14
+ * 已經過大量現實資料驗證,另有搭配 150 萬筆商工行政資料確定校對方式正確。
15
+ * 與 財政部財資中心 2021年6月公告的新驗證邏輯同步 ( 因應 2024 年公司統一編號用盡之問題 )
16
+
17
+ 驗證之規則與細節參考,皆來自台灣財政部財政資訊中心之公告 「 [營利事業統一編號檢查碼邏輯修正說明][fia_rule] 」
18
+
19
+ ## Installation / 安裝方式
20
+
21
+ via Rubygems
22
+
23
+ ```shell
24
+ gem install tw_ubn
25
+ ```
26
+
27
+ In your Gemfile:
28
+
29
+ ```ruby
30
+ gem 'tw_ubn'
31
+ ```
32
+
33
+ ## Usage / 如何使用
34
+
35
+ 使用很簡單,僅需要呼叫 TwUbn::Validator.call 即可確認是否正確
36
+
37
+ ```ruby
38
+ TwUbn::Validator.call("8碼公司統編")
39
+ ```
40
+
41
+ ```ruby
42
+ # 有效的公司統一編號
43
+ TwUbn::Validator.call("22099131") # true
44
+ TwUbn::Validator.call("47217677") # true
45
+ TwUbn::Validator.call("22822281") # true
46
+
47
+ # 錯誤的公司統編
48
+ TwUbn::Validator.call("22822280") # false
49
+ TwUbn::Validator.call("47217977") # false
50
+ TwUbn::Validator.call("88123") # false
51
+ TwUbn::Validator.call("11099131") # false
52
+ ```
53
+
54
+ 在 Ruby on Rails 內,您也可以將 TwUbn::Validator 導入到自建的 validator 內使用。
55
+
56
+ https://api.rubyonrails.org/classes/ActiveModel/Validator.html
57
+
58
+ ```ruby
59
+ class TaxDataValidator < ActiveModel::Validator
60
+ def validate(record)
61
+ if !TwUbn::Validator.call(record.tax_number)
62
+ record.errors.add(:tax_number, '統一編號格式不正確。')
63
+ end
64
+ end
65
+ end
66
+ ```
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "tw_ubn"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
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,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TwUbn
4
+ VERIFY_UBN_KEY = 5
5
+
6
+ UBN_CALC_WEIGHT = [1, 2, 1, 2, 1, 2, 4, 1].freeze
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TwUbn
4
+ VERSION = "1.0.0"
5
+ end
data/lib/tw_ubn.rb ADDED
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tw_ubn/rules"
4
+ require_relative "tw_ubn/version"
5
+
6
+ module TwUbn
7
+ # taiwan unified business number validator
8
+ class Validator
9
+ def initialize(ubn)
10
+ @ubn = ubn.to_s
11
+ end
12
+
13
+ def self.call(*args)
14
+ new(*args).verify
15
+ end
16
+
17
+ def verify
18
+ return false if (@ubn =~ /^\d{8}$/).nil?
19
+
20
+ ubn_array = @ubn.chars.map(&:to_i)
21
+ checksum = calculate_checksum(ubn_array)
22
+
23
+ verify_checksum(checksum)
24
+ end
25
+
26
+ def calculate_checksum(ubn_array)
27
+ first_result =
28
+ ubn_array.map.with_index do |v, i|
29
+ v * TwUbn::UBN_CALC_WEIGHT[i]
30
+ end
31
+
32
+ second_result =
33
+ first_result.map { |v| (v / 10).floor + v % 10 }
34
+
35
+ second_result.sum
36
+ end
37
+
38
+ def verify_checksum(checksum)
39
+ key = TwUbn::VERIFY_UBN_KEY
40
+ use_special_verify = @ubn[6] == "7"
41
+
42
+ if use_special_verify
43
+ ((checksum - 10) % key).zero? ||
44
+ ((checksum - 9) % key).zero?
45
+ else
46
+ (checksum % key).zero?
47
+ end
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tw_ubn
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Guanting Chen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 方便您在 ruby 校驗,台灣公司統一編號資料。(支援財政部2021版6月規則更新)
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - ".rubocop.yml"
20
+ - Gemfile
21
+ - LICENSE.txt
22
+ - README.md
23
+ - Rakefile
24
+ - bin/console
25
+ - bin/setup
26
+ - lib/tw_ubn.rb
27
+ - lib/tw_ubn/rules.rb
28
+ - lib/tw_ubn/version.rb
29
+ homepage: https://github.com/guanting112/tw_ubn
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/guanting112/tw_ubn
34
+ source_code_uri: https://github.com/guanting112/tw_ubn
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.5.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.0.3.1
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: TwUbn 台灣公司統一編號驗證器 (Taiwan unified business number validator)
54
+ test_files: []