idcard_operation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "idcard_operation/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "idcard_operation"
8
+ spec.version = IdcardOperation::VERSION
9
+ spec.authors = ["atpking"]
10
+ spec.email = ["atpking@gmail.com"]
11
+
12
+ spec.summary = %q{Chinese idcard operation}
13
+ spec.description = %q{Chinese idcard operation}
14
+ spec.homepage = "https://github.com/jicheng1014/idcard_info"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
@@ -0,0 +1,15 @@
1
+ require_relative "./idcard_operation/version"
2
+ require_relative './idcard_operation/idcard_info'
3
+ require_relative './idcard_operation/roc_idcard_info'
4
+
5
+ module IdcardOperation
6
+ class << self
7
+ def analysis(idcard)
8
+ if idcard.length == 18
9
+ IdcardInfo.new(idcard)
10
+ else
11
+ RocIdcardInfo.new(idcard)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ c
2
+ continue
3
+ @@dict["geoinfo"][i
4
+ @@dict["geoinfo"]
5
+ s
6
+ self.gender
7
+ birthday
8
+ n
9
+ continue
10
+ sum.divmod(11)
11
+ sum.divmod(3)
12
+ sum.divmod(2)
13
+ sum.divmod(1)
14
+ sum.divmod
15
+ sum
16
+ sum.divmod(11)
17
+ office_bit
18
+ office_bit.at(sum.divmod(11).last)
19
+ sum
20
+ n
@@ -0,0 +1,70 @@
1
+ require 'yaml'
2
+ class IdcardInfo
3
+ attr_accessor :idcard, :valid, :gender, :birthday, :main_location, :location_raw, :name, :name_valid
4
+
5
+ def initialize(idcard)
6
+ @@dict ||= YAML.safe_load(File.read(File.expand_path('../../../geo_info.yml', __FILE__)))
7
+ @@provinces_standard ||= %w[北京市 天津市 河北省 山西省 内蒙古自治区 辽宁省 吉林省 黑龙江省 上海市 江苏省 浙江省 安徽省 福建省 江西省 山东省 河南省 湖北省 湖南省 广东省 广西壮族自治区 海南省 重庆市 四川省 贵州省 云南省 西藏自治区 陕西省 甘肃省 青海省 宁夏回族自治区 新疆维吾尔自治区 台湾省 香港特别行政区 澳门特别行政区].freeze
8
+ self.idcard = idcard
9
+ self.valid = valid_idcard?
10
+ return unless valid
11
+
12
+ self.birthday = calc_birthday
13
+ self.gender = calc_gender
14
+ self.location_raw = @@dict["geoinfo"][idcard[0..5].to_i]
15
+ self.main_location = format_province(location_raw)
16
+ end
17
+
18
+ def format_province(province)
19
+ answer = @@provinces_standard.find do |goal|
20
+ /^#{goal}/.match? province
21
+ end
22
+ return answer unless answer.nil?
23
+
24
+ answer || province
25
+ end
26
+
27
+ def calc_gender
28
+ idcard[-2].to_i % 2 == 1 ? "男" : "女"
29
+ end
30
+
31
+ def calc_birthday
32
+ Date.new(idcard[6..9].to_i, idcard[10..11].to_i, idcard[12..13].to_i)
33
+ end
34
+
35
+ def valid_idcard?
36
+ self.class.valid_idcard?(idcard)
37
+ end
38
+
39
+ def to_hash
40
+ instance_variables.map do |var|
41
+ [var[1..-1].to_sym, instance_variable_get(var)]
42
+ end.to_h
43
+ end
44
+ alias to_h to_hash
45
+
46
+ class << self
47
+ def valid_idcard?(idcard_no)
48
+ return false unless /\d{17}([0-9]|x|X)/.match?(idcard_no)
49
+ sysbit(idcard_no).downcase == idcard_no[-1]&.downcase
50
+ end
51
+
52
+ private
53
+
54
+ def sysbit(idcard_no)
55
+ sum = 0
56
+ idcard_no[0..-2].split('').each_with_index do |bit, i|
57
+ sum += bit.to_i * weight(18 - i)
58
+ end
59
+ office_bit.at(sum.divmod(11).last)
60
+ end
61
+
62
+ def weight(i)
63
+ (2**(i - 1)).divmod(11).last
64
+ end
65
+
66
+ def office_bit
67
+ %w[1 0 x 9 8 7 6 5 4 3 2]
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+ # 台湾国民身份证校验
3
+
4
+ class RocIdcardInfo
5
+ attr_accessor :idcard, :gender, :location, :valid
6
+ def initialize(idcard)
7
+ self.idcard = idcard.upcase
8
+ return unless valid?
9
+
10
+ self.valid = valid?
11
+
12
+ self.gender = idcard[1] == "1" ? "男" : "女"
13
+ self.location = location_dict[idcard[0]].first
14
+ end
15
+
16
+ def valid?
17
+ return false unless /^[A-Z]\d{9}$/.match?(idcard)
18
+ check
19
+ end
20
+
21
+ def to_hash
22
+ instance_variables.map do |var|
23
+ [var[1..-1].to_sym, instance_variable_get(var)]
24
+ end.to_h
25
+ end
26
+ alias to_h to_hash
27
+
28
+ private
29
+
30
+ def check
31
+ tmp = [1,9,8,7,6,5,4,3,2,1,1]
32
+ sum = 0
33
+ "#{location_dict[idcard[0]].last}#{idcard[1..-1]}".split('').each_with_index do |item, index|
34
+ sum += item.to_i * tmp[index]
35
+ end
36
+ sum % 10 == 0
37
+ end
38
+
39
+ def char_to_calc_int
40
+ # ascII 码 - 55
41
+ idcard[0].upcase.ord - 55
42
+ end
43
+
44
+ def location_dict
45
+ {
46
+ 'A' => ['台北市', 10],
47
+ 'B' => ['台中市', 11],
48
+ 'C' => ['基隆市', 12],
49
+ 'D' => ['台南市', 13],
50
+ 'E' => ['高雄市', 14],
51
+ 'F' => ['台北县', 15],
52
+ 'G' => ['宜兰县', 16],
53
+ 'H' => ['桃园县', 17],
54
+ 'I' => ['嘉义市', 34],
55
+ 'J' => ['新竹县', 18],
56
+ 'K' => ['苗栗县', 19],
57
+ 'L' => ['台中县', 20],
58
+ 'M' => ['南投县', 21],
59
+ 'N' => ['彰化县', 22],
60
+ 'O' => ['新竹市', 35],
61
+ 'P' => ['云林县', 23],
62
+ 'Q' => ['嘉义县', 24],
63
+ 'R' => ['台南县', 25],
64
+ 'S' => ['高雄县', 26],
65
+ 'T' => ['屏东县', 27],
66
+ 'U' => ['花莲县', 28],
67
+ 'V' => ['台东县', 29],
68
+ 'W' => ['金门县', 32],
69
+ 'X' => ['澎湖县', 30],
70
+ 'Y' => ['阳明山管理局', 31],
71
+ 'Z' => ['连江县', 33]
72
+ }
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module IdcardOperation
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idcard_operation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - atpking
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Chinese idcard operation
56
+ email:
57
+ - atpking@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - geo_info.yml
73
+ - idcard_operation.gemspec
74
+ - lib/idcard_operation.rb
75
+ - lib/idcard_operation/.byebug_history
76
+ - lib/idcard_operation/idcard_info.rb
77
+ - lib/idcard_operation/roc_idcard_info.rb
78
+ - lib/idcard_operation/version.rb
79
+ homepage: https://github.com/jicheng1014/idcard_info
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.14
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Chinese idcard operation
103
+ test_files: []