Class: Decidim::OpenDataExporter

Inherits:
Object
  • Object
show all
Includes:
TranslatableAttributes
Defined in:
decidim-core/app/services/decidim/open_data_exporter.rb

Overview

Public: It generates a ZIP file with Open Data CSV files ready to be uploaded somewhere so users can download an organization data.

Constant Summary collapse

FILE_NAME_PATTERN =
"%{host}-open-data-%{entity}.csv"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TranslatableAttributes

#attachment?, #default_locale?

Constructor Details

#initialize(organization, path, resource = nil) ⇒ OpenDataExporter

Public: Initializes the class.

organization - The Organization to export the data from. path - The String path where to write the zip file. resource - The String of the component or participatory space to export. If nil, it will export all.



21
22
23
24
25
26
# File 'decidim-core/app/services/decidim/open_data_exporter.rb', line 21

def initialize(organization, path, resource = nil)
  @organization = organization
  @path = File.expand_path path
  @resource = resource
  @help_definition = {}
end

Instance Attribute Details

#help_definitionObject (readonly)

Returns the value of attribute help_definition.



12
13
14
# File 'decidim-core/app/services/decidim/open_data_exporter.rb', line 12

def help_definition
  @help_definition
end

#organizationObject (readonly)

Returns the value of attribute organization.



12
13
14
# File 'decidim-core/app/services/decidim/open_data_exporter.rb', line 12

def organization
  @organization
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'decidim-core/app/services/decidim/open_data_exporter.rb', line 12

def path
  @path
end

#resourceObject (readonly)

Returns the value of attribute resource.



12
13
14
# File 'decidim-core/app/services/decidim/open_data_exporter.rb', line 12

def resource
  @resource
end

Instance Method Details

#exportObject



28
29
30
31
32
33
34
35
36
# File 'decidim-core/app/services/decidim/open_data_exporter.rb', line 28

def export
  dirname = File.dirname(path)
  FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
  if resource.nil?
    File.binwrite(path, data_for_all_resources)
  else
    File.write(path, data_for_resource(resource))
  end
end