Class: Scribo::SiteExportService

Inherits:
ApplicationService show all
Defined in:
app/services/scribo/site_export_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

#call

Constructor Details

#initialize(site) ⇒ SiteExportService

Returns a new instance of SiteExportService.



9
10
11
12
# File 'app/services/scribo/site_export_service.rb', line 9

def initialize(site)
  super()
  @site = site
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



7
8
9
# File 'app/services/scribo/site_export_service.rb', line 7

def site
  @site
end

Instance Method Details

#performObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/scribo/site_export_service.rb', line 14

def perform
  return unless site.contents.count.positive?

  zip_name = (site.properties['title'] || 'untitled').to_s
  base_path = "#{zip_name}/"

  stringio = Zip::OutputStream.write_buffer do |zio|
    site.contents.each do |content|
      content_path = content_path_for_zip(content)
      next unless content_path

      next if content.kind == 'folder'

      zio.put_next_entry(base_path + content_path)
      zio.write content.data_with_frontmatter
    rescue StandardError => e
      Rails.logger.error "Error while exporting content #{content.id}: #{e.message}"
    end
  end

  ["#{zip_name}.zip", stringio.string]
end