idcard_verity 0.1.1 → 0.2.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.
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'idcard_verity/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "idcard_verity"
8
+ spec.version = IdcardVerity::VERSION
9
+ spec.authors = ["lvjianwei"]
10
+ spec.email = ["lvjw@zhuopucapital.com"]
11
+
12
+ spec.summary = %q{中国身份证验证,包括地区,生日,校验位的验证。支持15位和18位身份证。}
13
+ spec.description = %q{中国身份证验证,包括地区,生日,校验位的验证。支持15位和18位身份证。}
14
+ spec.homepage = "https://github.com/leosaullv/idcard_verity"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,3 @@
1
+ require "idcard_verity/version"
2
+ require "idcard_verity/idcard"
3
+
@@ -0,0 +1,109 @@
1
+ require 'yaml'
2
+ module IdcardVerity
3
+ class Idcard
4
+ attr_reader :idcard
5
+
6
+ def initialize(idcard)
7
+ @idcard = idcard.to_s
8
+ @geoinfo = load_geoinfo
9
+ end
10
+
11
+ def idcard
12
+ @idcard
13
+ end
14
+
15
+ def geoinfo
16
+ @geoinfo
17
+ end
18
+
19
+ def type
20
+ return true if size == 18
21
+ false
22
+ end
23
+
24
+ def size
25
+ idcard.size
26
+ end
27
+
28
+ def body
29
+ idcard.slice(0..-2)
30
+ end
31
+
32
+ def lastbit
33
+ return idcard.slice(-1..-1) if type
34
+ false
35
+ end
36
+
37
+ def address
38
+ idcard.slice(0..5)
39
+ end
40
+
41
+ def others
42
+ idcard.slice(-4..-2)
43
+ end
44
+
45
+ def birth
46
+ return idcard.slice(6..14) if type
47
+ '19' + idcard.slice(6..12)
48
+ end
49
+
50
+ def year
51
+ birth.slice(0..3).to_i
52
+ end
53
+
54
+ def month
55
+ birth.slice(4..5).to_i
56
+ end
57
+
58
+ def day
59
+ birth.slice(6..7).to_i
60
+ end
61
+
62
+ def right?
63
+ size == 15 || size == 18
64
+ end
65
+
66
+ def check_birth
67
+ !(year < 1900 || month > 12 || month == 0 || day > 31 || day == 0)
68
+ end
69
+
70
+ def check_bit
71
+ return true unless lastbit
72
+ return true if lastbit == sysbit
73
+ end
74
+
75
+ def check_geoinfo
76
+ address.length == 6
77
+ end
78
+
79
+ def sysbit
80
+ sum = 0
81
+ body.split('').each_with_index do |bit,i|
82
+ sum += bit.to_i * weight(18 - i)
83
+ end
84
+ office_bit.at(sum.divmod(11).last)
85
+ end
86
+
87
+ def valid?
88
+ right? && check_birth && check_bit && check_geoinfo
89
+ end
90
+
91
+ private
92
+
93
+ def weight i
94
+ (2 ** (i-1)).divmod(11).last
95
+ end
96
+
97
+ def load_geoinfo
98
+ yml = YAML.load_file(File.expand_path("../../../geo_info.yml", __FILE__))
99
+ yml['geoinfo']
100
+ end
101
+
102
+ def office_bit
103
+ ['1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2']
104
+ end
105
+
106
+ end
107
+ end
108
+
109
+
@@ -0,0 +1,3 @@
1
+ module IdcardVerity
2
+ VERSION = "0.2.0"
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idcard_verity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lvjianwei
@@ -44,7 +44,20 @@ email:
44
44
  executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
- files: []
47
+ files:
48
+ - ".gitignore"
49
+ - CODE_OF_CONDUCT.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - geo_info.yml
57
+ - idcard_verity.gemspec
58
+ - lib/idcard_verity.rb
59
+ - lib/idcard_verity/idcard.rb
60
+ - lib/idcard_verity/version.rb
48
61
  homepage: https://github.com/leosaullv/idcard_verity
49
62
  licenses:
50
63
  - MIT