Class: Kleya::Artifact

Inherits:
Object
  • Object
show all
Defined in:
lib/kleya/artifact.rb

Instance Method Summary collapse

Constructor Details

#initialize(data:, url:, viewport:, format:, encoding:, quality: nil) ⇒ Artifact

Returns a new instance of Artifact.

Parameters:

  • data (String)

    the screenshot data (base64 or binary)

  • url (String)

    the URL that was captured

  • viewport (Viewport)

    the viewport used

  • format (Symbol)

    the image format (:jpeg, :png)

  • quality (Integer) (defaults to: nil)

    the quality (1-100)

  • encoding (Symbol)

    the data encoding (:base64, :binary)



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 = viewport
  @format = format
  @quality = quality
  @encoding = encoding
  @captured_at = Time.now
end

Instance Method Details

#base64String

Returns the base64-encoded data of the artifact.

Returns:

  • (String)

    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

#binaryString

Returns the binary data of the artifact.

Returns:

  • (String)

    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_typeString

Returns the content type of the artifact.

Returns:

  • (String)

    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

#dimensionsHash

Returns the dimensions of the artifact.

Returns:

  • (Hash)

    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.

Parameters:

  • prefix (String) (defaults to: 'screenshot')

    optional prefix for the filename

Returns:

  • (String)

    a generated filename for the artifact



28
29
30
31
32
33
# File 'lib/kleya/artifact.rb', line 28

def filename(prefix: 'screenshot')
  timestamp = @captured_at.strftime('%Y%m%d_%H%M%S')
  extension = @format == :jpeg ? 'jpg' : @format.to_s

  "#{prefix}_#{timestamp}.#{extension}"
end

#inspectString

Returns the inspection of the artifact.

Returns:

  • (String)

    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.

Parameters:

  • path (String) (defaults to: nil)

    the path to save the artifact

Returns:

  • (String)

    the full path where the file was saved

Raises:

  • (ArgumentError)


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

#sizeInteger

Returns the size of the artifact.

Returns:

  • (Integer)

    the size of the artifact



22
23
24
# File 'lib/kleya/artifact.rb', line 22

def size
  binary.bytesize
end