chinese_pid 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b1b589563fc378260a3d1ab383b9370cb9a34ad
4
+ data.tar.gz: 168982d178676c1304d1a095fab35cb8d717b5de
5
+ SHA512:
6
+ metadata.gz: 1a318577f0af76a709f848800e32acd887a747d2b368a12ec8478fd1acda7036c3245be17a5cf330074fab1b64b4673e6301496232738acb7d731bfe17b75843
7
+ data.tar.gz: 9710c79bb3e1a0d7b0d7b105b136e69bdbbd63d8540722be09caa566ad9dbe5509aaeaf2e98491b53d691723efa36034f3f87966e034d7b4984a934d0a8581c8
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ notifications:
5
+ hipchat: c3f3f577167a8a61d3a91a3c9816e6@Notify
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chinese_pid.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 刘泽磊
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 liuzelei
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.
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # PRC公民身份证号码验证和信息提取
2
+
3
+ [![Travis](https://travis-ci.org/liuzelei/chinese_pid.svg?branch=master)](https://travis-ci.org/liuzelei/chinese_pid)
4
+
5
+ ## 安装
6
+
7
+ Gemfile文件中添加:
8
+
9
+ gem 'chinese_pid'
10
+
11
+ bundle:
12
+
13
+ $ bundle
14
+
15
+ 或者直接安装:
16
+
17
+ $ gem install chinese_pid
18
+
19
+ ## 使用
20
+
21
+ ChinesePid.new("your id card").valid? #返回是否通过验证
22
+ ChinesePid.new("your id card").gender #1是男性/0是女性
23
+ ChinesePid.new("your id card").birthday #Date类型的生日
24
+
25
+ 支持15位和18位身份证号
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,22 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "chinese_pid"
6
+ spec.version = "0.0.1"
7
+ spec.authors = ["liuzelei"]
8
+ spec.email = ["liuzelei@gmail.com"]
9
+ spec.summary = "验证RPC公民身份证号码"
10
+ spec.description = "RUBY版本"
11
+ spec.homepage = ""
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.6"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "rspec", "~> 3.0"
22
+ end
@@ -0,0 +1,56 @@
1
+ class ChinesePid
2
+ CITY_CODES = {
3
+ "11" => "北京市", "12" => "天津市", "13" => "河北省", "14" => "山西省", "15" => "内蒙古自治区",
4
+ "21" => "辽宁省", "22" => "吉林省", "23" => "黑龙江省",
5
+ "31" => "上海市", "32" => "江苏省", "33" => "浙江省", "34" => "安徽省", "35" => "福建省", "36" => "江西省", "37" => "山东省",
6
+ "41" => "河南省", "42" => "湖北省", "43" => "湖南省", "44" => "广东省", "45" => "广西壮族自治区", "46" => "海南省",
7
+ "50" => "重庆市", "51" => "四川省", "52" => "贵州省", "53" => "云南省", "54" => "西藏自治区",
8
+ "61" => "陕西省", "62" => "甘肃省", "63" => "青海省", "64" => "宁夏回族自治区", "65" => "新疆维吾尔自治区",
9
+ "71" => "台湾省",
10
+ "81" => "香港特别行政区", "82" => "澳门特别行政区",
11
+ "91" => "国外"
12
+ }
13
+
14
+ def initialize(pid)
15
+ @pid = pid
16
+ end
17
+
18
+ def gender
19
+ return nil unless valid?
20
+ if old_standard?
21
+ return @pid[14].to_i % 2
22
+ else
23
+ return @pid[16].to_i % 2
24
+ end
25
+ end
26
+
27
+ def valid?
28
+ return false if @pid.size != 15 && @pid.size != 18
29
+ return false unless /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|x|X)?$/.match(@pid)
30
+ return false if CITY_CODES[@pid[0, 2]].nil?
31
+ unless old_standard?
32
+ factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
33
+ parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]
34
+ sum = 0
35
+ for index in 0..16
36
+ sum += @pid[index].to_i * factor[index]
37
+ end
38
+ return false if parity[sum % 11].to_s.upcase != @pid[17].to_s.upcase
39
+ end
40
+ return true
41
+ end
42
+
43
+ def birthday
44
+ return nil unless valid?
45
+ if old_standard?
46
+ return Date.parse("19#{@pid[6, 6]}")
47
+ else
48
+ return Date.parse(@pid[6, 8])
49
+ end
50
+ end
51
+
52
+ def old_standard?
53
+ return true if @pid.size == 15
54
+ return false
55
+ end
56
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ChinesePid do
4
+ it "validate chinese pid" do
5
+ p1 = ChinesePid.new("310104197912241276")
6
+ expect(p1.gender).to eq(1)
7
+ expect(p1.birthday).to eq(Date.new(1979, 12, 24))
8
+ expect(p1.valid?).to be_truthy
9
+
10
+ p2 = ChinesePid.new("310112199412155389")
11
+ expect(p2.gender).to eq(0)
12
+ expect(p2.birthday).to eq(Date.new(1994, 12, 15))
13
+ expect(p2.valid?).to be_truthy
14
+
15
+ p3 = ChinesePid.new("310112199412155380")
16
+ expect(p3.gender).to be_nil
17
+ expect(p3.birthday).to be_nil
18
+ expect(p3.valid?).to be_falsey
19
+
20
+ p4 = ChinesePid.new("310104791224127")
21
+ expect(p4.gender).to eq(1)
22
+ expect(p4.birthday).to eq(Date.new(1979, 12, 24))
23
+ expect(p4.valid?).to be_truthy
24
+
25
+ p5 = ChinesePid.new("310112941215538")
26
+ expect(p5.gender).to eq(0)
27
+ expect(p5.birthday).to eq(Date.new(1994, 12, 15))
28
+ expect(p5.valid?).to be_truthy
29
+
30
+ p6 = ChinesePid.new("31011294121553")
31
+ expect(p6.gender).to be_nil
32
+ expect(p6.birthday).to be_nil
33
+ expect(p6.valid?).to be_falsey
34
+
35
+ p7 = ChinesePid.new("31011619780516889X")
36
+ expect(p7.gender).to eq(1)
37
+ expect(p7.birthday).to eq(Date.new(1978, 5, 16))
38
+ expect(p7.valid?).to be_truthy
39
+
40
+ p8 = ChinesePid.new("110101201201012998")
41
+ expect(p8.gender).to eq(1)
42
+ expect(p8.birthday).to eq(Date.new(2012, 1, 1))
43
+ expect(p8.valid?).to be_truthy
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec'
2
+ require 'chinese_pid'
3
+
4
+ RSpec.configure do |config|
5
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinese_pid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - liuzelei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-11 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: RUBY版本
56
+ email:
57
+ - liuzelei@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - chinese_pid.gemspec
70
+ - lib/chinese_pid.rb
71
+ - spec/chinese_pid_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: ''
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: "验证RPC公民身份证号码"
97
+ test_files:
98
+ - spec/chinese_pid_spec.rb
99
+ - spec/spec_helper.rb