Skip to content

Commit

Permalink
Avoid conflict with Phantomjs class. Closes #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
vikhyat committed Nov 6, 2014
1 parent 09710b1 commit 9557c5f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
36 changes: 19 additions & 17 deletions lib/indexable/phantomjs.rb
@@ -1,26 +1,28 @@
require 'timeout'

class Phantomjs
attr_accessor :timeout
module Indexable
class Phantomjs
attr_accessor :timeout

def initialize(script, *args)
@script = script
@args = args
@timeout = 20
end
def initialize(script, *args)
@script = script
@args = args
@timeout = 20
end

def run
pipe = nil
begin
Timeout.timeout(@timeout) do
pipe = IO.popen(["phantomjs", @script] + @args)
def run
pipe = nil
begin
Timeout.timeout(@timeout) do
pipe = IO.popen(["phantomjs", @script] + @args)
Process.wait pipe.pid
return pipe.read
end
rescue Timeout::Error
Process.kill 9, pipe.pid
Process.wait pipe.pid
return pipe.read
return "Couldn't render page... orz."
end
rescue Timeout::Error
Process.kill 9, pipe.pid
Process.wait pipe.pid
return "Couldn't render page... orz."
end
end
end
4 changes: 2 additions & 2 deletions test/test_indexable.rb
@@ -1,9 +1,9 @@
require 'indexable'

require 'test/unit'
require 'minitest/autorun'
require 'rack/test'

class IndexableTest < Test::Unit::TestCase
class IndexableTest < Minitest::Test
include Rack::Test::Methods

def default_app
Expand Down
6 changes: 3 additions & 3 deletions test/test_phantomjs.rb
@@ -1,19 +1,19 @@
require 'indexable/phantomjs'

class PhantomjsTest < Test::Unit::TestCase
class PhantomjsTest < Minitest::Test
def test_phantomjs_run
file = Tempfile.new('phantomjstest')
file.write("console.log('test');phantom.exit();")
file.close
assert_equal 'test', Phantomjs.new(file.path).run.strip
assert_equal 'test', Indexable::Phantomjs.new(file.path).run.strip
file.unlink
end

def test_timeout
file = Tempfile.new('phantomjstest')
file.write("console.log('this will not exit');")
file.close
phantomjs = Phantomjs.new(file.path)
phantomjs = Indexable::Phantomjs.new(file.path)
phantomjs.timeout = 1
assert_equal "Couldn't render page... orz.", phantomjs.run
file.unlink
Expand Down

0 comments on commit 9557c5f

Please sign in to comment.