Class: Decidim::DataPortabilityFileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/data_portability_file_reader.rb

Overview

This class generates the information needed to be read by DataPortability. It creates the file_name, file_path, check that the token is valid and generates the token if no exists. Originally meant for DataPortability functionality and adding user images to this file, but other usage can be found.

Direct Known Subclasses

DataPortabilityFileZipper

Instance Method Summary collapse

Constructor Details

#initialize(user, token = nil) ⇒ DataPortabilityFileReader

Public: Initialize the reader with a user, and token

user - The user of data portability to be zipped. token - The token to be send by email, and return to controller.



12
13
14
15
16
# File 'lib/decidim/data_portability_file_reader.rb', line 12

def initialize(user, token = nil)
  @user = user
  @organization = user.organization
  @token = token
end

Instance Method Details

#file_nameObject

Returns a String with the filename of the file to be read or generate.



19
20
21
22
23
24
25
26
27
# File 'lib/decidim/data_portability_file_reader.rb', line 19

def file_name
  name = ""
  name += @user.nickname
  name += "-"
  name += @organization.name.parameterize
  name += "-"
  name += token
  name + ".zip"
end

#file_pathObject

Returns a String with the absolute file_path to be read or generate.



30
31
32
33
34
# File 'lib/decidim/data_portability_file_reader.rb', line 30

def file_path
  directory_name = Rails.root.join(Decidim::DataPortabilityUploader.new.store_dir)
  FileUtils.mkdir_p(directory_name) unless File.exist?(directory_name)
  directory_name + file_name
end

#file_path_readerObject



36
37
38
# File 'lib/decidim/data_portability_file_reader.rb', line 36

def file_path_reader
  Decidim::DataPortabilityUploader.new.retrieve_from_store!(file_name)
end

#tokenObject

Returns a the token or generate a new one



46
47
48
# File 'lib/decidim/data_portability_file_reader.rb', line 46

def token
  @token ||= generate_new_token
end

#valid_token?Boolean

Check if token is present and length equal 10

Returns:

  • (Boolean)


41
42
43
# File 'lib/decidim/data_portability_file_reader.rb', line 41

def valid_token?
  token.present? && token.length == 10
end