phantomrb 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b54fa3a557ec73d390581fcf4febfb2ed1f4b5c
4
+ data.tar.gz: c45cc805ec80e938b75e992d8288e48c31d8db00
5
+ SHA512:
6
+ metadata.gz: fda6335854d7d49aa24c25fb1fa399e30cf72d6fd78f4d93c76d5a6c66cbd2309a3ec7f6feb7d1dc0593649e6bfeba4f975a586d9371de22c9492f47addb9777
7
+ data.tar.gz: c7a06efe3caf53ea840d2c19a41920bce354bf51703f371757754d457bf3f3f8eecb0bea513033e59019265eb91726954eb39bfa78bfa120290dadfcf2c5f91d
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg
5
+ .idea
6
+ .DS_Store
7
+ doc
8
+ .yardoc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phantomrb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrei Gladkyi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # Phantomrb
2
+
3
+ An interface with PhantomJS for Ruby.
4
+
5
+ ## Dependencies
6
+
7
+ [PhantomJS](http://phantomjs.org) must be installed on your system.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'phantomrb'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install phantomrb
22
+
23
+ ## Usage
24
+
25
+ ### Simple
26
+
27
+ Phantomrb.run('/path/to/script', arg1, arg2, ...)
28
+
29
+ ### With block
30
+
31
+ Phantomrb.run('/path/to/script', arg1, arg2, ...) { |line| ... }
32
+
33
+ ### Notes
34
+
35
+ * Path can be absolute or relative to app directory.
36
+ * All arguments are optional.
37
+ * If block is specified it will be called with each line from PhantomJS stdout.
38
+
39
+ ### Example
40
+
41
+ // echo.js
42
+ for (var i = 0; i < phantom.args.length; i++) {
43
+ console.log(phantom.args[i]);
44
+ }
45
+
46
+ In your ruby code:
47
+
48
+ Phantomrb.run('echo.js', 'line one', 'line two') { |line| puts line }
49
+
50
+ Will output:
51
+
52
+ test1
53
+ test2
54
+
55
+ ### Return value
56
+
57
+ Phantomrb returns an OpenStruct object with following data:
58
+
59
+ * `output` - full stdout from PhantomJS
60
+ * `exit_status` - exit status code
61
+ * `command_line` - full command line for PhantomJS
62
+
63
+ ## Configuration
64
+
65
+ Phantomrb provides a way to define a list of command line options for PhantomJS.
66
+ Create an initializer `config/initializers/phantomrb.rb` with following code:
67
+
68
+ Phantomrb.configure do
69
+ option '<parameter name>', <parameter value>
70
+ end
71
+
72
+ ### Example
73
+ # config/initializers/phantomrb.rb
74
+ Phantomrb.configure do
75
+ option 'load-images', false
76
+ option 'ignore-ssl-errors', true
77
+ end
78
+
79
+ Phantomrb.run('myscript.js', 'arg1', 'arg2')
80
+
81
+ will run `phantomjs --load-images=false --ignore-ssl-errors=true /absolute/path/to/myscript.js arg1 arg2`
82
+
83
+ More info about PhantomJS command line options you can found in [wiki](https://github.com/ariya/phantomjs/wiki/API-Reference#command-line-options).
84
+
85
+ ## License
86
+
87
+ Licensed under the MIT license.
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
94
+ 4. Push to the branch (`git push origin my-new-feature`)
95
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/lib/phantomrb.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'phantomrb/version'
2
+ require 'phantomrb/errors'
3
+ require 'phantomrb/configuration'
4
+ require 'phantomrb/runner'
5
+
6
+ module Phantomrb
7
+ class << self
8
+ # Returns the global configuration.
9
+ # @return [Object] configuration
10
+ def configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ # Yields the global configuration to a block.
15
+ # @yield global configuration
16
+ #
17
+ # @example
18
+ # Phantomrb.configure do
19
+ # parameter 'ignore-ssl-errors', true
20
+ # end
21
+ def configure(&block)
22
+ configuration.instance_eval(&block) if block_given?
23
+ end
24
+
25
+ # Runs JavaScript file.
26
+ # @param script [String] path for a script
27
+ # @param args [String] script arguments
28
+ # @yieldparam line [String] line from stdout
29
+ # @return [OpenStruct] data
30
+ # * <b>output</b> (String) --- full stdout from PhantomJS
31
+ # * <b>exit_status</b> (Integer) --- exit status code
32
+ # * <b>command_line</b> (String) --- full command line
33
+ #
34
+ # @example Simple output
35
+ # puts Phantomrb.run('echo.js', 'test').output #=> "test"
36
+ #
37
+ # @example Each line separate output
38
+ # Phantomrb.run('echo.js', 'test') do |line|
39
+ # puts line #=> "test"
40
+ # end
41
+ def run(script, *args, &block)
42
+ @runner ||= Runner.new
43
+ @runner.run(script, args, &block)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ module Phantomrb
2
+ class Configuration
3
+ attr_reader :options, :executable
4
+
5
+ def initialize
6
+ @options = {}
7
+ @executable = 'phantomjs'
8
+ end
9
+
10
+ def option(key, value)
11
+ @options[key] = value
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Phantomrb
2
+ class ExecutableLoadError < StandardError
3
+ end
4
+
5
+ class ScriptLoadError < StandardError
6
+ end
7
+ end
@@ -0,0 +1,48 @@
1
+ require 'ostruct'
2
+
3
+ module Phantomrb
4
+ class Runner
5
+ def initialize
6
+ config = Phantomrb.configuration
7
+ @command = config.options.reduce(config.executable) do |memo, (key, value)|
8
+ "#{memo} --#{key}=#{value}"
9
+ end
10
+ end
11
+
12
+ def run(script, *arguments, &block)
13
+ command_line = "#{@command} #{full_script_path(script)} #{arguments.join(' ')}"
14
+ begin
15
+ process = IO.popen(command_line)
16
+ rescue => e
17
+ raise ExecutableLoadError.new(e)
18
+ end
19
+ output = capture_output(process, &block)
20
+ process.close
21
+ OpenStruct.new(output: output, exit_status: $?.exitstatus, command_line: command_line)
22
+ end
23
+
24
+ private
25
+
26
+ def full_script_path(script)
27
+ full_script_path = File.expand_path(script)
28
+ if File.file?(full_script_path)
29
+ full_script_path
30
+ else
31
+ raise ScriptLoadError.new("#{full_script_path} not found")
32
+ end
33
+ end
34
+
35
+ def capture_output(process)
36
+ if block_given?
37
+ output = ''
38
+ process.each_line do |output_line|
39
+ output << output_line
40
+ yield(output_line)
41
+ end
42
+ output
43
+ else
44
+ process.read
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Phantomrb
2
+ VERSION = '1.0.0'
3
+ end
data/phantomrb.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'phantomrb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'phantomrb'
8
+ spec.version = Phantomrb::VERSION
9
+ spec.authors = ['Andrei Gladkyi']
10
+ spec.email = ['arg@arglabs.net']
11
+ spec.description = 'An interface with PhantomJS for Ruby.'
12
+ spec.summary = 'An interface with PhantomJS for Ruby.'
13
+ spec.homepage = 'https://github.com/agladkyi/phantomrb'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(/^spec\//)
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec', '>= 2.0.0'
23
+ end
@@ -0,0 +1,73 @@
1
+ describe Phantomrb do
2
+ let(:js_test_script) { 'spec/test.js' }
3
+
4
+ describe '::configure' do
5
+ before :all do
6
+ Phantomrb.configure do
7
+ option('ignore-ssl-errors', true)
8
+ end
9
+ end
10
+
11
+ it 'has the correct executable command' do
12
+ expect(Phantomrb.configuration.executable).to eq('phantomjs')
13
+ end
14
+
15
+ it 'allows to set parameters' do
16
+ expect(Phantomrb.configuration.options).to eq({ 'ignore-ssl-errors' => true })
17
+ end
18
+ end
19
+
20
+ describe '::run' do
21
+ describe Phantomrb::ExecutableLoadError do
22
+ before do
23
+ Phantomrb.configuration.instance_variable_set(:'@executable', 'sjmotnahp')
24
+ Phantomrb.instance_variable_set(:'@runner', nil)
25
+ end
26
+
27
+ after do
28
+ Phantomrb.configuration.instance_variable_set(:'@executable', 'phantomjs')
29
+ Phantomrb.instance_variable_set(:'@runner', nil)
30
+ end
31
+
32
+ it 'raises an exception if PhantomJS not found' do
33
+ expect { Phantomrb.run(js_test_script) }.to raise_exception(Phantomrb::ExecutableLoadError)
34
+ end
35
+ end
36
+
37
+ describe Phantomrb::ScriptLoadError do
38
+ it 'raises an exception if script file not found' do
39
+ expect { Phantomrb.run('none.js') }.to raise_exception(Phantomrb::ScriptLoadError)
40
+ end
41
+ end
42
+
43
+ describe 'Result object' do
44
+ subject { Phantomrb.run(js_test_script) }
45
+
46
+ it 'has correct type' do
47
+ expect(subject).to be_an(OpenStruct)
48
+ end
49
+
50
+ it 'contains the correct exit status code' do
51
+ expect(subject.exit_status).to eq(123)
52
+ end
53
+
54
+ it 'contains output string' do
55
+ expect(subject.output).to eq("test\n")
56
+ end
57
+
58
+ it 'contains command line string' do
59
+ expect(subject.command_line).to_not be_empty
60
+ end
61
+ end
62
+
63
+ it 'passes parameters to a script' do
64
+ expect(Phantomrb.run(js_test_script, 1, 2, 3).output).to eq("test\n1\n2\n3\n")
65
+ end
66
+
67
+ it 'invokes a block on each output line' do
68
+ output = ''
69
+ Phantomrb.run(js_test_script, 1, 2, 3) { |line| output << line }
70
+ expect(output).to eq("test\n1\n2\n3\n")
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,17 @@
1
+ describe Phantomrb::Runner do
2
+ before :all do
3
+ Phantomrb.configure do
4
+ option('load-images', false)
5
+ end
6
+ end
7
+
8
+ it 'sets the correct executable command' do
9
+ runner = Phantomrb::Runner.new
10
+ expect(runner.instance_variable_get(:'@command')).to include('phantomjs')
11
+ end
12
+
13
+ it 'sets the correct command line parameters' do
14
+ runner = Phantomrb::Runner.new
15
+ expect(runner.instance_variable_get(:'@command')).to include('--load-images=false')
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'phantomrb'
5
+
6
+ RSpec.configure do |config|
7
+ config.expect_with :rspec do |c|
8
+ c.syntax = :expect
9
+ end
10
+ end
data/spec/test.js ADDED
@@ -0,0 +1,7 @@
1
+ console.log('test');
2
+
3
+ for (var i = 0; i < phantom.args.length; i++) {
4
+ console.log(phantom.args[i]);
5
+ }
6
+
7
+ phantom.exit(123);
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phantomrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Gladkyi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.0
55
+ description: An interface with PhantomJS for Ruby.
56
+ email:
57
+ - arg@arglabs.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/phantomrb.rb
69
+ - lib/phantomrb/configuration.rb
70
+ - lib/phantomrb/errors.rb
71
+ - lib/phantomrb/runner.rb
72
+ - lib/phantomrb/version.rb
73
+ - phantomrb.gemspec
74
+ - spec/phantomrb_spec.rb
75
+ - spec/runner_spec.rb
76
+ - spec/spec_helper.rb
77
+ - spec/test.js
78
+ homepage: https://github.com/agladkyi/phantomrb
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.1.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: An interface with PhantomJS for Ruby.
102
+ test_files:
103
+ - spec/phantomrb_spec.rb
104
+ - spec/runner_spec.rb
105
+ - spec/spec_helper.rb
106
+ - spec/test.js