phantomjs.rb 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/Gemfile.lock +31 -0
- data/README.md +22 -4
- data/lib/phantomjs.rb +29 -9
- data/lib/phantomjs/configuration.rb +14 -0
- data/lib/phantomjs/version.rb +1 -1
- data/spec/configuration_spec.rb +19 -0
- data/spec/phantomjs_spec.rb +17 -17
- data/spec/spec_helper.rb +7 -0
- metadata +18 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 000928d55d632a0b8d414a17586f3ef38b351038
|
4
|
+
data.tar.gz: 8a6432fb658fe94b1488ded6e8a7d8462189a653
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41f56b5d74730c4a62db1189330415a75ef2335d3dbec7ad63af30ab7c4fe6b216a0502296043f7a313f41626f8971d38a03278f255567b59281dbf547ec28bf
|
7
|
+
data.tar.gz: 55fad899048c39893f0a02f2e5d85e4e834a828192415ed7720b7a5dbd65937a94ab2885c2a87f957396cc375a2948fc46502baaebac81ec22bf7732c3497bf1
|
data/.gitignore
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
phantomjs.rb (2.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
rake (10.4.2)
|
11
|
+
rspec (3.3.0)
|
12
|
+
rspec-core (~> 3.3.0)
|
13
|
+
rspec-expectations (~> 3.3.0)
|
14
|
+
rspec-mocks (~> 3.3.0)
|
15
|
+
rspec-core (3.3.1)
|
16
|
+
rspec-support (~> 3.3.0)
|
17
|
+
rspec-expectations (3.3.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.3.0)
|
20
|
+
rspec-mocks (3.3.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.3.0)
|
23
|
+
rspec-support (3.3.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
phantomjs.rb!
|
30
|
+
rake
|
31
|
+
rspec
|
data/README.md
CHANGED
@@ -18,6 +18,8 @@ gem install phantomjs.rb
|
|
18
18
|
|
19
19
|
### Pass a file
|
20
20
|
|
21
|
+
Use `Phantomjs.run` to run the passed script file.
|
22
|
+
|
21
23
|
```js
|
22
24
|
// my_runner.js
|
23
25
|
var arg1 = phantom.args[0];
|
@@ -34,8 +36,9 @@ Phantomjs.run('my_runner.js', 'hello', 'world')
|
|
34
36
|
|
35
37
|
### Pass a script
|
36
38
|
|
37
|
-
You can also pass a javascript string as an argument and
|
38
|
-
will create a temporary file for it
|
39
|
+
You can also pass a javascript string as an argument and call
|
40
|
+
`Phantomjs.inline`. This will create a temporary file for it
|
41
|
+
and run the script.
|
39
42
|
|
40
43
|
NOTE: Just don't forget to call `phantom.exit`.
|
41
44
|
|
@@ -45,13 +48,14 @@ js = <<JS
|
|
45
48
|
phantom.exit();
|
46
49
|
JS
|
47
50
|
|
48
|
-
Phantomjs.
|
51
|
+
Phantomjs.inline(js, 'hello', 'world')
|
49
52
|
#=> 'hello world'
|
50
53
|
```
|
51
54
|
|
52
55
|
### But what about async scripts?
|
53
56
|
|
54
|
-
Well it works for that too!
|
57
|
+
Well it works for that too! Just pass a `block` to the method call and the
|
58
|
+
argument will be whatever your script puts out on `stdout`.
|
55
59
|
|
56
60
|
```rb
|
57
61
|
js = <<JS
|
@@ -74,6 +78,20 @@ end
|
|
74
78
|
ctr is: 2
|
75
79
|
```
|
76
80
|
|
81
|
+
## Configuring the path to phantomjs
|
82
|
+
|
83
|
+
If you are using phantomjs in a non-typical installation path or are including the
|
84
|
+
binary inside your application, you can configure phantomjs.rb to use a custom path.
|
85
|
+
|
86
|
+
The following example is a good starting point to follow.
|
87
|
+
|
88
|
+
```rb
|
89
|
+
osx = Rails.env.development? || Rails.env.test?
|
90
|
+
Phantomjs.configure do |config|
|
91
|
+
config.phantomjs_path = "#{Rails.root}/bin/phantomjs-#{osx ? 'osx' : 'x86'}"
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
77
95
|
## Running the tests
|
78
96
|
|
79
97
|
```
|
data/lib/phantomjs.rb
CHANGED
@@ -1,18 +1,26 @@
|
|
1
|
-
require
|
1
|
+
require "tempfile"
|
2
|
+
require "phantomjs/configuration"
|
2
3
|
require "phantomjs/version"
|
3
|
-
require
|
4
|
+
require "phantomjs/errors"
|
4
5
|
|
5
6
|
class Phantomjs
|
6
|
-
EXEC = 'phantomjs'
|
7
7
|
|
8
|
-
def self.run(path, *args)
|
8
|
+
def self.run(path, *args, &block)
|
9
|
+
Phantomjs.new.run(path, *args, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def run(path, *args)
|
9
13
|
epath = File.expand_path(path)
|
10
14
|
raise NoSuchPathError.new(epath) unless File.exist?(epath)
|
11
15
|
block = block_given? ? Proc.new : nil
|
12
16
|
execute(epath, args, block)
|
13
17
|
end
|
14
18
|
|
15
|
-
def self.inline(script, *args)
|
19
|
+
def self.inline(script, *args, &block)
|
20
|
+
Phantomjs.new.inline(script, *args, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def inline(script, *args)
|
16
24
|
file = Tempfile.new('script.js')
|
17
25
|
file.write(script)
|
18
26
|
file.close
|
@@ -20,19 +28,31 @@ class Phantomjs
|
|
20
28
|
execute(file.path, args, block)
|
21
29
|
end
|
22
30
|
|
23
|
-
|
31
|
+
def self.configure(&block)
|
32
|
+
Configuration.configure(&block)
|
33
|
+
end
|
24
34
|
|
25
|
-
|
35
|
+
private
|
36
|
+
|
37
|
+
# Credit to github.com/alex88 for fixing the zombie process issue
|
38
|
+
def execute(path, arguments, block)
|
26
39
|
begin
|
40
|
+
f = IO.popen([exec, path, arguments].flatten)
|
27
41
|
if block
|
28
|
-
|
42
|
+
result = f.each_line do |line|
|
29
43
|
block.call(line)
|
30
44
|
end
|
31
45
|
else
|
32
|
-
|
46
|
+
result = f.read
|
33
47
|
end
|
48
|
+
f.close
|
49
|
+
result
|
34
50
|
rescue Errno::ENOENT
|
35
51
|
raise CommandNotFoundError.new('Phantomjs is not installed')
|
36
52
|
end
|
37
53
|
end
|
54
|
+
|
55
|
+
def exec
|
56
|
+
Phantomjs::Configuration.phantomjs_path
|
57
|
+
end
|
38
58
|
end
|
data/lib/phantomjs/version.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Phantomjs::Configuration do
|
4
|
+
describe ".configure" do
|
5
|
+
context "defaults" do
|
6
|
+
it "default path" do
|
7
|
+
expect(Phantomjs::Configuration.phantomjs_path).to eq('phantomjs')
|
8
|
+
end
|
9
|
+
|
10
|
+
context "with custom settings" do
|
11
|
+
before { Phantomjs.configure { |config| config.phantomjs_path = '/bin/phantomjs' } }
|
12
|
+
|
13
|
+
it "custom path" do
|
14
|
+
expect(Phantomjs::Configuration.phantomjs_path).to eq('/bin/phantomjs')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/spec/phantomjs_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Phantomjs do
|
4
4
|
describe ".run" do
|
5
5
|
describe 'when phantomjs is non installed' do
|
6
|
-
before { Phantomjs
|
7
|
-
after
|
6
|
+
before { Phantomjs.configure { |config| config.phantomjs_path = 'not_existing_phantomjs' } }
|
7
|
+
after { Phantomjs.configure { |config| config.phantomjs_path = 'phantomjs' } }
|
8
8
|
|
9
9
|
it "raises an error" do
|
10
10
|
script = File.expand_path('./spec/runner.js')
|
@@ -24,16 +24,16 @@ describe Phantomjs do
|
|
24
24
|
it "runs phantomjs binary with the correct arguments" do
|
25
25
|
script = File.expand_path('./spec/runner.js')
|
26
26
|
result = Phantomjs.run(script, 'foo1', 'foo2')
|
27
|
-
result.
|
27
|
+
expect(result).to eq("bar\nfoo1\nfoo2\n")
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
it "accepts a block that will get called for each line of output" do
|
31
31
|
line = ''
|
32
32
|
script = File.expand_path('./spec/runner.js')
|
33
33
|
Phantomjs.run(script, 'foo1', 'foo2') do |l|
|
34
34
|
line << l
|
35
35
|
end
|
36
|
-
line.
|
36
|
+
expect(line).to eq("bar\nfoo1\nfoo2\n")
|
37
37
|
end
|
38
38
|
|
39
39
|
it "accepts empty parameters" do
|
@@ -42,7 +42,7 @@ describe Phantomjs do
|
|
42
42
|
Phantomjs.run(script, 'foo1', 'foo2', '') do |l|
|
43
43
|
line << l
|
44
44
|
end
|
45
|
-
line.
|
45
|
+
expect(line).to eq("bar\nfoo1\nfoo2\n\n")
|
46
46
|
end
|
47
47
|
|
48
48
|
it "accepts parameters with spaces" do
|
@@ -51,33 +51,33 @@ describe Phantomjs do
|
|
51
51
|
Phantomjs.run(script, 'foo bar', 'foo2') do |l|
|
52
52
|
line << l
|
53
53
|
end
|
54
|
-
line.
|
54
|
+
expect(line).to eq("bar\nfoo bar\nfoo2\n")
|
55
55
|
end
|
56
56
|
|
57
57
|
it "accepts empty parameters without block" do
|
58
58
|
script = File.expand_path('./spec/runner.js')
|
59
59
|
output = Phantomjs.run(script, 'foo1', 'foo2', '')
|
60
|
-
output.
|
60
|
+
expect(output).to eq("bar\nfoo1\nfoo2\n\n")
|
61
61
|
end
|
62
62
|
|
63
63
|
it "accepts parameters with spaces without block" do
|
64
64
|
script = File.expand_path('./spec/runner.js')
|
65
65
|
output = Phantomjs.run(script, 'foo bar', 'foo2')
|
66
|
-
output.
|
66
|
+
expect(output).to eq("bar\nfoo bar\nfoo2\n")
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
describe
|
71
|
-
it
|
70
|
+
describe ".inline" do
|
71
|
+
it "accepts and runs a script as an argument" do
|
72
72
|
js = %q(
|
73
73
|
console.log(phantom.args[0]);
|
74
74
|
phantom.exit();
|
75
75
|
)
|
76
76
|
result = Phantomjs.inline(js, 'works!')
|
77
|
-
result.
|
77
|
+
expect(result).to eq("works!\n")
|
78
78
|
end
|
79
79
|
|
80
|
-
it
|
80
|
+
it "accepts a block as an argument" do
|
81
81
|
js = %q(
|
82
82
|
ctr = 0;
|
83
83
|
setInterval(function() {
|
@@ -90,11 +90,11 @@ describe Phantomjs do
|
|
90
90
|
console.log('running interval')
|
91
91
|
)
|
92
92
|
expected = "running interval\nctr is 0\nctr is 1\nctr is 2\n"
|
93
|
-
str =
|
94
|
-
|
93
|
+
str = "";
|
94
|
+
Phantomjs.inline(js) do |line|
|
95
95
|
str << line
|
96
96
|
end
|
97
|
-
str.
|
97
|
+
expect(str).to match(expected)
|
98
98
|
end
|
99
99
|
end
|
100
100
|
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phantomjs.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- William Estoque
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
description: Ruby wrapper for phantomjs
|
@@ -45,20 +45,24 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
-
- .autotest
|
49
|
-
- .gitignore
|
50
|
-
- .travis.yml
|
48
|
+
- ".autotest"
|
49
|
+
- ".gitignore"
|
50
|
+
- ".travis.yml"
|
51
51
|
- CHANGELOG
|
52
52
|
- Gemfile
|
53
|
+
- Gemfile.lock
|
53
54
|
- MIT-LICENSE
|
54
55
|
- README.md
|
55
56
|
- Rakefile
|
56
57
|
- lib/phantomjs.rb
|
58
|
+
- lib/phantomjs/configuration.rb
|
57
59
|
- lib/phantomjs/errors.rb
|
58
60
|
- lib/phantomjs/version.rb
|
59
61
|
- phantomjs.rb.gemspec
|
62
|
+
- spec/configuration_spec.rb
|
60
63
|
- spec/phantomjs_spec.rb
|
61
64
|
- spec/runner.js
|
65
|
+
- spec/spec_helper.rb
|
62
66
|
homepage: http://westoque.com
|
63
67
|
licenses: []
|
64
68
|
metadata: {}
|
@@ -68,20 +72,22 @@ require_paths:
|
|
68
72
|
- lib
|
69
73
|
required_ruby_version: !ruby/object:Gem::Requirement
|
70
74
|
requirements:
|
71
|
-
- -
|
75
|
+
- - ">="
|
72
76
|
- !ruby/object:Gem::Version
|
73
77
|
version: '0'
|
74
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
79
|
requirements:
|
76
|
-
- -
|
80
|
+
- - ">="
|
77
81
|
- !ruby/object:Gem::Version
|
78
82
|
version: '0'
|
79
83
|
requirements: []
|
80
84
|
rubyforge_project: phantomjs.rb
|
81
|
-
rubygems_version: 2.
|
85
|
+
rubygems_version: 2.4.6
|
82
86
|
signing_key:
|
83
87
|
specification_version: 4
|
84
88
|
summary: Ruby wrapper for phantomjs
|
85
89
|
test_files:
|
90
|
+
- spec/configuration_spec.rb
|
86
91
|
- spec/phantomjs_spec.rb
|
87
92
|
- spec/runner.js
|
93
|
+
- spec/spec_helper.rb
|