Class: S3FileHandler::CSV::AppendRow

Inherits:
BaseOperation show all
Defined in:
lib/s3_file_handler/csv/append_row.rb

Instance Attribute Summary collapse

Attributes inherited from BaseOperation

#client, #errors

Instance Method Summary collapse

Constructor Details

#initialize(bucket_name, file_key, row_data:, client: S3FileHandler.client) ⇒ AppendRow

Returns a new instance of AppendRow.



10
11
12
13
14
15
16
# File 'lib/s3_file_handler/csv/append_row.rb', line 10

def initialize(bucket_name, file_key, row_data:, client: S3FileHandler.client)
  super(client: client)

  @bucket_name = bucket_name
  @file_key = file_key
  @row_data = row_data
end

Instance Attribute Details

#bucket_nameObject (readonly)

Returns the value of attribute bucket_name.



8
9
10
# File 'lib/s3_file_handler/csv/append_row.rb', line 8

def bucket_name
  @bucket_name
end

#file_keyObject (readonly)

Returns the value of attribute file_key.



8
9
10
# File 'lib/s3_file_handler/csv/append_row.rb', line 8

def file_key
  @file_key
end

#row_dataObject (readonly)

Returns the value of attribute row_data.



8
9
10
# File 'lib/s3_file_handler/csv/append_row.rb', line 8

def row_data
  @row_data
end

Instance Method Details

#executeObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/s3_file_handler/csv/append_row.rb', line 18

def execute
  read_result = S3FileHandler.read_file(bucket_name, file_key)
  return read_result unless read_result.success?

  new_csv_content = ::CSV.generate do |csv|
    csv << ::CSV.parse(read_result.data[:content]).flatten if read_result.data[:content]
    
    csv << row_data
  end

  S3FileHandler.upload_file(bucket_name, file_key, content: new_csv_content)
end