Class: DefraRubyMocks::AwsBucketService

Inherits:
Object
  • Object
show all
Defined in:
app/services/defra_ruby_mocks/aws_bucket_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bucket_nameObject

Returns the value of attribute bucket_name.



9
10
11
# File 'app/services/defra_ruby_mocks/aws_bucket_service.rb', line 9

def bucket_name
  @bucket_name
end

#file_nameObject

Returns the value of attribute file_name.



9
10
11
# File 'app/services/defra_ruby_mocks/aws_bucket_service.rb', line 9

def file_name
  @file_name
end

Class Method Details

.read(bucket_name, file_name) ⇒ Object



15
16
17
# File 'app/services/defra_ruby_mocks/aws_bucket_service.rb', line 15

def self.read(bucket_name, file_name)
  new.read(bucket_name, file_name)
end

.write(bucket_name, file_name, content) ⇒ Object



11
12
13
# File 'app/services/defra_ruby_mocks/aws_bucket_service.rb', line 11

def self.write(bucket_name, file_name, content)
  new.write(bucket_name, file_name, content)
end

Instance Method Details

#read(bucket_name, file_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/services/defra_ruby_mocks/aws_bucket_service.rb', line 31

def read(bucket_name, file_name)
  @bucket_name = bucket_name
  @file_name = file_name
  Rails.logger.debug ":::::: mocks reading from S3"

  s3 = Aws::S3::Client.new(
    region: ENV.fetch("AWS_REGION", nil),
    credentials: Aws::Credentials.new(
      ENV.fetch("AWS_DEFRA_RUBY_MOCKS_ACCESS_KEY_ID", nil),
      ENV.fetch("AWS_DEFRA_RUBY_MOCKS_SECRET_ACCESS_KEY", nil)
    )
  )
  s3.get_object(bucket: bucket_name, key: file_name).body.read
end

#write(bucket_name, file_name, content) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/defra_ruby_mocks/aws_bucket_service.rb', line 19

def write(bucket_name, file_name, content)
  @bucket_name = bucket_name
  @file_name = file_name
  Rails.logger.debug ":::::: mocks writing to S3"

  write_temp_file(content)

  load_temp_file_to_s3

  true
end