Class: Sidekiq::Form526HistoricalDataExporter::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/form526_historical_data_exporter/exporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(batch_size, start_id, end_id, data = []) ⇒ Exporter

Returns a new instance of Exporter.



6
7
8
9
10
11
12
13
# File 'lib/sidekiq/form526_historical_data_exporter/exporter.rb', line 6

def initialize(batch_size, start_id, end_id, data = [])
  @batch_size = batch_size.to_i
  @start_id = start_id
  @end_id = end_id
  @data = data
  @file_name = "#{start_id}_#{end_id}.json"
  @file_path_and_name = "tmp/#{@file_name}"
end

Instance Method Details

#get_submission_stats(submission) ⇒ Object



37
38
39
# File 'lib/sidekiq/form526_historical_data_exporter/exporter.rb', line 37

def get_submission_stats(submission)
  [submission.id, submission., submission.created_at, submission.form]
end

#new_s3_resourceObject



29
30
31
32
33
34
35
# File 'lib/sidekiq/form526_historical_data_exporter/exporter.rb', line 29

def new_s3_resource
  Aws::S3::Resource.new(
    region: Settings.form526_export.aws.region,
    access_key_id: Settings.form526_export.aws.access_key_id,
    secret_access_key: Settings.form526_export.aws.secret_access_key
  )
end

#process!Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sidekiq/form526_historical_data_exporter/exporter.rb', line 41

def process!
  all = []
  batches = Form526Submission.select(:id, :created_at, :encrypted_kms_key, :form_json_ciphertext,
                                     :submitted_claim_id).find_in_batches(batch_size: @batch_size,
                                                                          start: @start_id, finish: @end_id)
  batches.each do |batch|
    batch.each do |submission|
      all << get_submission_stats(submission)
    end
  end
  write_to_file(all.to_json)
  upload_to_s3!
ensure
  Common::FileHelpers.delete_file_if_exists(@file_path_and_name)
end

#s3_bucketObject



25
26
27
# File 'lib/sidekiq/form526_historical_data_exporter/exporter.rb', line 25

def s3_bucket
  Settings.form526_export.aws.bucket
end

#upload_to_s3!Object



19
20
21
22
23
# File 'lib/sidekiq/form526_historical_data_exporter/exporter.rb', line 19

def upload_to_s3!
  s3_resource = new_s3_resource
  obj = s3_resource.bucket(s3_bucket).object(@file_name)
  obj.upload_file(@file_path_and_name, content_type: 'application/json')
end

#write_to_file(content) ⇒ Object



15
16
17
# File 'lib/sidekiq/form526_historical_data_exporter/exporter.rb', line 15

def write_to_file(content)
  File.write(@file_path_and_name, content)
end