Module: IdValidator::Concern::Helper
- Included in:
- Validator
- Defined in:
- lib/id_validator/concern/helper.rb
Instance Method Summary collapse
-
#check_address_code(address_code) ⇒ Object
检查地址码.
-
#check_birthday_code(birthday_code) ⇒ Object
检查出生日期.
-
#check_is_abandoned(address_code) ⇒ Object
检查是否已经废弃.
-
#check_order_code(order_code) ⇒ Object
检查顺序码.
-
#generate_address_code(address) ⇒ Object
生成地址码.
-
#generate_birthday_code(birthday) ⇒ Object
生成生日信息.
-
#generate_check_bit(body) ⇒ Object
生成校验码.
-
#generate_order_code(sex) ⇒ Object
生成顺序码.
-
#get_address_info(address_code) ⇒ Object
获取地址信息.
-
#get_chinese_zodiac(birthday_code) ⇒ Object
获取生肖信息.
-
#get_constellation(birthday_code) ⇒ Object
获取星座信息.
-
#get_id_argument(id_card) ⇒ Object
获取身份证信息.
Instance Method Details
#check_address_code(address_code) ⇒ Object
检查地址码
132 133 134 135 136 137 |
# File 'lib/id_validator/concern/helper.rb', line 132 def check_address_code(address_code) address_info = get_address_info(address_code.to_s) return false if address_info[:province].nil? true end |
#check_birthday_code(birthday_code) ⇒ Object
检查出生日期
140 141 142 143 144 |
# File 'lib/id_validator/concern/helper.rb', line 140 def check_birthday_code(birthday_code) return false unless Func.check_birthday(birthday_code.to_s) true end |
#check_is_abandoned(address_code) ⇒ Object
检查是否已经废弃
154 155 156 157 158 |
# File 'lib/id_validator/concern/helper.rb', line 154 def check_is_abandoned(address_code) address = IdValidator::Config.latest_address_info.fetch(address_code.to_i, nil) address.nil? end |
#check_order_code(order_code) ⇒ Object
检查顺序码
147 148 149 150 151 |
# File 'lib/id_validator/concern/helper.rb', line 147 def check_order_code(order_code) return false if order_code.length != 3 true end |
#generate_address_code(address) ⇒ Object
生成地址码
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/id_validator/concern/helper.rb', line 174 def generate_address_code(address) key = IdValidator::Config.latest_address_info.key(address).to_s case when key.empty? reg = %r(^\d{4}(?!00)\d{2}$) return Func.get_random_address_code(reg) when key[0] == '8' return key when key =~ /^\d{2}0000$/ reg = %r(^#{key[0..1]}\d{2}(?!00)\d{2}$) return Func.get_random_address_code(reg) when key =~ /^\d{4}00$/ reg = %r(^#{key[0..3]}(?!00)\d{2}$) return Func.get_random_address_code(reg) else return key.to_s end end |
#generate_birthday_code(birthday) ⇒ Object
生成生日信息
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/id_validator/concern/helper.rb', line 195 def generate_birthday_code(birthday) return birthday if Func.check_birthday(birthday) birthday = Func.get_str_pad(birthday.to_s, 8) year = Func.get_random_right_num(birthday[0..3].to_i, 1900, Time.now.year - 1) month = Func.get_random_right_num(birthday[4..5].to_i, 1, 12) day = Func.get_random_right_num(birthday[6..7], 1, 28) year = Func.get_str_pad(year.to_s, 4) month = Func.get_str_pad(month.to_s, 2, '0', false) day = Func.get_str_pad(day.to_s, 2, '0', false) result = year + month + day if Func.check_birthday(result) result else Func.get_random_birthday_code end end |
#generate_check_bit(body) ⇒ Object
生成校验码
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/id_validator/concern/helper.rb', line 161 def generate_check_bit(body) body = body.to_s.reverse return nil if body.length != 17 body_sum = 0 weight = (2..18).map{ |a| 2**(a - 1) % 11 } weight.each_with_index{ |w, i| body_sum += body[i].to_i * w } check_bit = (12 - (body_sum % 11)) % 11 check_bit < 10 ? check_bit.to_s : 'X' end |
#generate_order_code(sex) ⇒ Object
生成顺序码
216 217 218 219 220 221 222 |
# File 'lib/id_validator/concern/helper.rb', line 216 def generate_order_code(sex) order_code = rand(1..999) order_code -= 1 if sex != (order_code % 2) Func.get_str_pad(order_code, 3, '0', false) end |
#get_address_info(address_code) ⇒ Object
获取地址信息
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/id_validator/concern/helper.rb', line 92 def get_address_info(address_code) province_address_code = "#{address_code[0...2]}0000" city_address_code = "#{address_code[0...4]}00" is_mainland = (address_code[0].to_i != 8) { province: Func.get_address_info(province_address_code), city: is_mainland ? Func.get_address_info(city_address_code) : nil, district: is_mainland ? Func.get_address_info(address_code) : nil } end |
#get_chinese_zodiac(birthday_code) ⇒ Object
获取生肖信息
124 125 126 127 128 129 |
# File 'lib/id_validator/concern/helper.rb', line 124 def get_chinese_zodiac(birthday_code) # 1900为子鼠 index = (birthday_code[0...4].to_i - 1900) % 12 IdValidator::Config.chinese_zodiac[index] end |
#get_constellation(birthday_code) ⇒ Object
获取星座信息
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/id_validator/concern/helper.rb', line 106 def get_constellation(birthday_code) date = Date.parse(birthday_code.to_s) month = date.month day = date.day constellation = IdValidator::Config.constellation start_day = constellation[month][:start_date].split('-').last.to_i if day < start_day tmp_month = month - 1 == 0 ? 12 : month - 1 constellation[tmp_month][:name] else constellation[month][:name] end end |
#get_id_argument(id_card) ⇒ Object
获取身份证信息
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/id_validator/concern/helper.rb', line 67 def get_id_argument(id_card) id_card = id_card.to_s.upcase if id_card.length == 18 { body: id_card[0...17], address_code: id_card[0...6], birthday_code: id_card[6...14], order_code: id_card[14...17], check_bit: id_card[17], type: 18 } else { body: id_card, address_code: id_card[0...6], birthday_code: '19' + id_card[6...12], order_code: id_card[12...15], check_bit: '', type: 15 } end end |