jpp_customercode_transfer 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.
Files changed (49) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +35 -0
  4. data/app/assets/javascripts/jpp_customercode_transfer/application.js +15 -0
  5. data/app/assets/stylesheets/jpp_customercode_transfer/application.css +13 -0
  6. data/app/controllers/jpp_customercode_transfer/application_controller.rb +4 -0
  7. data/app/helpers/jpp_customercode_transfer/application_helper.rb +4 -0
  8. data/app/models/jpp_customercode_transfer/zip_code_list.rb +275 -0
  9. data/app/views/layouts/jpp_customercode_transfer/application.html.erb +14 -0
  10. data/config/routes.rb +2 -0
  11. data/db/migrate/20120401133633_create_jpp_customercode_transfer_zip_code_lists.rb +22 -0
  12. data/lib/jpp_customercode_transfer.rb +4 -0
  13. data/lib/jpp_customercode_transfer/engine.rb +5 -0
  14. data/lib/jpp_customercode_transfer/version.rb +3 -0
  15. data/lib/tasks/jpp_customercode_transfer_tasks.rake +4 -0
  16. data/test/dummy/README.rdoc +261 -0
  17. data/test/dummy/Rakefile +7 -0
  18. data/test/dummy/app/assets/javascripts/application.js +15 -0
  19. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  20. data/test/dummy/app/controllers/application_controller.rb +3 -0
  21. data/test/dummy/app/helpers/application_helper.rb +2 -0
  22. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  23. data/test/dummy/config.ru +4 -0
  24. data/test/dummy/config/application.rb +56 -0
  25. data/test/dummy/config/boot.rb +10 -0
  26. data/test/dummy/config/database.yml +25 -0
  27. data/test/dummy/config/environment.rb +5 -0
  28. data/test/dummy/config/environments/development.rb +37 -0
  29. data/test/dummy/config/environments/production.rb +67 -0
  30. data/test/dummy/config/environments/test.rb +37 -0
  31. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  32. data/test/dummy/config/initializers/inflections.rb +15 -0
  33. data/test/dummy/config/initializers/mime_types.rb +5 -0
  34. data/test/dummy/config/initializers/secret_token.rb +7 -0
  35. data/test/dummy/config/initializers/session_store.rb +8 -0
  36. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  37. data/test/dummy/config/locales/en.yml +5 -0
  38. data/test/dummy/config/routes.rb +4 -0
  39. data/test/dummy/public/404.html +26 -0
  40. data/test/dummy/public/422.html +26 -0
  41. data/test/dummy/public/500.html +25 -0
  42. data/test/dummy/public/favicon.ico +0 -0
  43. data/test/dummy/script/rails +6 -0
  44. data/test/fixtures/jpp_customercode_transfer/zip_code_lists.yml +11 -0
  45. data/test/integration/navigation_test.rb +10 -0
  46. data/test/jpp_customercode_transfer_test.rb +7 -0
  47. data/test/test_helper.rb +15 -0
  48. data/test/unit/jpp_customercode_transfer/zip_code_list_test.rb +9 -0
  49. metadata +165 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = JppCustomercodeTransfer
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'JppCustomercodeTransfer'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ task :default => :spec
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module JppCustomercodeTransfer
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module JppCustomercodeTransfer
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,275 @@
1
+ # -*- encoding: utf-8 -*-
2
+ class String
3
+ def strip_hypen_front_alphabet
4
+ s = ""
5
+ c = 0
6
+ while true
7
+ break if c > self.size
8
+ c2 = c
9
+ c = self.index(/\-[A-Z]/, c)
10
+ unless c
11
+ s = s + self[c2..-1]
12
+ break
13
+ end
14
+ s = s + self[c2...c]
15
+ c = c + 1
16
+ end
17
+
18
+ return s
19
+ end
20
+
21
+ def strip_hypen_behind_alphabet
22
+ s2 = ""
23
+ pos = 0
24
+ doFlag = true
25
+ while doFlag
26
+ break if pos > self.size
27
+ pos2 = pos
28
+ pos = self.index(/[A-Z]\-/, pos)
29
+ unless pos
30
+ s2 = s2 + self[pos2..-1]
31
+ break
32
+ end
33
+ s2 = s2 + self[pos2..pos]
34
+ pos = pos + 2
35
+ end
36
+
37
+ #puts "strip_hypen_around_alphabet s2=#{s2}"
38
+ return s2
39
+ end
40
+
41
+ def self.kanju2num(kan)
42
+ nums = %w|〇 一 二 三 四 五 六 七 八 九|
43
+ digits = %w|十 百 千|
44
+ digits2 = %w|万 億 兆|
45
+
46
+ pieces = kan.split(//)
47
+ stack = 0
48
+ sum = 0
49
+ result = 0
50
+ while kan = pieces.shift
51
+ if i = nums.index(kan)
52
+ stack = i
53
+ elsif i = digits.index(kan)
54
+ stack = 1 if stack == 0
55
+ sum += stack * (10 ** (i + 1))
56
+ stack = 0
57
+ elsif i = digits2.index(kan)
58
+ result += (sum + stack) * ( 10 ** ((i + 1) * 4))
59
+ sum = 0
60
+ stack = 0
61
+ end
62
+ end
63
+ result += sum + stack
64
+ return result.to_s
65
+ end
66
+ end
67
+
68
+ module JppCustomercodeTransfer
69
+ class ZipCodeList < ActiveRecord::Base
70
+ def self.generate_japanpost_customer_code(zip_code, address)
71
+ # see http://www.post.japanpost.jp/zipcode/zipmanual/p19.html
72
+ sp1 = ["丁目","丁","番地","番","号","地割","線","の","ノ"]
73
+
74
+ logger.info "@@@ Start zip_code=#{zip_code} address=#{address}"
75
+ if zip_code.blank? or address.blank?
76
+ raise ArgumentError, "zip_code or address is empty."
77
+ end
78
+ zip_code = zip_code.gsub(/\D/, "")
79
+ unless zip_code.size == 7
80
+ raise ArgumentError, "zip_code is invalid. (size invalid)"
81
+ end
82
+ zip = ZipCodeList.find_by_zipcode(zip_code)
83
+ unless zip
84
+ raise ArgumentError, "zip_code is invalid. (no record)"
85
+ end
86
+ if zip.zipcode[5, 2] == "00"
87
+ # 下2ケタが00の場合はバーコードは利用しない
88
+ return ""
89
+ end
90
+
91
+ # whitespece remove
92
+ #address = address.strip_with_full_size_space
93
+ asub = zip.check_include_address(address)
94
+ logger.debug "asub=#{asub}"
95
+ a2 = address.delete(asub)
96
+ logger.debug "a2=#{a2}"
97
+
98
+ # rule 1 ascii alphabet small to large
99
+ a2.upcase!
100
+
101
+ # rule 2
102
+ a2 = a2.gsub(/[&\/..・]/, "")
103
+ logger.debug "rule2 a3=#{a2}"
104
+
105
+ # sp1 kanji number to number-char
106
+ sp1.each do |c|
107
+ #logger.debug "a4=#{a4} c=#{c}"
108
+ pos = a2.index(c)
109
+ if pos
110
+ #logger.debug "find pos=#{pos}"
111
+ break_flag = false
112
+ rs = ""
113
+ pos2 = pos - 1
114
+ #logger.debug "pos2=#{pos2}"
115
+ loop do
116
+ #logger.debug "a4=#{a4[pos2]}"
117
+ if a2[pos2] =~ /[一二三四五六七八九十〇]/
118
+ rs += a2[pos2]
119
+ logger.debug "append rs=#{rs}"
120
+ if pos2 < 0
121
+ break_flag = true
122
+ end
123
+ else
124
+ break_flag = true
125
+ end
126
+ if break_flag
127
+ logger.debug "break. pos2=#{pos2} rs=#{rs}"
128
+ if rs.present?
129
+ rs.reverse!
130
+ #logger.debug "rs=#{rs}"
131
+ rp = String.kanju2num(rs)
132
+ a2[pos2+1, rs.size] = rp
133
+ #logger.debug "a4rp=#{a3}"
134
+ end
135
+ break
136
+ end
137
+ pos2 = pos2 - 1
138
+ end
139
+ end
140
+ end
141
+ # rule 3
142
+ a2 = a2.gsub(/[^0-9A-Z\-]{1,}/, "-").gsub(/[A-Z]{2,}/, "-")
143
+ a2 = a2.gsub(/[-]{2,}/, "-").gsub(/^[-]|[-]$/, "")
144
+ logger.debug "rule3-2 a2=#{a2}"
145
+
146
+ if a2 =~ /\dF$/
147
+ a2 = a2.gsub(/F$/, "")
148
+ end
149
+ a2 = a2.gsub(/F/, "-")
150
+ logger.debug "rule3-3 a2=#{a2}"
151
+
152
+ a2 = a2.strip_hypen_front_alphabet
153
+ logger.debug "rule3-4 a2=#{a2}"
154
+ a2 = a2.strip_hypen_behind_alphabet
155
+ logger.debug "rule3-5 a2=#{a2}"
156
+
157
+ code = zip_code + a2
158
+
159
+ logger.debug "return code=#{code}"
160
+
161
+ return code
162
+ end
163
+
164
+ def check_include_address(address)
165
+ suffix = "丁目"; range_char = "〜"
166
+
167
+ #puts "address=#{address}"
168
+ address = self.build_asub(address)
169
+
170
+ if self.flag10 && self.flag10 == 1
171
+ #puts "flag10 multiple address"
172
+ self.region_name =~ /(.*)((.*)#{suffix})/
173
+ unless $2
174
+ logger.error "error"
175
+ raise ArgumentError, "trap 1 at check_include_address"
176
+ end
177
+ a = $1.dup
178
+
179
+ #puts "$2=#{$2}"
180
+ s = $2.dup.tr("0-9", "0-9")
181
+ s =~ /(\d{1,})#{range_char}(\d{1,})/
182
+ #puts "range check $1=#{$1} $2=#{$2}"
183
+ if $1.present? && $2.present?
184
+ ($1..$2).each do |i|
185
+ asub = self.build_asub(self.prefecture_name, self.city_name, a, i.to_s, suffix)
186
+ #puts asub
187
+ if address.index(asub) == 0
188
+ return self.prefecture_name+self.city_name+a
189
+ end
190
+ end
191
+ end
192
+
193
+ raise ArgumentError, "trap 2 at check_include_address"
194
+ else
195
+ asub = self.build_asub(self.prefecture_name, self.city_name, self.region_name)
196
+ logger.debug "asub1=#{asub}"
197
+ unless address.index(asub) == 0
198
+ logger.info "try matching city+region"
199
+ asub = self.build_asub(self.city_name, self.region_name)
200
+ logger.debug "asub2=#{asub}"
201
+ logger.debug "address=#{asub}"
202
+ unless address.index(asub) == 0
203
+ raise ArgumentError, "trap 3 at check_include_address"
204
+ end
205
+ end
206
+ end
207
+
208
+ return asub
209
+ end
210
+ def build_asub(*args)
211
+ asub = args.join
212
+ #puts "asub=#{asub} flag11=#{self.flag11}"
213
+ if self.flag11 = 1
214
+ asub = asub.gsub(/大字/, "")
215
+ end
216
+ return asub
217
+ end
218
+
219
+ def self.import(filename = nil)
220
+ zipcode_import = self.new
221
+ rows = zipcode_import.open_import_file(filename)
222
+ ZipCodeList.delete_all
223
+
224
+ #ZipCodeList.transaction do
225
+ rows.each_with_index do |row, row_num|
226
+ h = {}
227
+ h[:union_code] = row[0]
228
+ h[:zipcode] = row[2]
229
+ h[:prefectrure_name_kana] = row[3]
230
+ h[:city_name_kana] = row[4]
231
+ h[:region_name_kana] = row[5]
232
+ h[:prefecture_name] = row[6]
233
+ h[:city_name] = row[7]
234
+ h[:region_name] = row[8]
235
+ h[:flag10] = row[9]
236
+ h[:flag11] = row[10]
237
+ h[:flag12] = row[11]
238
+ h[:flag13] = row[12]
239
+ h[:flag14] = row[13]
240
+ h[:update_flag] = row[14]
241
+ ZipCodeList.create!(h)
242
+
243
+ if row_num % 200 == 0
244
+ Sunspot.commit
245
+ GC.start
246
+ puts "#{row_num} success."
247
+ end
248
+ end
249
+ #end
250
+ Sunspot.commit
251
+ end
252
+
253
+ def open_import_file(upload_file_path)
254
+ tempfile = Tempfile.new('zipcode_import_file')
255
+ #TODO
256
+ open(upload_file_path){|f|
257
+ f.each{|line|
258
+ tempfile.puts(NKF.nkf('-w -Lu', line))
259
+ }
260
+ }
261
+ tempfile.close
262
+
263
+ file = CSV.open(tempfile.path, :col_sep => "\t")
264
+ #header = file.first
265
+ #rows = CSV.open(tempfile.path, :headers => header, :col_sep => "\t")
266
+ rows = CSV.open(tempfile.path)
267
+
268
+ #ResourceImportResult.create(:resource_import_file => self, :body => header.join("\t"), :error_msg => "HEADER DATA")
269
+ tempfile.close(true)
270
+ file.close
271
+ rows
272
+ end
273
+ end
274
+ end
275
+
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>JppCustomercodeTransfer</title>
5
+ <%= stylesheet_link_tag "jpp_customercode_transfer/application", :media => "all" %>
6
+ <%= javascript_include_tag "jpp_customercode_transfer/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ JppCustomercodeTransfer::Engine.routes.draw do
2
+ end
@@ -0,0 +1,22 @@
1
+ class CreateJppCustomercodeTransferZipCodeLists < ActiveRecord::Migration
2
+ def change
3
+ create_table :jpp_customercode_transfer_zip_code_lists do |t|
4
+ t.integer :union_code
5
+ t.string :zipcode5
6
+ t.string :zipcode
7
+ t.string :prefectrure_name_kana
8
+ t.string :city_name_kana
9
+ t.string :region_name_kana
10
+ t.string :prefecture_name
11
+ t.string :city_name
12
+ t.string :region_name
13
+ t.integer :flag10
14
+ t.integer :flag11
15
+ t.integer :flag12
16
+ t.integer :flag13
17
+ t.integer :flag14
18
+ t.integer :update_flag
19
+ t.timestamps
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ require "jpp_customercode_transfer/engine"
2
+
3
+ module JppCustomercodeTransfer
4
+ end
@@ -0,0 +1,5 @@
1
+ module JppCustomercodeTransfer
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace JppCustomercodeTransfer
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module JppCustomercodeTransfer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :jpp_customercode_transfer do
3
+ # # Task goes here
4
+ # end