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, object_configuration, options = {}) ⇒ S3Saver

Returns a new instance of S3Saver.



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

def initialize(saver, s3_client, bucket_name, object_configuration, options={})
  @saver = saver
  @s3_client = s3_client
  @bucket_name = bucket_name
  @key_prefix = options[:key_prefix]
  @object_configuration = object_configuration
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



52
53
54
55
56
57
# File 'lib/capybara-screenshot/s3_saver.rb', line 52

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_configuration) ⇒ Object



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

def self.new_with_configuration(saver, configuration, object_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, object_configuration, configuration)
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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/capybara-screenshot/s3_saver.rb', line 33

def save_and_upload_screenshot
  save_and do |local_file_path|
    File.open(local_file_path) do |file|
      object_payload = {
        bucket: bucket_name,
        key: "#{@key_prefix}#{File.basename(local_file_path)}",
        body: file
      }

      object_payload.merge!(object_configuration) unless object_configuration.empty?

      s3_client.put_object(
          object_payload
      )
    end
  end
end