Class: Plum::SiteArchive::Exporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/plum/site_archive.rb

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Exporter

Returns a new instance of Exporter.



25
26
27
# File 'app/services/plum/site_archive.rb', line 25

def initialize(site)
  @site = site
end

Instance Method Details

#write(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/plum/site_archive.rb', line 29

def write(path)
  destination = Pathname(path).expand_path
  destination.dirname.mkpath
  FileUtils.rm_f(destination)

  Zip::File.open(destination, create: true) do |zip|
    zip.get_output_stream("manifest.json") { |stream| stream.write(JSON.pretty_generate(manifest)) }
    site.assets.with_attached_file.find_each do |asset|
      next unless asset.file.attached?

      zip.get_output_stream(asset_path(asset)) do |stream|
        asset.file.blob.open { |file| IO.copy_stream(file, stream) }
      end
    end
  end
  destination
end