phantomjs.rb 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b301765a3009c343d855a4a7fc373dbeaf1fdb1d
4
+ data.tar.gz: d9a337020722eb4f3d228253502668e4cc4b1f40
5
+ SHA512:
6
+ metadata.gz: 765641c2ac1344df5808265990d192a25910d28240a6c8188a1cf50ab0cc1497003f27cd8cd8801f20f7dfbb43d7a70ca2da92fbc75889fcf6dbb910861ba8a6
7
+ data.tar.gz: 1c4d8c39817a207f91714b1039bc9db17a7b92fac2a5f30588886b313670f7e3489d29a5c896177d2ac3832c7df943f09aa7083b779eb7c39ca6c3fea29ca876
data/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 2013.09.07, Version 1.0.2
2
+
3
+ * Complex parameters. (#8) (alwold)
4
+
1
5
  2013.05.16, Version 1.0.1
2
6
 
3
7
  * Added better error messaging when script doesn't exist
data/README.md CHANGED
@@ -16,10 +16,68 @@ gem install phantomjs.rb
16
16
 
17
17
  ## Usage
18
18
 
19
+ ### Pass a file
20
+
21
+ ```js
22
+ // my_runner.js
23
+ var arg1 = phantom.args[0];
24
+ var arg2 = phantom.args[1];
25
+ console.log(arg1 + ' ' + arg2);
26
+ ```
27
+
28
+ Then in ruby:
29
+
30
+ ```rb
31
+ Phantomjs.run('my_runner.js', 'hello', 'world')
32
+ #=> 'hello world'
33
+ ```
34
+
35
+ ### Pass a script
36
+
37
+ You can also pass a javascript string as an argument and the gem
38
+ will create a temporary file for it and run the script.
39
+
40
+ NOTE: Just don't forget to call `phantom.exit`.
41
+
42
+ ```rb
43
+ js = <<JS
44
+ console.log(phantom.args[0] + ' ' + phantom.args[1]);
45
+ phantom.exit();
46
+ JS
47
+
48
+ Phantomjs.run(js, 'hello', 'world')
49
+ #=> 'hello world'
50
+ ```
51
+
52
+ ### But what about async scripts?
53
+
54
+ Well it works for that too!
55
+
19
56
  ```rb
20
- script = File.expand_path('my_runner.js')
21
- output = Phantomjs.run(script, 'myarg1', 'myarg2')
22
- p output # Whatever it outputs from STDOUT
57
+ js = <<JS
58
+ ctr = 0;
59
+ setInterval(function() {
60
+ console.log('ctr is: ' + ctr);
61
+ ctr++;
62
+
63
+ if (ctr == 3) {
64
+ phantom.exit();
65
+ }
66
+ }, 5000);
67
+ JS
68
+
69
+ Phantomjs.inline(js) do |line|
70
+ p line
71
+ end
72
+ #=> ctr is: 0
73
+ ctr is: 1
74
+ ctr is: 2
75
+ ```
76
+
77
+ ## Running the tests
78
+
79
+ ```
80
+ bundle exec rake spec
23
81
  ```
24
82
 
25
83
  ## License
@@ -1,29 +1,35 @@
1
+ require 'tempfile'
1
2
  require "phantomjs/version"
2
3
  require 'phantomjs/errors'
3
4
 
4
5
  class Phantomjs
5
6
  EXEC = 'phantomjs'
6
7
 
7
- # Public: Runs the phantomjs binary
8
- #
9
- # script - The absolute path to the script
10
- # *args - The arguments to pass to the script
11
- #
12
- # Returns the stdout output of phantomjs
13
- def self.run(script, *args)
14
- epath = File.expand_path(script)
15
-
8
+ def self.run(path, *args)
9
+ epath = File.expand_path(path)
16
10
  raise NoSuchPathError.new(epath) unless File.exist?(epath)
11
+ block = block_given? ? Proc.new : nil
12
+ execute(epath, args, block)
13
+ end
14
+
15
+ def self.inline(script, *args)
16
+ file = Tempfile.new('script.js')
17
+ file.write(script)
18
+ file.close
19
+ block = block_given? ? Proc.new : nil
20
+ execute(file.path, args, block)
21
+ end
17
22
 
18
- string_args = args.join(" ")
23
+ private
19
24
 
25
+ def self.execute(path, arguments, block)
20
26
  begin
21
- if block_given?
22
- IO.popen([EXEC, script, args].flatten).each_line do |line|
23
- yield line
27
+ if block
28
+ IO.popen([EXEC, path, arguments].flatten).each_line do |line|
29
+ block.call(line)
24
30
  end
25
31
  else
26
- IO.popen([EXEC, script, args].flatten).read
32
+ IO.popen([EXEC, path, arguments].flatten).read
27
33
  end
28
34
  rescue Errno::ENOENT
29
35
  raise CommandNotFoundError.new('Phantomjs is not installed')
@@ -1,3 +1,3 @@
1
1
  class Phantomjs
2
- VERSION = "1.0.2"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -66,4 +66,35 @@ describe Phantomjs do
66
66
  output.should eq("bar\nfoo bar\nfoo2\n")
67
67
  end
68
68
  end
69
+
70
+ describe '.inline' do
71
+ it 'accepts and runs a script as an argument' do
72
+ js = %q(
73
+ console.log(phantom.args[0]);
74
+ phantom.exit();
75
+ )
76
+ result = Phantomjs.inline(js, 'works!')
77
+ result.should eq("works!\n")
78
+ end
79
+
80
+ it 'accepts a block as an argument' do
81
+ js = %q(
82
+ ctr = 0;
83
+ setInterval(function() {
84
+ if (ctr == 3) {
85
+ phantom.exit();
86
+ }
87
+ console.log('ctr is ' + ctr);
88
+ ctr++;
89
+ }, 1000)
90
+ console.log('running interval')
91
+ )
92
+ expected = "running interval\nctr is 0\nctr is 1\nctr is 2\n"
93
+ str = '';
94
+ result = Phantomjs.inline(js) do |line|
95
+ str << line
96
+ end
97
+ str.should eq(expected)
98
+ end
99
+ end
69
100
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phantomjs.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
5
- prerelease:
4
+ version: 2.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - William Estoque
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-08 00:00:00.000000000 Z
11
+ date: 2013-10-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Ruby wrapper for phantomjs
@@ -66,33 +61,26 @@ files:
66
61
  - spec/runner.js
67
62
  homepage: http://westoque.com
68
63
  licenses: []
64
+ metadata: {}
69
65
  post_install_message:
70
66
  rdoc_options: []
71
67
  require_paths:
72
68
  - lib
73
69
  required_ruby_version: !ruby/object:Gem::Requirement
74
- none: false
75
70
  requirements:
76
- - - ! '>='
71
+ - - '>='
77
72
  - !ruby/object:Gem::Version
78
73
  version: '0'
79
- segments:
80
- - 0
81
- hash: -794486166950785980
82
74
  required_rubygems_version: !ruby/object:Gem::Requirement
83
- none: false
84
75
  requirements:
85
- - - ! '>='
76
+ - - '>='
86
77
  - !ruby/object:Gem::Version
87
78
  version: '0'
88
- segments:
89
- - 0
90
- hash: -794486166950785980
91
79
  requirements: []
92
80
  rubyforge_project: phantomjs.rb
93
- rubygems_version: 1.8.24
81
+ rubygems_version: 2.0.3
94
82
  signing_key:
95
- specification_version: 3
83
+ specification_version: 4
96
84
  summary: Ruby wrapper for phantomjs
97
85
  test_files:
98
86
  - spec/phantomjs_spec.rb