Class: MagLove::PhantomScript
- Inherits:
-
Object
- Object
- MagLove::PhantomScript
- Defined in:
- lib/maglove/phantom_script.rb
Instance Attribute Summary collapse
-
#log_file ⇒ Object
Returns the value of attribute log_file.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#running ⇒ Object
Returns the value of attribute running.
-
#script ⇒ Object
Returns the value of attribute script.
Instance Method Summary collapse
-
#initialize(script, options = {}) ⇒ PhantomScript
constructor
A new instance of PhantomScript.
- #run(*args) ⇒ Object
Constructor Details
#initialize(script, options = {}) ⇒ PhantomScript
Returns a new instance of PhantomScript.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/maglove/phantom_script.rb', line 7 def initialize(script, = {}) @running = false @script = script = .merge({ script_path: Gem.datadir("maglove") }) @path = File.absolute_path(File.join([:script_path], "#{@script}.js")) throw "Script #{script} not found at #{@path}" unless File.exist?(@path) @log_file = Tempfile.new('pslog') end |
Instance Attribute Details
#log_file ⇒ Object
Returns the value of attribute log_file.
5 6 7 |
# File 'lib/maglove/phantom_script.rb', line 5 def log_file @log_file end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/maglove/phantom_script.rb', line 5 def end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/maglove/phantom_script.rb', line 5 def path @path end |
#running ⇒ Object
Returns the value of attribute running.
5 6 7 |
# File 'lib/maglove/phantom_script.rb', line 5 def running @running end |
#script ⇒ Object
Returns the value of attribute script.
5 6 7 |
# File 'lib/maglove/phantom_script.rb', line 5 def script @script end |
Instance Method Details
#run(*args) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/maglove/phantom_script.rb', line 18 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 |