Sha256: 82e1bbcdc72505e774fb4ce3d96bba98c6cd58e88a37acb72dd90f8dbb64e9c6

Contents?: true

Size: 1.18 KB

Versions: 21

Compression:

Stored size: 1.18 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}" if not File.exists?(@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{
        f = File.open(@log_file.path, "r")
        f.seek(0, IO::SEEK_END)
        puts "phantom@0ms ▸ (start)"
        while @running do
          select([f])
          line = f.gets
          puts "phantom@#{((Time.now - start) * 1000.0).to_i}ms ▸ #{line}" if line
        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

21 entries across 21 versions & 1 rubygems

Version Path
maglove-0.8.1 lib/maglove/phantom_script.rb
maglove-0.8.0 lib/maglove/phantom_script.rb
maglove-0.7.2 lib/maglove/phantom_script.rb
maglove-0.7.1 lib/maglove/phantom_script.rb
maglove-0.7.0 lib/maglove/phantom_script.rb
maglove-0.6.6 lib/maglove/phantom_script.rb
maglove-0.6.5 lib/maglove/phantom_script.rb
maglove-0.6.4 lib/maglove/phantom_script.rb
maglove-0.6.3 lib/maglove/phantom_script.rb
maglove-0.6.2 lib/maglove/phantom_script.rb
maglove-0.6.1 lib/maglove/phantom_script.rb
maglove-0.6.0 lib/maglove/phantom_script.rb
maglove-0.5.9 lib/maglove/phantom_script.rb
maglove-0.5.8 lib/maglove/phantom_script.rb
maglove-0.5.7 lib/maglove/phantom_script.rb
maglove-0.5.5 lib/maglove/phantom_script.rb
maglove-0.5.4 lib/maglove/phantom_script.rb
maglove-0.5.2 lib/maglove/phantom_script.rb
maglove-0.5.1 lib/maglove/phantom_script.rb
maglove-0.5.0 lib/maglove/phantom_script.rb