Class: GoStatic

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

Constant Summary collapse

EXE_NAME =
"wkhtmltoimage"
@@config =
{}

Instance Method Summary collapse

Constructor Details

#initialize(wkhtmltoimage_binary_path = nil) ⇒ GoStatic

Returns a new instance of GoStatic.



19
20
21
22
23
24
# File 'lib/go_static.rb', line 19

def initialize(wkhtmltoimage_binary_path = nil)
  @exe_path = wkhtmltoimage_binary_path || find_wkhtmltoimage_binary_path
  raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
  raise "Bad #{EXE_NAME}'s path" unless File.exists?(@exe_path)
  raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
end

Instance Method Details

#png_from_string(string, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/go_static.rb', line 26

def png_from_string(string, options={})
  command = "\"#{@exe_path}\" #{parse_options(options)} - - " # -q for no errors on stdout
  print_command(command) if in_development_mode?
  png, err = Open3.popen3(command) do |stdin, stdout, stderr|
    stdin.binmode
    stdout.binmode
    stderr.binmode
    stdin.write(string)
    stdin.close
    [stdout.read, stderr.read]
  end
  raise "IMAGE could not be generated!" if png and png.rstrip.length == 0
  png
rescue Exception => e
  raise "Failed to execute:\n#{command}\nError: #{e}"
end