Sha256: 80c68788483ab92a6a3af5d63c076885c9a79c5770ff1aa84099a82e15e3e175

Contents?: true

Size: 887 Bytes

Versions: 1

Compression:

Stored size: 887 Bytes

Contents

require 'tempfile'
require "phantomjs/version"
require 'phantomjs/errors'

class Phantomjs
  EXEC = 'phantomjs'

  def self.run(path, *args)
    epath = File.expand_path(path)
    raise NoSuchPathError.new(epath) unless File.exist?(epath)
    block = block_given? ? Proc.new : nil
    execute(epath, args, block)
  end

  def self.inline(script, *args)
    file = Tempfile.new('script.js')
    file.write(script)
    file.close
    block = block_given? ? Proc.new : nil
    execute(file.path, args, block)
  end

  private

  def self.execute(path, arguments, block)
    begin
      if block
        IO.popen([EXEC, path, arguments].flatten).each_line do |line|
          block.call(line)
        end
      else
        IO.popen([EXEC, path, arguments].flatten).read
      end
    rescue Errno::ENOENT
      raise CommandNotFoundError.new('Phantomjs is not installed')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phantomjs.rb-2.0.0 lib/phantomjs.rb