Git Product home page Git Product logo

phantomjs.rb's Introduction

phantomjs.rb

Build Status

A ruby wrapper for phantomjs.

Requirements

You have to have phantomjs installed.

Install

gem install phantomjs.rb

Usage

Pass a file

Use Phantomjs.run to run the passed script file.

// my_runner.js
var arg1 = phantom.args[0];
var arg2 = phantom.args[1];
console.log(arg1 + ' ' + arg2);

Then in ruby:

Phantomjs.run('my_runner.js', 'hello', 'world')
#=> 'hello world'

Pass a script

You can also pass a javascript string as an argument and call Phantomjs.inline. This will create a temporary file for it and run the script.

NOTE: Just don't forget to call phantom.exit.

js = <<JS
  console.log(phantom.args[0] + ' ' + phantom.args[1]);
  phantom.exit();
JS

Phantomjs.inline(js, 'hello', 'world')
#=> 'hello world'

But what about async scripts?

Well it works for that too! Just pass a block to the method call and the argument will be whatever your script puts out on stdout.

js = <<JS
  ctr = 0;
  setInterval(function() {
    console.log('ctr is: ' + ctr);
    ctr++;

    if (ctr == 3) {
      phantom.exit();
    }
  }, 5000);
JS

Phantomjs.inline(js) do |line|
  p line
end
#=> ctr is: 0
    ctr is: 1
    ctr is: 2

Configuring the path to phantomjs

If you are using phantomjs in a non-typical installation path or are including the binary inside your application, you can configure phantomjs.rb to use a custom path.

The following example is a good starting point to follow.

osx = Rails.env.development? || Rails.env.test?
Phantomjs.configure do |config|
  config.phantomjs_path = "#{Rails.root}/bin/phantomjs-#{osx ? 'osx' : 'x86'}"
end

Running the tests

bundle exec rake spec

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.