Skip to content

Commit

Permalink
Configure non-default path to phantomjs binary
Browse files Browse the repository at this point in the history
  • Loading branch information
esparkman committed Oct 16, 2013
1 parent 32fd30b commit a547b54
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.bundle
Gemfile.lock
pkg/*
*.un~
32 changes: 24 additions & 8 deletions lib/phantomjs.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
require 'tempfile'
require "tempfile"
require "phantomjs/configuration"
require "phantomjs/version"
require 'phantomjs/errors'
require "phantomjs/errors"

class Phantomjs
EXEC = 'phantomjs'

def self.run(path, *args)
def self.run(path, *args, &block)
Phantomjs.new.run(path, *args, &block)
end

def 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)
def self.inline(script, *args, &block)
Phantomjs.new.inline(script, *args, &block)
end

def 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

def self.configure(&block)
Configuration.configure(&block)
end

private

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

def exec
Phantomjs::Configuration.phantomjs_path
end
end
14 changes: 14 additions & 0 deletions lib/phantomjs/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Phantomjs
module Configuration
extend self

attr_accessor :phantomjs_path

Phantomjs::Configuration.phantomjs_path ||= 'phantomjs'

def configure
yield self
end

end
end
19 changes: 19 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'

describe Phantomjs::Configuration do
describe ".configure" do
context "defaults" do
its "default path" do
expect(Phantomjs::Configuration.phantomjs_path).to eq('phantomjs')
end

context "with custom settings" do
before { Phantomjs.configure { |config| config.phantomjs_path = '/bin/phantomjs' } }

its "custom path" do
expect(Phantomjs::Configuration.phantomjs_path).to eq('/bin/phantomjs')
end
end
end
end
end
8 changes: 4 additions & 4 deletions spec/phantomjs_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'phantomjs'
require 'spec_helper'

describe Phantomjs do
describe ".run" do
describe 'when phantomjs is non installed' do
before { Phantomjs::EXEC = 'not_existing_phantomjs' }
after { Phantomjs::EXEC = 'phantomjs' }
before { Phantomjs.configure { |config| config.phantomjs_path = 'not_existing_phantomjs' } }
after { Phantomjs.configure { |config| config.phantomjs_path = 'phantomjs' } }

it "raises an error" do
script = File.expand_path('./spec/runner.js')
Expand All @@ -26,7 +26,7 @@
result = Phantomjs.run(script, 'foo1', 'foo2')
result.should eq("bar\nfoo1\nfoo2\n")
end

it "accepts a block that will get called for each line of output" do
line = ''
script = File.expand_path('./spec/runner.js')
Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'phantomjs'

RSpec.configure do |config|
config.after(:each) do
Phantomjs.configure { |cfg| cfg.phantomjs_path = 'phantomjs' }
end
end

0 comments on commit a547b54

Please sign in to comment.