Class: Shrimp::Phantom

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

Constant Summary collapse

SCRIPT_FILE =
File.expand_path('../rasterize.js', __FILE__)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_or_file, options = { }, cookies = { }, outfile = nil) ⇒ Phantom

Public: initializes a new Phantom Object

url_or_file - The url of the html document to render options - a hash with options for rendering

* format              - the paper format for the output eg: "5in*7.5in", "10cm*20cm", "A4", "Letter"
* zoom                - the viewport zoom factor
* margin              - the margins for the pdf
* command_config_file - the path to a json configuration file for command-line options

cookies - hash with cookies to use for rendering outfile - optional path for the output file a Tempfile will be created if not given

Returns self

Raises:



95
96
97
98
99
100
101
# File 'lib/shrimp/phantom.rb', line 95

def initialize(url_or_file, options = { }, cookies={ }, outfile = nil)
  @source  = Source.new(url_or_file)
  @options = Shrimp.configuration.default_options.merge(options)
  @cookies = cookies
  @outfile = File.expand_path(outfile) if outfile
  raise NoExecutableError.new unless File.exists?(Shrimp.configuration.phantomjs)
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



27
28
29
# File 'lib/shrimp/phantom.rb', line 27

def configuration
  @configuration
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



28
29
30
# File 'lib/shrimp/phantom.rb', line 28

def cookies
  @cookies
end

#errorObject (readonly)

Returns the value of attribute error.



28
29
30
# File 'lib/shrimp/phantom.rb', line 28

def error
  @error
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/shrimp/phantom.rb', line 28

def options
  @options
end

#outfileObject

Returns the value of attribute outfile.



27
28
29
# File 'lib/shrimp/phantom.rb', line 27

def outfile
  @outfile
end

#resultObject (readonly)

Returns the value of attribute result.



28
29
30
# File 'lib/shrimp/phantom.rb', line 28

def result
  @result
end

#sourceObject

Returns the value of attribute source.



27
28
29
# File 'lib/shrimp/phantom.rb', line 27

def source
  @source
end

Instance Method Details

#cmdObject

Public: Returns the phantom rasterize command



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shrimp/phantom.rb', line 56

def cmd
  cookie_file                       = dump_cookies
  format, zoom, margin, orientation = options[:format], options[:zoom], options[:margin], options[:orientation]
  rendering_time, timeout           = options[:rendering_time], options[:rendering_timeout]
  viewport_width, viewport_height   = options[:viewport_width], options[:viewport_height]
  max_redirect_count                = options[:max_redirect_count]
  @outfile                          ||= "#{options[:tmpdir]}/#{Digest::MD5.hexdigest((Time.now.to_i + rand(9001)).to_s)}.pdf"
  command_config_file               = "--config=#{options[:command_config_file]}"
  [
    Shrimp.configuration.phantomjs,
    command_config_file,
    SCRIPT_FILE,
    @source.to_s.shellescape,
    @outfile,
    format,
    zoom,
    margin,
    orientation,
    cookie_file,
    rendering_time,
    timeout,
    viewport_width,
    viewport_height,
    max_redirect_count
  ].join(" ")
end

#runObject

Public: Runs the phantomjs binary

Returns the stdout output of phantomjs



34
35
36
37
38
39
40
41
42
# File 'lib/shrimp/phantom.rb', line 34

def run
  @error  = nil
  @result = `#{cmd}`
  unless $?.exitstatus == 0
    @error  = @result
    @result = nil
  end
  @result
end

#run!Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/shrimp/phantom.rb', line 44

def run!
  @error  = nil
  @result = `#{cmd}`
  unless $?.exitstatus == 0
    @error  = @result
    @result = nil
    raise RenderingError.new(@error)
  end
  @result
end

#to_file(path = nil) ⇒ Object

Public: renders to pdf path - the destination path defaults to outfile

Returns a File Handle of the Resulting pdf



117
118
119
120
# File 'lib/shrimp/phantom.rb', line 117

def to_file(path=nil)
  self.to_pdf(path)
  File.new(@outfile)
end

#to_file!(path = nil) ⇒ Object



136
137
138
139
# File 'lib/shrimp/phantom.rb', line 136

def to_file!(path=nil)
  self.to_pdf!(path)
  File.new(@outfile)
end

#to_pdf(path = nil) ⇒ Object

Public: renders to pdf path - the destination path defaults to outfile

Returns the path to the pdf file



107
108
109
110
111
# File 'lib/shrimp/phantom.rb', line 107

def to_pdf(path=nil)
  @outfile = File.expand_path(path) if path
  self.run
  @outfile
end

#to_pdf!(path = nil) ⇒ Object



130
131
132
133
134
# File 'lib/shrimp/phantom.rb', line 130

def to_pdf!(path=nil)
  @outfile = File.expand_path(path) if path
  self.run!
  @outfile
end

#to_string(path = nil) ⇒ Object

Public: renders to pdf path - the destination path defaults to outfile

Returns the binary string of the pdf



126
127
128
# File 'lib/shrimp/phantom.rb', line 126

def to_string(path=nil)
  File.open(self.to_pdf(path)).read
end

#to_string!(path = nil) ⇒ Object



141
142
143
# File 'lib/shrimp/phantom.rb', line 141

def to_string!(path=nil)
  File.open(self.to_pdf!(path)).read
end