Class: WkhtmltopdfRunner::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/wkhtmltopdf_runner/cmd.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, file:, config:, options: {}) ⇒ Cmd

Returns a new instance of Cmd.



9
10
11
12
13
14
# File 'lib/wkhtmltopdf_runner/cmd.rb', line 9

def initialize(url:, file:, config:, options: {})
  @url = url
  @file = file
  @config = config
  @options = config.options.merge(options)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/wkhtmltopdf_runner/cmd.rb', line 7

def config
  @config
end

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/wkhtmltopdf_runner/cmd.rb', line 7

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/wkhtmltopdf_runner/cmd.rb', line 7

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/wkhtmltopdf_runner/cmd.rb', line 7

def url
  @url
end

Instance Method Details

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wkhtmltopdf_runner/cmd.rb', line 16

def run
  validate!
  debug_command!

  err, exit_status = Open3.popen3(*command) do |_stdin, _stdout, stderr, wait_thr|
    [stderr.read, wait_thr.value]
  end

  unless exit_status.success?
    raise WkhtmltopdfRunner::Error,
      "Failed to generate PDF. Status: #{exit_status.exitstatus} Error:\n#{err&.strip}"
  end

  true
end