Class: Puppeteer::Page::ScreenshotOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/page/screenshot_options.rb

Overview

/**

* @typedef {Object} ScreenshotOptions
* @property {string=} type
* @property {string=} path
* @property {boolean=} fullPage
* @property {{x: number, y: number, width: number, height: number}=} clip
* @property {number=} quality
* @property {boolean=} omitBackground
* @property {string=} encoding
*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ScreenshotOptions



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/puppeteer/page/screenshot_options.rb', line 16

def initialize(options)
  if options[:type]
    unless [:png, :jpeg].include?(options[:type].to_sym)
      raise ArgumentError.new("Unknown options.type value: #{options[:type]}")
    end
    @type = options[:type]
  elsif options[:path]
    mime_types = MIME::Types.type_for(options[:path])
    if mime_types.include?('image/png')
      @type = 'png'
    elsif mime_types.include?('image/jpeg')
      @type = 'jpeg'
    else
      raise ArgumentError.new("Unsupported screenshot mime type resolved: #{mime_types}, path: #{options[:path]}")
    end
  end
  @type ||= 'png'

  if options[:quality]
    unless @type == 'jpeg'
      raise ArgumentError.new("options.quality is unsupported for the #{@type} screenshots")
    end
    unless options[:quality].is_a?(Numeric)
      raise ArgumentError.new("Expected options.quality to be a number but found #{options[:quality].class}")
    end
    quality = options[:quality].to_i
    unless (0..100).include?(quality)
      raise ArgumentError.new("Expected options.quality to be between 0 and 100 (inclusive), got #{quality}")
    end
    @quality = quality
  end

  if options[:clip] && options[:full_page]
    raise ArgumentError.new('options.clip and options.fullPage are exclusive')
  end

  # if (options.clip) {
  #   assert(typeof options.clip.x === 'number', 'Expected options.clip.x to be a number but found ' + (typeof options.clip.x));
  #   assert(typeof options.clip.y === 'number', 'Expected options.clip.y to be a number but found ' + (typeof options.clip.y));
  #   assert(typeof options.clip.width === 'number', 'Expected options.clip.width to be a number but found ' + (typeof options.clip.width));
  #   assert(typeof options.clip.height === 'number', 'Expected options.clip.height to be a number but found ' + (typeof options.clip.height));
  #   assert(options.clip.width !== 0, 'Expected options.clip.width not to be 0.');
  #   assert(options.clip.height !== 0, 'Expected options.clip.height not to be 0.');
  # }

  @path = options[:path]
  @full_page = options[:full_page]
  @clip = options[:clip]
  @omit_background = options[:omit_background]
  @encoding = options[:encoding]
end

Instance Attribute Details

#clipObject (readonly)

Returns the value of attribute clip.



68
69
70
# File 'lib/puppeteer/page/screenshot_options.rb', line 68

def clip
  @clip
end

#encodingObject (readonly)

Returns the value of attribute encoding.



68
69
70
# File 'lib/puppeteer/page/screenshot_options.rb', line 68

def encoding
  @encoding
end

#pathObject (readonly)

Returns the value of attribute path.



68
69
70
# File 'lib/puppeteer/page/screenshot_options.rb', line 68

def path
  @path
end

#qualityObject (readonly)

Returns the value of attribute quality.



68
69
70
# File 'lib/puppeteer/page/screenshot_options.rb', line 68

def quality
  @quality
end

#typeObject (readonly)

Returns the value of attribute type.



68
69
70
# File 'lib/puppeteer/page/screenshot_options.rb', line 68

def type
  @type
end

Instance Method Details

#full_page?Boolean



70
71
72
# File 'lib/puppeteer/page/screenshot_options.rb', line 70

def full_page?
  @full_page
end

#omit_background?Boolean



74
75
76
# File 'lib/puppeteer/page/screenshot_options.rb', line 74

def omit_background?
  @omit_background
end