Class: JppCustomercodeTransfer::ZipCodeList
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- JppCustomercodeTransfer::ZipCodeList
- Defined in:
- app/models/jpp_customercode_transfer/zip_code_list.rb
Class Method Summary collapse
Instance Method Summary collapse
- #build_asub(*args) ⇒ Object
- #check_include_address(address) ⇒ Object
- #open_import_file(upload_file_path) ⇒ Object
Class Method Details
.generate_japanpost_customer_code(zip_code, address) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'app/models/jpp_customercode_transfer/zip_code_list.rb', line 78 def self.generate_japanpost_customer_code(zip_code, address) # see http://www.post.japanpost.jp/zipcode/zipmanual/p19.html sp1 = ["丁目","丁","番地","番","号","地割","線","の","ノ"] logger.info "@@@ Start zip_code=#{zip_code} address=#{address}" if zip_code.blank? or address.blank? raise ArgumentError, "zip_code or address is empty." end zip_code = zip_code.gsub(/\D/, "") unless zip_code.size == 7 raise ArgumentError, "zip_code is invalid. (size invalid)" end zip = ZipCodeList.find_by_zipcode(zip_code) unless zip raise ArgumentError, "zip_code is invalid. (no record)" end if zip.zipcode[5, 2] == "00" # 下2ケタが00の場合はバーコードは利用しない return "" end # whitespece remove #address = address.strip_with_full_size_space asub = zip.check_include_address(address) logger.debug "asub=#{asub}" a2 = address.delete(asub) logger.debug "a2=#{a2}" # rule 1 ascii alphabet small to large a2.upcase! # rule 2 a2 = a2.gsub(/[&\/..・]/, "") logger.debug "rule2 a3=#{a2}" # sp1 kanji number to number-char sp1.each do |c| #logger.debug "a4=#{a4} c=#{c}" pos = a2.index(c) if pos #logger.debug "find pos=#{pos}" break_flag = false rs = "" pos2 = pos - 1 #logger.debug "pos2=#{pos2}" loop do #logger.debug "a4=#{a4[pos2]}" if a2[pos2] =~ /[一二三四五六七八九十〇]/ rs += a2[pos2] logger.debug "append rs=#{rs}" if pos2 < 0 break_flag = true end else break_flag = true end if break_flag logger.debug "break. pos2=#{pos2} rs=#{rs}" if rs.present? rs.reverse! #logger.debug "rs=#{rs}" rp = String.kanju2num(rs) a2[pos2+1, rs.size] = rp #logger.debug "a4rp=#{a3}" end break end pos2 = pos2 - 1 end end end # rule 3 a2 = a2.gsub(/[^0-9A-Z\-]{1,}/, "-").gsub(/[A-Z]{2,}/, "-") a2 = a2.gsub(/[-]{2,}/, "-").gsub(/^[-]|[-]$/, "") logger.debug "rule3-2 a2=#{a2}" if a2 =~ /\dF$/ a2 = a2.gsub(/F$/, "") end a2 = a2.gsub(/F/, "-") logger.debug "rule3-3 a2=#{a2}" a2 = a2.strip_hypen_front_alphabet logger.debug "rule3-4 a2=#{a2}" a2 = a2.strip_hypen_behind_alphabet logger.debug "rule3-5 a2=#{a2}" code = zip_code + a2 logger.debug "return code=#{code}" return code end |
.import(filename = nil) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'app/models/jpp_customercode_transfer/zip_code_list.rb', line 231 def self.import(filename = nil) if filename.blank? raise ArgumentError, "filename is blank." end zipcode_import = self.new rows = zipcode_import.open_import_file(filename) ZipCodeList.delete_all #ZipCodeList.transaction do rows.each_with_index do |row, row_num| h = {} h[:union_code] = row[0] h[:zipcode5] = row[1] h[:zipcode] = row[2] h[:prefectrure_name_kana] = row[3] h[:city_name_kana] = row[4] h[:region_name_kana] = row[5] h[:prefecture_name] = row[6] h[:city_name] = row[7] h[:region_name] = row[8] h[:flag10] = row[9] h[:flag11] = row[10] h[:flag12] = row[11] h[:flag13] = row[12] h[:flag14] = row[13] h[:update_flag] = row[14] ZipCodeList.new(h).save! if row_num > 0 && row_num % 500 == 0 GC.start puts "#{row_num} success." end end end |
Instance Method Details
#build_asub(*args) ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'app/models/jpp_customercode_transfer/zip_code_list.rb', line 222 def build_asub(*args) asub = args.join #puts "asub=#{asub} flag11=#{self.flag11}" if self.flag11 = 1 asub = asub.gsub(/大字/, "") end return asub end |
#check_include_address(address) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'app/models/jpp_customercode_transfer/zip_code_list.rb', line 172 def check_include_address(address) suffix = "丁目"; range_char = "〜" #puts "address=#{address}" address = self.build_asub(address) if self.flag10 && self.flag10 == 1 #puts "flag10 multiple address" self.region_name =~ /(.*)((.*)#{suffix})/ unless $2 logger.error "error" raise ArgumentError, "trap 1 at check_include_address" end a = $1.dup #puts "$2=#{$2}" s = $2.dup.tr("0-9", "0-9") s =~ /(\d{1,})#{range_char}(\d{1,})/ #puts "range check $1=#{$1} $2=#{$2}" if $1.present? && $2.present? ($1..$2).each do |i| asub = self.build_asub(self.prefecture_name, self.city_name, a, i.to_s, suffix) #puts asub if address.index(asub) == 0 return self.prefecture_name+self.city_name+a end end end raise ArgumentError, "trap 2 at check_include_address" else asub = self.build_asub(self.prefecture_name, self.city_name, self.region_name) logger.debug "asub1=#{asub}" unless address.index(asub) == 0 logger.info "try matching city+region" asub = self.build_asub(self.city_name, self.region_name) logger.debug "asub2=#{asub}" logger.debug "address=#{asub}" unless address.index(asub) == 0 raise ArgumentError, "trap 3 at check_include_address" end end end # TODO #if asub.length > 20 # asus = asub[0, 20] #end return asub end |
#open_import_file(upload_file_path) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'app/models/jpp_customercode_transfer/zip_code_list.rb', line 267 def open_import_file(upload_file_path) rows = [] tempfile = Tempfile.new('zipcode_import_file') open(upload_file_path){|f| f.each{|line| tempfile.puts(NKF.nkf('-wS -Lw', line)) } } tempfile.close rows = CSV.open(tempfile.path, 'r') tempfile.close(true) rows end |