Class: KenAll::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/ken_all/import.rb

Constant Summary collapse

URI =
"http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip"

Instance Method Summary collapse

Constructor Details

#initialize(opt = {visualize: true}) ⇒ Import

Returns a new instance of Import.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ken_all/import.rb', line 10

def initialize(opt = {visualize: true})
  @visualizer = KenAll::Visualizer.new(opt[:visualize])
  if old_schema?
    error = <<EOS
KenAll Error.
This project is using old schema information.
Type First.
$ rake ken_all:install:migrations
$ rake db:migrate
EOS
    raise KenAll::OldSchemaException.new(error)
  end
end

Instance Method Details

#download_file(file) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/ken_all/import.rb', line 49

def download_file(file)
  @visualizer.download_status do
    file.binmode
    open(URI, 'rb') do |read_file|
      file.write(read_file.read)
    end
    file.rewind
  end
end

#from_fileObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ken_all/import.rb', line 34

def from_file
  if ENV["FILE"].present?
    if File.extname(ENV["FILE"]).downcase == '.zip'
      import_model zip_to_csv(ENV["FILE"])
    else
      CSV.open(ENV["FILE"],:encoding => "Shift_JIS:UTF-8") do |csv|
        import_model(csv)
      end
    end
  else
    puts "Specify FILE arguments."
    puts "rake ken_all:import:file FILE=/path/to/x-ken-all.csv"
  end
end

#from_netObject



24
25
26
27
28
29
30
31
32
# File 'lib/ken_all/import.rb', line 24

def from_net
  @visualizer.screen_init do
    Tempfile.open("ken_all.zip") do |f|
      download_file(f)
      csv = zip_to_csv(f)
      import_model(csv)
    end
  end
end

#import_model(csv) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ken_all/import.rb', line 73

def import_model(csv)
  @visualizer.import_status do
    ActiveRecord::Base.transaction do
      header = [:code, :address1, :address2, :address3, :address_kana1, :address_kana2, :address_kana3]
      list = []
      merge = MergeBox.new
      csv.each do |row|
        post = Post.new(row)
        if post.adr_start?
          merge.add(post)
        elsif post.adr_end?
          merge.add(post)
          list << merge.to_array
          merge.clear
          next
        elsif merge.count > 0
          merge.add(post)
        else
          list << post.to_array
        end
      end
      KenAll::PostalCode.delete_all
      KenAll::PostalCode.import(header, list)
    end
  end
end

#old_schema?Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/ken_all/import.rb', line 100

def old_schema?
  column = KenAll::PostalCode.columns.select{|v| v.name == 'address1'}.first
  !(column.sql_type =~ /text/)
end

#zip_to_csv(zip_file) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ken_all/import.rb', line 59

def zip_to_csv(zip_file)
  csv = nil
  @visualizer.unzip_status do
    # Use Zip::File for rubyzip >= 1.0.0, Zip::ZipFile for older.
    klass = defined?(Zip::File) ? Zip::File : Zip::ZipFile
    klass.foreach(zip_file) do |entry|
      File.extname(entry.name).downcase == '.csv' or next
      csv = CSV.parse(entry.get_input_stream.read.encode("utf-8", "sjis"))
      break
    end
  end
  csv
end