id_validator 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ab0ebe883bfc647be492fdbc4fd3b80e35a4a33e2a9f3d9a97802ac4e8e99c4
4
- data.tar.gz: 84e81d6823a9aaf1ed12ffa4d1ecb0a35d54ea4c46a96a1aaf46dabb608ba280
3
+ metadata.gz: 8bd01142bac23a42e104f98cbbbd5f863b187cc2eedcf0a63df79bbb43ea2d78
4
+ data.tar.gz: 793d358b5a92c519c137f798870b67fc904cb9ae33384ee432097a879c939e76
5
5
  SHA512:
6
- metadata.gz: 35bc2b3ba1e7deb4d2340d9ad83cbf1e924a5e06917ca77f2e1cb9660ec8c1d3648a68a230955b9ebeb3a1ec56e5adcbfcba6444453a02f4b74efc512ed96938
7
- data.tar.gz: 0f77b4441b862d41b6f55d5b9bfc6801d39f986c8273ca1559fff747e849020a1a8bab19dca7481882bd385095c78a76d0d6ee6ea4b934601dcdf8274d2e5113
6
+ metadata.gz: 5c876bffcf541eed5c9cedcae239cb9c9d3b31f92af697754ce78e7d1cf23c416cbb634d45d62b2103183ac16f239f491519956350bcb7ef2d28cfc3a5dffa84
7
+ data.tar.gz: dd5a98ed7ed655d159cef97f1295db5e6991522ad039b53ef8f0b9dfd615766a040b0d2cbd6da0e31fa940eb603cd58494fc6fbde40a40dc664e72b991f97001
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 renyijiu
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/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  [![Build Status](https://travis-ci.com/renyijiu/id_validator.svg?branch=master)](https://travis-ci.com/renyijiu/id_validator)
2
-
2
+ [![Gem Version](https://badge.fury.io/rb/id_validator.svg)](https://badge.fury.io/rb/id_validator)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/c12676a3c0261956e06c/maintainability)](https://codeclimate.com/github/renyijiu/id_validator/maintainability)
3
4
 
4
5
  # IdValidator
5
6
 
@@ -105,7 +106,7 @@ IdValidator.upgrade_id('610104620927690')
105
106
 
106
107
  - [中华人民共和国民政部:行政区划代码](http://www.mca.gov.cn/article/sj/xzqh/)
107
108
 
108
- - [中华人民共和国行政区划代码历史数据集](https://github.com/jxlwqq/address-code-of-china)
109
+ - [中华人民共和国行政区划代码历史数据集](https://github.com/renyijiu/address-code-of-china)
109
110
 
110
111
  - [国务院办公厅关于印发《港澳台居民居住证申领发放办法》的通知](http://www.gov.cn/zhengce/content/2018-08/19/content_5314865.htm)
111
112
 
@@ -26,7 +26,7 @@ module IdValidator
26
26
 
27
27
  # 随意获取一个符合要求的地址码
28
28
  def get_random_address_code(reg)
29
- keys = IdValidator::Config.address_info.keys.shuffle
29
+ keys = IdValidator::Config.latest_address_info.keys.shuffle
30
30
 
31
31
  keys.find { |key| key.to_s =~ reg }.to_s
32
32
  end
@@ -51,6 +51,15 @@ module IdValidator
51
51
  def get_random_birthday_code
52
52
  Time.at(rand * Time.now.to_i).strftime('%Y%m%d')
53
53
  end
54
+
55
+ # 获取指定区划代码最新的地址名称
56
+ def get_address_info(address_code)
57
+ address_code = address_code.to_i
58
+ address_arr = IdValidator::Config.total_address_info.fetch(address_code, [])
59
+
60
+ address = address_arr.sort_by{|a| a[:end_year]}.last || {}
61
+ address.fetch(:address, nil)
62
+ end
54
63
  end
55
64
 
56
65
  module Helper
@@ -81,26 +90,15 @@ module IdValidator
81
90
 
82
91
  # 获取地址信息
83
92
  def get_address_info(address_code)
84
- address_code_hash = IdValidator::Config.address_info
85
- abandoned_address_code_hash = IdValidator::Config.abandoned_address_code
93
+ province_address_code = "#{address_code[0...2]}0000"
94
+ city_address_code = "#{address_code[0...4]}00"
86
95
 
87
- province_address_code = "#{address_code[0...2]}0000".to_i
88
- city_address_code = "#{address_code[0...4]}00".to_i
89
-
90
- province = address_code_hash[province_address_code] || abandoned_address_code_hash[province_address_code]
91
-
92
- if address_code[0].to_i != 8
93
- city = address_code_hash[city_address_code] || abandoned_address_code_hash[city_address_code]
94
- district = address_code_hash[address_code.to_i] ||abandoned_address_code_hash[address_code.to_i]
95
- else
96
- city = nil
97
- district = nil
98
- end
96
+ is_mainland = (address_code[0].to_i != 8)
99
97
 
100
98
  {
101
- province: province,
102
- city: city,
103
- district: district
99
+ province: Func.get_address_info(province_address_code),
100
+ city: is_mainland ? Func.get_address_info(city_address_code) : nil,
101
+ district: is_mainland ? Func.get_address_info(address_code) : nil
104
102
  }
105
103
  end
106
104
 
@@ -154,9 +152,9 @@ module IdValidator
154
152
 
155
153
  # 检查是否已经废弃
156
154
  def check_is_abandoned(address_code)
157
- address = IdValidator::Config.abandoned_address_code.fetch(address_code.to_i, nil)
155
+ address = IdValidator::Config.latest_address_info.fetch(address_code.to_i, nil)
158
156
 
159
- !address.nil?
157
+ address.nil?
160
158
  end
161
159
 
162
160
  # 生成校验码
@@ -174,7 +172,7 @@ module IdValidator
174
172
 
175
173
  # 生成地址码
176
174
  def generate_address_code(address)
177
- key = IdValidator::Config.address_info.key(address).to_s
175
+ key = IdValidator::Config.latest_address_info.key(address).to_s
178
176
 
179
177
  case
180
178
  when key.empty?
@@ -218,8 +216,7 @@ module IdValidator
218
216
  def generate_order_code(sex)
219
217
  order_code = rand(1..999)
220
218
 
221
- order_code = order_code % 2 == 0 ? order_code : order_code - 1 if sex == 0
222
- order_code = order_code % 2 == 0 ? order_code - 1 : order_code if sex == 1
219
+ order_code -= 1 if sex != (order_code % 2)
223
220
 
224
221
  Func.get_str_pad(order_code, 3, '0', false)
225
222
  end