Class: Capybara::Screenshot::S3Saver
- Inherits:
-
Object
- Object
- Capybara::Screenshot::S3Saver
- Defined in:
- lib/capybara-screenshot/s3_saver.rb
Constant Summary collapse
- DEFAULT_REGION =
'us-east-1'
Instance Attribute Summary collapse
-
#html_path ⇒ Object
Returns the value of attribute html_path.
-
#screenshot_path ⇒ Object
Returns the value of attribute screenshot_path.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(saver, s3_client, bucket_name, object_configuration, options = {}) ⇒ S3Saver
constructor
A new instance of S3Saver.
- #method_missing(method, *args) ⇒ Object
- #save_and_upload_screenshot ⇒ Object (also: #save)
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, ={}) @saver = saver @s3_client = s3_client @bucket_name = bucket_name @bucket_host = [:bucket_host] @key_prefix = [: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_path ⇒ Object
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_path ⇒ Object
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_screenshot ⇒ Object 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 |