Class: Shrimp::Phantom
- Inherits:
-
Object
- Object
- Shrimp::Phantom
- Defined in:
- lib/shrimp/phantom.rb
Constant Summary collapse
- SCRIPT_FILE =
File.('../rasterize.js', __FILE__)
Instance Attribute Summary collapse
-
#configuration ⇒ Object
Returns the value of attribute configuration.
-
#cookies ⇒ Object
readonly
Returns the value of attribute cookies.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#outfile ⇒ Object
Returns the value of attribute outfile.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
-
#cmd ⇒ Object
Public: Returns the phantom rasterize command.
-
#initialize(url_or_file, options = { }, cookies = { }, outfile = nil) ⇒ Phantom
constructor
Public: initializes a new Phantom Object.
-
#run ⇒ Object
Public: Runs the phantomjs binary.
- #run! ⇒ Object
-
#to_file(path = nil) ⇒ Object
Public: renders to pdf path - the destination path defaults to outfile.
- #to_file!(path = nil) ⇒ Object
-
#to_pdf(path = nil) ⇒ Object
Public: renders to pdf path - the destination path defaults to outfile.
- #to_pdf!(path = nil) ⇒ Object
-
#to_string(path = nil) ⇒ Object
Public: renders to pdf path - the destination path defaults to outfile.
- #to_string!(path = nil) ⇒ Object
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
95 96 97 98 99 100 101 |
# File 'lib/shrimp/phantom.rb', line 95 def initialize(url_or_file, = { }, ={ }, outfile = nil) @source = Source.new(url_or_file) @options = Shrimp.configuration..merge() @cookies = @outfile = File.(outfile) if outfile raise NoExecutableError.new unless File.exists?(Shrimp.configuration.phantomjs) end |
Instance Attribute Details
#configuration ⇒ Object
Returns the value of attribute configuration.
27 28 29 |
# File 'lib/shrimp/phantom.rb', line 27 def configuration @configuration end |
#cookies ⇒ Object (readonly)
Returns the value of attribute cookies.
28 29 30 |
# File 'lib/shrimp/phantom.rb', line 28 def @cookies end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
28 29 30 |
# File 'lib/shrimp/phantom.rb', line 28 def error @error end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/shrimp/phantom.rb', line 28 def @options end |
#outfile ⇒ Object
Returns the value of attribute outfile.
27 28 29 |
# File 'lib/shrimp/phantom.rb', line 27 def outfile @outfile end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
28 29 30 |
# File 'lib/shrimp/phantom.rb', line 28 def result @result end |
#source ⇒ Object
Returns the value of attribute source.
27 28 29 |
# File 'lib/shrimp/phantom.rb', line 27 def source @source end |
Instance Method Details
#cmd ⇒ Object
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 = format, zoom, margin, orientation = [:format], [:zoom], [:margin], [:orientation] rendering_time, timeout = [:rendering_time], [:rendering_timeout] , = [:viewport_width], [:viewport_height] max_redirect_count = [:max_redirect_count] @outfile ||= "#{[:tmpdir]}/#{Digest::MD5.hexdigest((Time.now.to_i + rand(9001)).to_s)}.pdf" command_config_file = "--config=#{[:command_config_file]}" [ Shrimp.configuration.phantomjs, command_config_file, SCRIPT_FILE, @source.to_s.shellescape, @outfile, format, zoom, margin, orientation, , rendering_time, timeout, , , max_redirect_count ].join(" ") end |
#run ⇒ Object
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.(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.(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 |