Class: Kleya::Artifact
- Inherits:
-
Object
- Object
- Kleya::Artifact
- Defined in:
- lib/kleya/artifact.rb
Instance Method Summary collapse
-
#base64 ⇒ String
The base64-encoded data of the artifact.
-
#binary ⇒ String
The binary data of the artifact.
-
#content_type ⇒ String
The content type of the artifact.
-
#dimensions ⇒ Hash
The dimensions of the artifact.
-
#filename(prefix: 'screenshot') ⇒ String
A generated filename for the artifact.
-
#initialize(data:, url:, viewport:, format:, encoding:, quality: nil) ⇒ Artifact
constructor
A new instance of Artifact.
-
#inspect ⇒ String
The inspection of the artifact.
-
#save(path = nil) ⇒ String
The full path where the file was saved.
-
#size ⇒ Integer
The size of the artifact.
Constructor Details
#initialize(data:, url:, viewport:, format:, encoding:, quality: nil) ⇒ Artifact
Returns a new instance of Artifact.
11 12 13 14 15 16 17 18 19 |
# File 'lib/kleya/artifact.rb', line 11 def initialize(data:, url:, viewport:, format:, encoding:, quality: nil) @data = data @url = url @viewport = @format = format @quality = quality @encoding = encoding @captured_at = Time.now end |
Instance Method Details
#base64 ⇒ String
Returns the base64-encoded data of the artifact.
52 53 54 |
# File 'lib/kleya/artifact.rb', line 52 def base64 @encoding == :base64 ? @data : Base64.encode64(@data) end |
#binary ⇒ String
Returns the binary data of the artifact.
57 58 59 |
# File 'lib/kleya/artifact.rb', line 57 def binary @encoding == :binary ? @data : Base64.decode64(@data) end |
#content_type ⇒ String
Returns the content type of the artifact.
62 63 64 65 66 67 68 |
# File 'lib/kleya/artifact.rb', line 62 def content_type case @format when :jpeg, :jpg then 'image/jpeg' when :png then 'image/png' else "image/#{@format}" end end |
#dimensions ⇒ Hash
Returns the dimensions of the artifact.
71 72 73 |
# File 'lib/kleya/artifact.rb', line 71 def dimensions @viewport.to_h end |
#filename(prefix: 'screenshot') ⇒ String
Returns a generated filename for the artifact.
28 29 30 31 32 33 |
# File 'lib/kleya/artifact.rb', line 28 def filename(prefix: 'screenshot') = @captured_at.strftime('%Y%m%d_%H%M%S') extension = @format == :jpeg ? 'jpg' : @format.to_s "#{prefix}_#{}.#{extension}" end |
#inspect ⇒ String
Returns the inspection of the artifact.
76 77 78 |
# File 'lib/kleya/artifact.rb', line 76 def inspect "#<#{self.class.name} @url=#{@url} @viewport=#{@viewport.inspect} @format=#{@format} @quality=#{@quality} @encoding=#{@encoding} @captured_at=#{@captured_at}>" end |
#save(path = nil) ⇒ String
Returns the full path where the file was saved.
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kleya/artifact.rb', line 37 def save(path = nil) raise ArgumentError, 'path cannot contain parent directory traversal' if path.to_s.split(/[\\\/]/).include?('..') if path.nil? path = filename elsif File.directory?(path) path = File.join(path, filename) elsif File.extname(path).empty? path = "#{path}.#{@format}" end File.write(path, binary, mode: 'wb').then { path } end |
#size ⇒ Integer
Returns the size of the artifact.
22 23 24 |
# File 'lib/kleya/artifact.rb', line 22 def size binary.bytesize end |