• Stars
    star
    446
  • Rank 97,888 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 15 years ago
  • Updated over 14 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Javascript + DOM in your ruby, the simple way

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 some dependencies manually. This will be fixed soon.
gem install stackdeck
gem install johnson -v "2.0.0.pre3"

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)

More Repositories

1

watchr

Modern continuous testing (flexible alternative to Autotest)
Ruby
1,277
star
2

holygrail

Harmony plugin for Ruby on Rails tests
Ruby
169
star
3

every

Symbol#to_proc's hot cousin. Simple and elegant alternative to using &:method with enumerables.
Ruby
75
star
4

nanotest

When all you need is #assert
Ruby
69
star
5

redgreen

Standalone redgreen eye candy for test results, ala autotest
Ruby
43
star
6

simple_router

Simple rack router
Ruby
28
star
7

unindent

Ruby method to unindent strings.
Ruby
27
star
8

phocus

Focus your tests
Ruby
25
star
9

rack-respond_to

Rack middleware port of Rails's respond_to feature
Ruby
23
star
10

attachment_fu_fixtures

Allows attachment_fu models to fully use fixtures. Ideal for seed, sample or dev data.
Ruby
17
star
11

montrealrb

Montreal.rb website source
Ruby
16
star
12

rack-accept-media-types

Rack convenience middleware for simplified handling of Accept header (Accept header parser).
Ruby
13
star
13

pathname

OO wrapper for node's path/fs modules. Based on Ruby's sweet Pathname class
JavaScript
12
star
14

rack-abstract-format

Rack middleware that abstracts format (extension) away from the path (into env)
Ruby
12
star
15

nanotest_extensions

Nano extensions for nanotest
Ruby
8
star
16

rack-supported-media-types

Rack middleware to specify an app's supported media types.
Ruby
7
star
17

override

Like #alias_method_chain, but awesome
Ruby
6
star
18

simple_example

Add easy fancy-pants examples to your projects
Ruby
3
star
19

swap

Ruby lib that allows dynamically replacing and restoring methods
Ruby
3
star
20

merb-in-file-templates

Define views right in your controller's file
Ruby
2
star
21

merb_simple_views

Merb plugin that allows defining templates (views, css, js)in the same file as the controller.
Ruby
2
star
22

tmptation

Classes that help safely manipulate temporary files and directories
Ruby
1
star
23

ntest

a little node.js test framework i wrote to learn the ropes.
JavaScript
1
star
24

rack-js4xhr

Rack middleware to set application/javascript media type on xhr requests.
Ruby
1
star