Class: Etna::Clients::Magma::SimpleFileLinkingWorkflow

Inherits:
Struct
  • Object
show all
Defined in:
lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribute_nameObject

Returns the value of attribute attribute_name

Returns:

  • (Object)

    the current value of attribute_name



150
151
152
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 150

def attribute_name
  @attribute_name
end

#bucket_nameObject

Returns the value of attribute bucket_name

Returns:

  • (Object)

    the current value of bucket_name



150
151
152
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 150

def bucket_name
  @bucket_name
end

#extensionObject

Returns the value of attribute extension

Returns:

  • (Object)

    the current value of extension



150
151
152
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 150

def extension
  @extension
end

#file_collectionObject

Returns the value of attribute file_collection

Returns:

  • (Object)

    the current value of file_collection



150
151
152
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 150

def file_collection
  @file_collection
end

#folderObject

Returns the value of attribute folder

Returns:

  • (Object)

    the current value of folder



150
151
152
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 150

def folder
  @folder
end

#metis_clientObject

Returns the value of attribute metis_client

Returns:

  • (Object)

    the current value of metis_client



150
151
152
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 150

def metis_client
  @metis_client
end

#project_nameObject

Returns the value of attribute project_name

Returns:

  • (Object)

    the current value of project_name



150
151
152
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 150

def project_name
  @project_name
end

#regexObject

Returns the value of attribute regex

Returns:

  • (Object)

    the current value of regex



150
151
152
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 150

def regex
  @regex
end

Instance Method Details

#find_matching_revisionsObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 160

def find_matching_revisions
  {}.tap do |revisions|
    metis_client.find(
        Etna::Clients::Metis::FindRequest.new(
            project_name: project_name,
            bucket_name: bucket_name,
            params: [Etna::Clients::Metis::FindParam.new(
                attribute: 'name',
                predicate: 'glob',
                value: "#{folder}/**/*.#{extension}",
                type: 'file'
            )]
        )).files.all.each do |file|
      puts "Checking #{file.file_path}"
      match = regex.match(file.file_path)
      if match
        match_map = match.names.zip(match.captures).to_h
        if !match_map.include?('identifier')
          raise "Regex #{regex.source} does not include a ?<identifier> named matcher, please add one to regulate how identifiers are created."
        end

        puts "Found match"

        revision = { 'path' => "metis://#{project_name}/#{bucket_name}/#{file.file_path}", 'original_filename' => "#{File.basename(file.file_path)}" }
        if file_collection
          collection = revisions[match_map['identifier']] ||= []
          collection << revision
        else
          record = revisions[match_map['identifier']] ||= {}
          unless record.empty?
            raise "Multiple files match #{match_map['identifier']}, found #{record['path']} and #{revision['path']}"
          end
          record.update(revision)
        end
      end
    end
  end
end

#write_csv_io(filename: nil, output_io: nil) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/etna/clients/magma/workflows/update_attributes_from_csv_workflow.rb', line 151

def write_csv_io(filename: nil, output_io: nil)
  exporter = Etna::CsvExporter.new([:identifier, attribute_name.to_sym])
  exporter.with_row_writeable(filename: filename, output_io: output_io) do |row_writeable|
    find_matching_revisions.each do |identifier, value|
      row_writeable << { identifier: identifier, attribute_name.to_sym => value.to_json }
    end
  end
end