Class: Ekylibre::PicturesExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/pictures_exchanger.rb

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, deprecated?, exchanger_name, export, exporters, find, find_and_import, find_by, human_name, import, import!, importers, importers_selection, inherited, #initialize, register_exchanger

Constructor Details

This class inherits a constructor from ActiveExchanger::Base

Instance Method Details

#importObject

Create or updates pictures


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/exchangers/ekylibre/pictures_exchanger.rb', line 4

def import
  # Unzip file
  dir = w.tmp_dir
  Zip::File.open(file) do |zile|
    zile.each do |entry|
      file = dir.join(entry.name)
      FileUtils.mkdir_p(file.dirname)
      entry.extract(file)
    end
  end

  mimetype = File.read(dir.join('mimetype')).to_s.strip
  nature = mimetype.split('.').last

  identifier = File.read(dir.join('identifier')).to_s.strip.to_sym

  klass = nil
  if nature == 'products'
    klass = Product
  else
    raise "Unknown picture type: #{mimetype.inspect}"
  end

  Dir.chdir(dir.join('pictures')) do
    Dir.glob('*') do |picture|
      path = Pathname.new(picture)
      extn = path.extname
      id = File.basename(picture, extn)
      if record = klass.find_by(identifier => id)
        f = File.open(picture)
        record.picture = f
        record.save!
      end
    end
  end
end