Class: Shrimple::Phantom

Inherits:
Process show all
Defined in:
lib/shrimple/phantom.rb

Instance Attribute Summary collapse

Attributes inherited from Process

#start_time, #stop_time

Instance Method Summary collapse

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
  @options = options

  # write the file required by phantom's --config option
  if options[:config]
    @config = Tempfile.new(File.basename(options[:output] || 'shrimple') + '.config')
    @config.write(options[:config].to_json)
    @config.close
  end

  # create the ios to supply input and read output
  @stdin  = new_io(options[:stdin] || StringIO.new(options.to_json))
  @stdout = new_io(options[:output], 'wb')
  @stderr = new_io(options[:stderr], 'wt')

  if options[:debug]
    # hm, should this be replaced with methods?  or maybe a superclass?
    $stderr.puts "COMMAND: #{command_line}"
    $stderr.puts "STDIN: #{options.to_json}"
  end

  super(command_line, @stdin, @stdout, @stderr, options[:timeout])
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/shrimple/phantom.rb', line 11

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/shrimple/phantom.rb', line 11

def options
  @options
end

Instance Method Details

#_cleanupObject

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

#stderrObject



50
51
52
# File 'lib/shrimple/phantom.rb', line 50

def stderr
  read_io @stderr
end

#stdoutObject



46
47
48
# File 'lib/shrimple/phantom.rb', line 46

def stdout
  read_io @stdout
end

#waitObject

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