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'

Instance Attribute Summary collapse

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.



10
11
12
13
14
15
16
17
# File 'lib/capybara-screenshot/s3_saver.rb', line 10

def initialize(saver, s3_client, bucket_name, object_configuration, options={})
  @saver = saver
  @s3_client = s3_client
  @bucket_name = bucket_name
  @bucket_host = options[:bucket_host]
  @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



61
62
63
64
65
66
# File 'lib/capybara-screenshot/s3_saver.rb', line 61

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

Instance Attribute Details

#html_pathObject

Returns the value of attribute html_path.



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

def html_path
  @html_path
end

#screenshot_pathObject

Returns the value of attribute screenshot_path.



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

def screenshot_path
  @screenshot_path
end

Class Method Details

.new_with_configuration(saver, configuration, object_configuration) ⇒ Object



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

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/capybara-screenshot/s3_saver.rb', line 36

def save_and_upload_screenshot
  save_and do |type, local_file_path|
    File.open(local_file_path) do |file|
      s3_upload_path = "#{@key_prefix}#{File.basename(local_file_path)}"

      object_payload = {
        bucket: bucket_name,
        key: s3_upload_path,
        body: file
      }

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

      s3_client.put_object(
          object_payload
      )

      host = bucket_host || determine_bucket_host

      send("#{type}_path=", "https://#{host}/#{s3_upload_path}")
    end
  end
end