Sha256: df63911e1c773b2262a5c756ced281ce1878db60709cdd6cb49b144fcc0feb7f

Contents?: true

Size: 1.17 KB

Versions: 17

Compression:

Stored size: 1.17 KB

Contents

require 'tempfile'

module MagLove
  class PhantomScript
    attr_accessor :running, :script, :options, :path, :log_file

    def initialize(script, options = {})
      @running = false
      @script = script
      @options = options.merge({
        script_path: Gem.datadir("maglove")
      })
      @path = File.absolute_path(File.join(@options[:script_path], "#{@script}.js"))
      throw "Script #{script} not found at #{@path}" unless File.exist?(@path)
      @log_file = Tempfile.new('pslog')
    end

    def run(*args)
      start = Time.now
      @running = true
      cmd = "phantomjs #{@path} #{@log_file.path} #{args.join(' ')}"

      # log phantomjs info
      @log_thread = Thread.new do
        f = File.open(@log_file.path, "r")
        f.seek(0, IO::SEEK_END)
        puts "phantom@0ms ▸ (start)"
        while @running
          select([f])
          line = f.gets
          puts "phantom@#{((Time.now - start) * 1000.0).to_i}ms ▸ #{line}" if line
        end
      end

      # run command and return result
      result = `#{cmd}`
      @running = false
      result == "ERROR" ? false : result
    ensure
      @log_file.close
      @log_file.unlink
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
maglove-1.1.5 lib/maglove/phantom_script.rb
maglove-1.1.4 lib/maglove/phantom_script.rb
maglove-1.1.3 lib/maglove/phantom_script.rb
maglove-2.0.4 lib/maglove/phantom_script.rb
maglove-2.0.2 lib/maglove/phantom_script.rb
maglove-2.0.1 lib/maglove/phantom_script.rb
maglove-1.1.2 lib/maglove/phantom_script.rb
maglove-2.0.0 lib/maglove/phantom_script.rb
maglove-1.1.1 lib/maglove/phantom_script.rb
maglove-1.1.0 lib/maglove/phantom_script.rb
maglove-1.0.9 lib/maglove/phantom_script.rb
maglove-1.0.8 lib/maglove/phantom_script.rb
maglove-1.0.7 lib/maglove/phantom_script.rb
maglove-1.0.5 lib/maglove/phantom_script.rb
maglove-1.0.4 lib/maglove/phantom_script.rb
maglove-1.0.3 lib/maglove/phantom_script.rb
maglove-1.0.2 lib/maglove/phantom_script.rb