Class: Shrimple::Phantom
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Attributes inherited from Process
Instance Method Summary collapse
-
#_cleanup ⇒ Object
cleans up after the process.
-
#initialize(options) ⇒ Phantom
constructor
A new instance of Phantom.
- #stderr ⇒ Object
- #stdout ⇒ Object
-
#wait ⇒ Object
blocks until the PhantomJS process is finished.
Methods inherited from Process
#_child_thread, #_deactivate, #finished?, #kill, #killed?, #stop, #success?, #timed_out?
Constructor Details
#initialize(options) ⇒ Phantom
Returns a new instance of Phantom.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/shrimple/phantom.rb', line 13 def initialize @options = # write the file required by phantom's --config option if [:config] @config = Tempfile.new(File.basename([:output] || 'shrimple') + '.config') @config.write([:config].to_json) @config.close end # create the ios to supply input and read output @stdin = new_io([:stdin] || StringIO.new(.to_json)) @stdout = new_io([:output], 'wb') @stderr = new_io([:stderr], 'wt') if [:debug] # hm, should this be replaced with methods? or maybe a superclass? $stderr.puts "COMMAND: #{command_line}" $stderr.puts "STDIN: #{.to_json}" end super(command_line, @stdin, @stdout, @stderr, [:timeout]) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
11 12 13 |
# File 'lib/shrimple/phantom.rb', line 11 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/shrimple/phantom.rb', line 11 def @options end |
Instance Method Details
#_cleanup ⇒ Object
cleans up after the process. synchronized so it’s guaranteed to only be called once. process is removed from the process table after this call returns
57 58 59 60 61 62 63 64 |
# File 'lib/shrimple/phantom.rb', line 57 def _cleanup super proc = (success? ? @options[:onSuccess] : @options[:onError]) proc.call(self) if proc @config.unlink if @config end |
#stderr ⇒ Object
50 51 52 |
# File 'lib/shrimple/phantom.rb', line 50 def stderr read_io @stderr end |
#stdout ⇒ Object
46 47 48 |
# File 'lib/shrimple/phantom.rb', line 46 def stdout read_io @stdout end |
#wait ⇒ Object
blocks until the PhantomJS process is finished. raises an exception if it failed.
38 39 40 41 42 43 44 |
# File 'lib/shrimple/phantom.rb', line 38 def wait stop unless @child.value.success? raise Shrimple::TimedOut.new if timed_out? raise Shrimple::PhantomError.new("PhantomJS returned #{@child.value.exitstatus}: #{stderr}") end end |