maccman-phantomrb 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4dea982ca28991fd17bfc1351997a4fd3a562b5a
4
+ data.tar.gz: ec3f2d1d97e20d50db2ba622d75a8c71eaa6e2d4
5
+ SHA512:
6
+ metadata.gz: 185498e6e8478f02323c14ca7a767eba59c317e4a97375f8b4c2ccd615001ad34c8b5cb9d8a300cfad8d66e39aa49be9680149547224a64a2fe0e747f933cdc7
7
+ data.tar.gz: 92c05de65a71f2f94722d7a1addda3074b8d3a9a02334bd9178b5b9a041e6576624fda92ed7c4f5b645a3e4203883861ecfc228853bb6797981e06cec528cb38
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/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+
7
+ branches:
8
+ only:
9
+ - master
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,99 @@
1
+ # Phantomrb
2
+
3
+ An interface with PhantomJS for Ruby.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/phantomrb.png)](http://badge.fury.io/rb/phantomrb)
6
+
7
+ [![Build Status](https://travis-ci.org/agladkyi/phantomrb.png?branch=master)](https://travis-ci.org/agladkyi/phantomrb)
8
+
9
+ ## Dependencies
10
+
11
+ [PhantomJS](http://phantomjs.org) must be installed on your system.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'phantomrb'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install phantomrb
26
+
27
+ ## Usage
28
+
29
+ ### Simple
30
+
31
+ Phantomrb.run('/path/to/script', arg1, arg2, ...)
32
+
33
+ ### With block
34
+
35
+ Phantomrb.run('/path/to/script', arg1, arg2, ...) { |line| ... }
36
+
37
+ ### Notes
38
+
39
+ * Path can be absolute or relative to app directory.
40
+ * All arguments are optional.
41
+ * If block is specified it will be called with each line from PhantomJS stdout.
42
+
43
+ ### Example
44
+
45
+ // echo.js
46
+ for (var i = 0; i < phantom.args.length; i++) {
47
+ console.log(phantom.args[i]);
48
+ }
49
+
50
+ In your ruby code:
51
+
52
+ Phantomrb.run('echo.js', 'line one', 'line two') { |line| puts line }
53
+
54
+ Will output:
55
+
56
+ test1
57
+ test2
58
+
59
+ ### Return value
60
+
61
+ Phantomrb returns an OpenStruct object with following data:
62
+
63
+ * `output` - full stdout from PhantomJS
64
+ * `exit_status` - exit status code
65
+ * `command_line` - full command line for PhantomJS
66
+
67
+ ## Configuration
68
+
69
+ Phantomrb provides a way to define a list of command line options for PhantomJS.
70
+ Create an initializer `config/initializers/phantomrb.rb` with following code:
71
+
72
+ Phantomrb.configure do
73
+ option '<parameter name>', <parameter value>
74
+ end
75
+
76
+ ### Example
77
+ # config/initializers/phantomrb.rb
78
+ Phantomrb.configure do
79
+ option 'load-images', false
80
+ option 'ignore-ssl-errors', true
81
+ end
82
+
83
+ Phantomrb.run('myscript.js', 'arg1', 'arg2')
84
+
85
+ will run `phantomjs --load-images=false --ignore-ssl-errors=true /absolute/path/to/myscript.js arg1 arg2`
86
+
87
+ More info about PhantomJS command line options you can found in [wiki](https://github.com/ariya/phantomjs/wiki/API-Reference#command-line-options).
88
+
89
+ ## License
90
+
91
+ Licensed under the MIT license.
92
+
93
+ ## Contributing
94
+
95
+ 1. Fork it
96
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
97
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
98
+ 4. Push to the branch (`git push origin my-new-feature`)
99
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
7
+ task test: :spec
@@ -0,0 +1,31 @@
1
+ module Phantomrb
2
+ class Configuration
3
+ attr_reader :options, :executable
4
+
5
+ def initialize(options = {})
6
+ @options = {}
7
+ @executable = 'phantomjs'
8
+ end
9
+
10
+ def option(key, value)
11
+ @options[key] = value
12
+ end
13
+
14
+ def merge(options)
15
+ Configuration.new(@options.merge(options))
16
+ end
17
+
18
+ def to_options
19
+ @options.inject({}) do |hash, (key, value)|
20
+ hash[key.to_s.gsub('_', '-')] = value
21
+ hash
22
+ end
23
+ end
24
+
25
+ def to_s
26
+ to_options.reduce(@executable) do |memo, (key, value)|
27
+ "#{memo} --#{key}=#{value}"
28
+ end
29
+ end
30
+ end
31
+ 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,55 @@
1
+ require 'ostruct'
2
+
3
+ module Phantomrb
4
+ class Runner
5
+ def initialize
6
+ @config = Phantomrb.configuration
7
+ end
8
+
9
+ def run(script, *arguments, &block)
10
+ options = arguments.last.is_a?(Hash) ? arguments.pop : {}
11
+ command = @config.merge(options)
12
+
13
+ command_line = "#{command} #{full_script_path(script)} #{arguments.join(' ')}"
14
+
15
+ begin
16
+ process = IO.popen(command_line)
17
+ rescue => e
18
+ raise ExecutableLoadError.new(e)
19
+ end
20
+
21
+ output = capture_output(process, &block)
22
+ process.close
23
+
24
+ OpenStruct.new(
25
+ output: output,
26
+ exit_status: $?.exitstatus,
27
+ command_line: command_line
28
+ )
29
+ end
30
+
31
+ private
32
+
33
+ def full_script_path(script)
34
+ full_script_path = File.expand_path(script)
35
+ if File.file?(full_script_path)
36
+ full_script_path
37
+ else
38
+ raise ScriptLoadError.new("#{full_script_path} not found")
39
+ end
40
+ end
41
+
42
+ def capture_output(process)
43
+ if block_given?
44
+ output = ''
45
+ process.each_line do |output_line|
46
+ output << output_line
47
+ yield(output_line)
48
+ end
49
+ output
50
+ else
51
+ process.read
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Phantomrb
2
+ VERSION = '1.0.0'
3
+ end
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
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 = 'maccman-phantomrb'
8
+ spec.version = Phantomrb::VERSION
9
+ spec.authors = ['Andrei Gladkyi', 'Alex MacCaw']
10
+ spec.email = ['arg@arglabs.net', 'info@eribium.org']
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,77 @@
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
+
19
+ it 'to_s' do
20
+ expect(Phantomrb.configuration.to_s).to eq('phantomjs --ignore-ssl-errors=true')
21
+ end
22
+ end
23
+
24
+ describe '::run' do
25
+ describe Phantomrb::ExecutableLoadError do
26
+ before do
27
+ Phantomrb.configuration.instance_variable_set(:'@executable', 'sjmotnahp')
28
+ Phantomrb.instance_variable_set(:'@runner', nil)
29
+ end
30
+
31
+ after do
32
+ Phantomrb.configuration.instance_variable_set(:'@executable', 'phantomjs')
33
+ Phantomrb.instance_variable_set(:'@runner', nil)
34
+ end
35
+
36
+ it 'raises an exception if PhantomJS not found' do
37
+ expect { Phantomrb.run(js_test_script) }.to raise_exception(Phantomrb::ExecutableLoadError)
38
+ end
39
+ end
40
+
41
+ describe Phantomrb::ScriptLoadError do
42
+ it 'raises an exception if script file not found' do
43
+ expect { Phantomrb.run('none.js') }.to raise_exception(Phantomrb::ScriptLoadError)
44
+ end
45
+ end
46
+
47
+ describe 'Result object' do
48
+ subject { Phantomrb.run(js_test_script) }
49
+
50
+ it 'has correct type' do
51
+ expect(subject).to be_an(OpenStruct)
52
+ end
53
+
54
+ it 'contains the correct exit status code' do
55
+ expect(subject.exit_status).to eq(123)
56
+ end
57
+
58
+ it 'contains output string' do
59
+ expect(subject.output).to eq("test\n")
60
+ end
61
+
62
+ it 'contains command line string' do
63
+ expect(subject.command_line).to_not be_empty
64
+ end
65
+ end
66
+
67
+ it 'passes parameters to a script' do
68
+ expect(Phantomrb.run(js_test_script, 1, 2, 3).output).to eq("test\n1\n2\n3\n")
69
+ end
70
+
71
+ it 'invokes a block on each output line' do
72
+ output = ''
73
+ Phantomrb.run(js_test_script, 1, 2, 3) { |line| output << line }
74
+ expect(output).to eq("test\n1\n2\n3\n")
75
+ end
76
+ end
77
+ 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,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maccman-phantomrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Gladkyi
8
+ - Alex MacCaw
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-09-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.0.0
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.0.0
56
+ description: An interface with PhantomJS for Ruby.
57
+ email:
58
+ - arg@arglabs.net
59
+ - info@eribium.org
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - lib/phantomrb.rb
72
+ - lib/phantomrb/configuration.rb
73
+ - lib/phantomrb/errors.rb
74
+ - lib/phantomrb/runner.rb
75
+ - lib/phantomrb/version.rb
76
+ - phantomrb.gemspec
77
+ - spec/phantomrb_spec.rb
78
+ - spec/spec_helper.rb
79
+ - spec/test.js
80
+ homepage: https://github.com/agladkyi/phantomrb
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.2.2
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: An interface with PhantomJS for Ruby.
104
+ test_files:
105
+ - spec/phantomrb_spec.rb
106
+ - spec/spec_helper.rb
107
+ - spec/test.js