Class: Phrender::PhantomJSEngine
- Inherits:
-
Object
- Object
- Phrender::PhantomJSEngine
- Defined in:
- lib/phrender/phantom_js_engine.rb
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ PhantomJSEngine
constructor
A new instance of PhantomJSEngine.
- #render(html, javascript, url = nil) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ PhantomJSEngine
Returns a new instance of PhantomJSEngine.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/phrender/phantom_js_engine.rb', line 10 def initialize(opts = {}) # Apply defaults opts = { :timeout => 10000, :ssl => false }.merge opts @poll_interval = 0.1 @timeout = opts[:timeout] / 1000.0 @ssl_protocol = opts.delete :ssl @logger = Phrender::Logger phantom_program = File. '../support/phantom_bridge.js', __FILE__ MultiJson.use :json_gem @boot_cmd = [ 'phantomjs', phantom_program, "--ignore-ssl-errors=true", "--load-images=false" ] @boot_cmd.push "--ssl-protocol=%s" % [ @ssl_protocol ] if @ssl_protocol end |
Instance Method Details
#render(html, javascript, url = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/phrender/phantom_js_engine.rb', line 32 def render(html, javascript, url = nil) javascript_file = make_temp_file(javascript, 'file.js') html_file = make_temp_file(html, 'file.html') = { :html => html_file.path, :javascript => javascript_file.path, :url => url, :timeout => @timeout * 1000.0 } session = Phrender::PhantomJSSession.new @boot_cmd.join(' '), @timeout session.stdin.puts MultiJson.dump() session.stdin.close begin sleep @poll_interval parse_output(session) end while !session.expired? && !session.rendered # Clean up phantom session.shutdown # Clean up temp files javascript_file.unlink html_file.unlink # Feed something out the chain if session.rendered session.page elsif session.expired? @logger.critical "PhantomJS timed out. Likely a javascript execution error." '' else @logger.critical "Phantom terminated without expiring or returning anything. This is bad." '' end end |