forked from westoque/phantomjs.rb
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure non-default path to phantomjs binary
- Loading branch information
Showing
6 changed files
with
69 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
.bundle | ||
Gemfile.lock | ||
pkg/* | ||
*.un~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |