class Phantomrb::Runner

Public Class Methods

new() click to toggle source
# File lib/phantomrb/runner.rb, line 6
def initialize
  @config = Phantomrb.configuration
end

Public Instance Methods

run(script, *args, &block) click to toggle source
# File lib/phantomrb/runner.rb, line 10
def run(script, *args, &block)
  options   = args.last.is_a?(Hash) ? args.pop : {}
  command   = @config.merge(options)
  sargs     = args.map {|a| Shellwords.escape(a) }

  command_line = [
    command,
    full_script_path(script),
    *sargs
  ].join(' ')

  begin
    process = IO.popen(command_line)
  rescue => e
    raise ExecutableLoadError.new(e)
  end

  output = capture_output(process, &block)
  process.close

  unless $?.exitstatus == 0
    raise ScriptRuntimeError.new(output)
  end

  OpenStruct.new(
    output: output,
    exit_status: $?.exitstatus,
    command_line: command_line
  )
end

Private Instance Methods

capture_output(process) { |output_line| ... } click to toggle source
# File lib/phantomrb/runner.rb, line 52
def capture_output(process)
  if block_given?
    output = ''
    process.each_line do |output_line|
      output << output_line
      yield(output_line)
    end
    output
  else
    process.read
  end
end
full_script_path(script) click to toggle source
# File lib/phantomrb/runner.rb, line 43
def full_script_path(script)
  full_script_path = File.expand_path(script)
  if File.file?(full_script_path)
    full_script_path
  else
    raise ScriptLoadError.new("#{full_script_path} not found")
  end
end