GithubHelp home page GithubHelp logo

harmony's Introduction

Harmony

                   .,ad88888888baa,
               ,d8P"""        ""9888ba.
            .a8"          ,ad88888888888a
           aP'          ,88888888888888888a
         ,8"           ,88888888888888888888,
        ,8'            (888888888( )888888888,
       ,8'             `8888888888888888888888
       8)               `888888888888888888888,
       8                  "8888888888888888888)
       8                   `888888888888888888)
       8)                    "8888888888888888
       (b                     "88888888888888'
       `8,        (8)          8888888888888)
        "8a                   ,888888888888)
          V8,                 d88888888888"
           `8b,             ,d8888888888P'
             `V8a,       ,ad8888888888P'
                ""88888888888888888P"
                     """"""""""""

Summary

Harmony provides a simple DSL to execute javascript + DOM code within ruby.

Examples

Simple Javascript Parsing

require 'harmony'

page = Harmony::Page.new(<<-HTML)
  <html>
    <head>
      <title>Foo</title>
    </head>
    <body></body>
  </html>
HTML

page.execute_js("1+1")            #=> 2
page.execute_js("document.title") #=> "Foo"

The Page object's #execute_js method (aliased as #x for convenience) takes a string of javascript code, executes it and returns the last statement's value (just like a ruby method).

Javascript Unit Tests

One interesting use of Harmony is to test your javascript code within your ruby application's own tests (test/unit, minitest, RSpec, nanotest, etc). Which consequently means that you can now run browser-less, fully command-line based, DOM-javascript tests.

require 'test/unit'
require 'harmony'

class JavascriptTest < Test::Unit::TestCase
  def setup
    @page = Harmony::Page.new
    @page.load('public/javascripts/foo.js')
  end

  def test_foo
    assert_equal "world", @page.execute_js(<<-JS)
      foo = new Foo;
      foo.hello();
    JS
  end
end

DOM Handling

Don't be affraid to throw in your favorite client-side js framework, like JQuery or Prototype. And notice that scripts linked to in <script> tags will automatically get pulled in.

require 'harmony'

page = Harmony::Page.new(<<-HTML)
  <html>
    <head>
      <script src="javascripts/jquery.js" type="text/javascript"></script>
    </head>
    <body>
      <div id="widget">ohaie</div>
    </body>
  </html>
HTML

page.execute_js("$('#widget').innerHTML") #=> "ohaie"

Fetching Documents

Use Harmony::Page.fetch(uri) to create a page from a remote document.

require 'harmony'

page = Harmony::Page.fetch('http://example.com')
page.execute_js('document.title') #=> "Example Web Page"

fetch also accepts "file://" uris.

Install

# There's a gem dependency bug in rubygems currently, so we'll have to
# install dependencies manually. This will be fixed soon.
gem install stackdeck
gem install johnson -v "2.0.0.pre1"

gem install harmony

See Also

Acknowledgement

Harmony is a thin DSL wrapper around three amazing libs, Johnson, env.js and Envjs . The authors of those libs have been doing a huge amount of great work for quite a while, so please go recommend them on WorkingWithRails right now and/or follow them on github:

jbarnette, tenderlove, smparkes, wycats, matthewd, thatcher, jeresig

Special thanks go to smparkes for his patient help, and for providing the last puzzle pieces that made everything work together.

Links

YinYang ASCII art is © Normand Veilleux (nveilleuATemr1.emrDOTca)

harmony's People

Contributors

hardbap avatar mynyml avatar qhoxie avatar

Stargazers

 avatar

Watchers

 avatar

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.