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
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 = @format = format @quality = quality @encoding = encoding @captured_at = Time.now end |
Instance Method Details
#base64 ⇒ String
50 51 52 |
# File 'lib/kleya/artifact.rb', line 50 def base64 @encoding == :base64 ? @data : Base64.encode64(@data) end |
#binary ⇒ String
55 56 57 |
# File 'lib/kleya/artifact.rb', line 55 def binary @encoding == :binary ? @data : Base64.decode64(@data) end |
#content_type ⇒ String
60 61 62 63 64 65 66 |
# File 'lib/kleya/artifact.rb', line 60 def content_type case @format when :jpeg, :jpg then 'image/jpeg' when :png then 'image/png' else "image/#{@format}" end end |
#dimensions ⇒ Hash
69 70 71 |
# File 'lib/kleya/artifact.rb', line 69 def dimensions .to_h end |
#filename(prefix: 'screenshot') ⇒ String
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}_#{timestamp}.#{extension}" end |
#inspect ⇒ String
74 75 76 |
# File 'lib/kleya/artifact.rb', line 74 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
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kleya/artifact.rb', line 37 def save(path = nil) 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
22 23 24 |
# File 'lib/kleya/artifact.rb', line 22 def size binary.bytesize end |