Class: DragonflyPuppeteer::Generators::Screenshot

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly_puppeteer/generators/screenshot.rb

Defined Under Namespace

Classes: UnsupportedFormat

Instance Method Summary collapse

Instance Method Details

#call(content, source, opts = {}) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dragonfly_puppeteer/generators/screenshot.rb', line 9

def call(content, source, opts = {})
  format = extract_format(opts)
  raise UnsupportedFormat unless %w[jpg png].include?(format.to_s)

  viewport_opts = extract_viewport_opts(opts)
  screenshot_opts = extract_screenshot_opts(opts)
  screenshot_opts[:type] = (format == 'jpg' ? 'jpeg' : format)
  goto_opts = extract_goto_opts(opts)
  http_headers = extract_http_headers(opts)
  delay = extract_delay(opts)

  file_name = extract_file_name(opts)

  node_command = content.env.fetch(:node_command, 'node')

  content.shell_generate(ext: format) do |path|
    screenshot_opts[:path] = path
    "#{node_command} #{script} #{Shellwords.escape(source)} '#{viewport_opts.to_json}' '#{screenshot_opts.to_json}' '#{goto_opts.to_json}' '#{http_headers.to_json}' #{delay}"
  end
  content.add_meta('format' => format, 'name' => "#{file_name}.#{format}")
end

#update_url(url_attributes, source, opts = {}) ⇒ Object



31
32
33
34
# File 'lib/dragonfly_puppeteer/generators/screenshot.rb', line 31

def update_url(url_attributes, source, opts = {})
  file_name = extract_file_name(opts)
  url_attributes.name = "#{file_name}.#{extract_format(opts)}"
end