Class: Capybara::Screenshot::S3Saver

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara-screenshot/s3_saver.rb

Constant Summary collapse

DEFAULT_REGION =
'us-east-1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(saver, s3_client, bucket_name) ⇒ S3Saver

Returns a new instance of S3Saver.



8
9
10
11
12
# File 'lib/capybara-screenshot/s3_saver.rb', line 8

def initialize(saver, s3_client, bucket_name)
  @saver = saver
  @s3_client = s3_client
  @bucket_name = bucket_name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



44
45
46
47
48
49
# File 'lib/capybara-screenshot/s3_saver.rb', line 44

def method_missing(method, *args)
  # Need to use @saver instead of S3Saver#saver attr_reader method because
  # using the method goes into infinite loop. Maybe attr_reader implements
  # its methods via method_missing?
  @saver.send(method, *args)
end

Class Method Details

.new_with_configuration(saver, configuration) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/capybara-screenshot/s3_saver.rb', line 14

def self.new_with_configuration(saver, configuration)
  default_s3_client_credentials = {
    region: DEFAULT_REGION
  }

  s3_client_credentials = default_s3_client_credentials.merge(
    configuration.fetch(:s3_client_credentials)
  )

  s3_client = Aws::S3::Client.new(s3_client_credentials)
  bucket_name = configuration.fetch(:bucket_name)

  new(saver, s3_client, bucket_name)
rescue KeyError
  raise "Invalid S3 Configuration #{configuration}. Please refer to the documentation for the necessary configurations."
end

Instance Method Details

#save_and_upload_screenshotObject Also known as: save



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/capybara-screenshot/s3_saver.rb', line 31

def save_and_upload_screenshot
  save_and do |local_file_path|
    File.open(local_file_path) do |file|
      s3_client.put_object(
          bucket: bucket_name,
          key: File.basename(local_file_path),
          body: file
      )
    end
  end
end