harmony 0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/.gitignore +5 -0
  2. data/LICENSE +19 -0
  3. data/Manifest +117 -0
  4. data/README.md +155 -0
  5. data/Rakefile +39 -0
  6. data/docs.watchr +25 -0
  7. data/harmony.gemspec +18 -0
  8. data/lib/harmony.rb +5 -0
  9. data/lib/harmony/page.rb +123 -0
  10. data/specs.watchr +36 -0
  11. data/test/harmony_test.rb +8 -0
  12. data/test/page_test.rb +73 -0
  13. data/test/test_helper.rb +13 -0
  14. data/vendor/README +1 -0
  15. data/vendor/envjs/README +36 -0
  16. data/vendor/envjs/bin/envjsrb +255 -0
  17. data/vendor/envjs/bin/jquery-1.2.6-test.js +33 -0
  18. data/vendor/envjs/bin/jquery-1.3.1-test.js +33 -0
  19. data/vendor/envjs/bin/jquery-1.3.2-test.js +106 -0
  20. data/vendor/envjs/bin/prototype-1.6.0.3-test.js +82 -0
  21. data/vendor/envjs/bin/prototype_1.6.0.3_tmpl.txt +27 -0
  22. data/vendor/envjs/bin/test-jquery.sh +58 -0
  23. data/vendor/envjs/bin/test-prototype.sh +54 -0
  24. data/vendor/envjs/bin/tidy +0 -0
  25. data/vendor/envjs/lib/envjs.rb +23 -0
  26. data/vendor/envjs/lib/envjs/env.js +21665 -0
  27. data/vendor/envjs/lib/envjs/net.rb +3 -0
  28. data/vendor/envjs/lib/envjs/net/cgi.rb +94 -0
  29. data/vendor/envjs/lib/envjs/net/file.rb +75 -0
  30. data/vendor/envjs/lib/envjs/options.rb +11 -0
  31. data/vendor/envjs/lib/envjs/runtime.rb +351 -0
  32. data/vendor/envjs/lib/envjs/tempfile.rb +24 -0
  33. data/vendor/envjs/test/base64.js +80 -0
  34. data/vendor/envjs/test/call-load-test.js +15 -0
  35. data/vendor/envjs/test/data.js +45 -0
  36. data/vendor/envjs/test/debug.js +53 -0
  37. data/vendor/envjs/test/firebug/errorIcon.png +0 -0
  38. data/vendor/envjs/test/firebug/firebug.css +209 -0
  39. data/vendor/envjs/test/firebug/firebug.html +23 -0
  40. data/vendor/envjs/test/firebug/firebug.js +672 -0
  41. data/vendor/envjs/test/firebug/firebugx.js +10 -0
  42. data/vendor/envjs/test/firebug/infoIcon.png +0 -0
  43. data/vendor/envjs/test/firebug/warningIcon.png +0 -0
  44. data/vendor/envjs/test/fixtures/html/events.html +171 -0
  45. data/vendor/envjs/test/fixtures/html/iframe1.html +46 -0
  46. data/vendor/envjs/test/fixtures/html/iframe1a.html +46 -0
  47. data/vendor/envjs/test/fixtures/html/iframe2.html +45 -0
  48. data/vendor/envjs/test/fixtures/html/iframe3.html +28 -0
  49. data/vendor/envjs/test/fixtures/html/iframeN.html +57 -0
  50. data/vendor/envjs/test/fixtures/html/malformed.html +181 -0
  51. data/vendor/envjs/test/fixtures/html/scope.html +81 -0
  52. data/vendor/envjs/test/fixtures/html/trivial.html +19 -0
  53. data/vendor/envjs/test/fixtures/html/with_js.html +26 -0
  54. data/vendor/envjs/test/fixtures/images/icon-blue.png +0 -0
  55. data/vendor/envjs/test/fixtures/js/external_script.js +1 -0
  56. data/vendor/envjs/test/fixtures/js/script.js +1 -0
  57. data/vendor/envjs/test/fixtures/js/script_error.js +2 -0
  58. data/vendor/envjs/test/html/events.html +171 -0
  59. data/vendor/envjs/test/html/iframe1.html +46 -0
  60. data/vendor/envjs/test/html/iframe1a.html +46 -0
  61. data/vendor/envjs/test/html/iframe2.html +45 -0
  62. data/vendor/envjs/test/html/iframe3.html +30 -0
  63. data/vendor/envjs/test/html/iframeN.html +57 -0
  64. data/vendor/envjs/test/html/malformed.html +181 -0
  65. data/vendor/envjs/test/html/scope.html +87 -0
  66. data/vendor/envjs/test/html/script.js +1 -0
  67. data/vendor/envjs/test/html/trivial.html +19 -0
  68. data/vendor/envjs/test/html/with_js.html +26 -0
  69. data/vendor/envjs/test/index.html +328 -0
  70. data/vendor/envjs/test/java-prototype.js +9 -0
  71. data/vendor/envjs/test/primary-tests.js +24 -0
  72. data/vendor/envjs/test/prototype-test.js +13 -0
  73. data/vendor/envjs/test/qunit.js +61 -0
  74. data/vendor/envjs/test/qunit/qunit/qunit.css +17 -0
  75. data/vendor/envjs/test/qunit/qunit/qunit.js +997 -0
  76. data/vendor/envjs/test/scope.rb +25 -0
  77. data/vendor/envjs/test/specs/dist/env.spec.js +1534 -0
  78. data/vendor/envjs/test/specs/envjs.spec.css +46 -0
  79. data/vendor/envjs/test/specs/parser/html.js +31 -0
  80. data/vendor/envjs/test/specs/parser/spec.html +40 -0
  81. data/vendor/envjs/test/specs/parser/xml.js +31 -0
  82. data/vendor/envjs/test/specs/qunit.bdd.js +210 -0
  83. data/vendor/envjs/test/specs/qunit.css +17 -0
  84. data/vendor/envjs/test/specs/qunit.js +997 -0
  85. data/vendor/envjs/test/specs/template/spec-0.js +31 -0
  86. data/vendor/envjs/test/specs/template/spec-1.js +31 -0
  87. data/vendor/envjs/test/specs/template/spec.html +40 -0
  88. data/vendor/envjs/test/specs/window/css.js +23 -0
  89. data/vendor/envjs/test/specs/window/dialog.js +25 -0
  90. data/vendor/envjs/test/specs/window/document.js +23 -0
  91. data/vendor/envjs/test/specs/window/event.js +25 -0
  92. data/vendor/envjs/test/specs/window/history.js +34 -0
  93. data/vendor/envjs/test/specs/window/location.js +34 -0
  94. data/vendor/envjs/test/specs/window/navigator.js +71 -0
  95. data/vendor/envjs/test/specs/window/screen.js +42 -0
  96. data/vendor/envjs/test/specs/window/spec.html +48 -0
  97. data/vendor/envjs/test/specs/window/timer.js +26 -0
  98. data/vendor/envjs/test/specs/window/window.js +53 -0
  99. data/vendor/envjs/test/specs/xhr/spec.html +47 -0
  100. data/vendor/envjs/test/specs/xhr/xhr.js +31 -0
  101. data/vendor/envjs/test/test.js +10 -0
  102. data/vendor/envjs/test/unit/dom.js +44 -0
  103. data/vendor/envjs/test/unit/elementmembers.js +60 -0
  104. data/vendor/envjs/test/unit/events.js +195 -0
  105. data/vendor/envjs/test/unit/fixtures/external_script.js +1 -0
  106. data/vendor/envjs/test/unit/iframe.js +234 -0
  107. data/vendor/envjs/test/unit/multi-window.js +212 -0
  108. data/vendor/envjs/test/unit/nu.validator.js +34 -0
  109. data/vendor/envjs/test/unit/onload.js +90 -0
  110. data/vendor/envjs/test/unit/parser.js +121 -0
  111. data/vendor/envjs/test/unit/prototypecompat.js +22 -0
  112. data/vendor/envjs/test/unit/proxy.js +6 -0
  113. data/vendor/envjs/test/unit/scope.js +209 -0
  114. data/vendor/envjs/test/unit/timer.js +115 -0
  115. data/vendor/envjs/test/unit/window.js +41 -0
  116. data/vendor/envjs/test/vendor/jQuery/README +2 -0
  117. data/vendor/envjs/test/vendor/prototype-1.6.0.3.js +4320 -0
  118. metadata +200 -0
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .yardoc/
3
+ doc/
4
+ TODO
5
+ .yard_issues.md
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright © 2009 Martin Aumont (mynyml)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,117 @@
1
+ .gitignore
2
+ LICENSE
3
+ Manifest
4
+ README.md
5
+ Rakefile
6
+ docs.watchr
7
+ harmony.gemspec
8
+ lib/harmony.rb
9
+ lib/harmony/page.rb
10
+ specs.watchr
11
+ test/harmony_test.rb
12
+ test/page_test.rb
13
+ test/test_helper.rb
14
+ vendor/README
15
+ vendor/envjs/README
16
+ vendor/envjs/bin/envjsrb
17
+ vendor/envjs/bin/jquery-1.2.6-test.js
18
+ vendor/envjs/bin/jquery-1.3.1-test.js
19
+ vendor/envjs/bin/jquery-1.3.2-test.js
20
+ vendor/envjs/bin/prototype-1.6.0.3-test.js
21
+ vendor/envjs/bin/prototype_1.6.0.3_tmpl.txt
22
+ vendor/envjs/bin/test-jquery.sh
23
+ vendor/envjs/bin/test-prototype.sh
24
+ vendor/envjs/bin/tidy
25
+ vendor/envjs/lib/envjs.rb
26
+ vendor/envjs/lib/envjs/env.js
27
+ vendor/envjs/lib/envjs/net.rb
28
+ vendor/envjs/lib/envjs/net/cgi.rb
29
+ vendor/envjs/lib/envjs/net/file.rb
30
+ vendor/envjs/lib/envjs/options.rb
31
+ vendor/envjs/lib/envjs/runtime.rb
32
+ vendor/envjs/lib/envjs/tempfile.rb
33
+ vendor/envjs/test/base64.js
34
+ vendor/envjs/test/call-load-test.js
35
+ vendor/envjs/test/data.js
36
+ vendor/envjs/test/debug.js
37
+ vendor/envjs/test/firebug/errorIcon.png
38
+ vendor/envjs/test/firebug/firebug.css
39
+ vendor/envjs/test/firebug/firebug.html
40
+ vendor/envjs/test/firebug/firebug.js
41
+ vendor/envjs/test/firebug/firebugx.js
42
+ vendor/envjs/test/firebug/infoIcon.png
43
+ vendor/envjs/test/firebug/warningIcon.png
44
+ vendor/envjs/test/fixtures/html/events.html
45
+ vendor/envjs/test/fixtures/html/iframe1.html
46
+ vendor/envjs/test/fixtures/html/iframe1a.html
47
+ vendor/envjs/test/fixtures/html/iframe2.html
48
+ vendor/envjs/test/fixtures/html/iframe3.html
49
+ vendor/envjs/test/fixtures/html/iframeN.html
50
+ vendor/envjs/test/fixtures/html/malformed.html
51
+ vendor/envjs/test/fixtures/html/scope.html
52
+ vendor/envjs/test/fixtures/html/trivial.html
53
+ vendor/envjs/test/fixtures/html/with_js.html
54
+ vendor/envjs/test/fixtures/images/icon-blue.png
55
+ vendor/envjs/test/fixtures/js/external_script.js
56
+ vendor/envjs/test/fixtures/js/script.js
57
+ vendor/envjs/test/fixtures/js/script_error.js
58
+ vendor/envjs/test/html/events.html
59
+ vendor/envjs/test/html/iframe1.html
60
+ vendor/envjs/test/html/iframe1a.html
61
+ vendor/envjs/test/html/iframe2.html
62
+ vendor/envjs/test/html/iframe3.html
63
+ vendor/envjs/test/html/iframeN.html
64
+ vendor/envjs/test/html/malformed.html
65
+ vendor/envjs/test/html/scope.html
66
+ vendor/envjs/test/html/script.js
67
+ vendor/envjs/test/html/trivial.html
68
+ vendor/envjs/test/html/with_js.html
69
+ vendor/envjs/test/index.html
70
+ vendor/envjs/test/java-prototype.js
71
+ vendor/envjs/test/primary-tests.js
72
+ vendor/envjs/test/prototype-test.js
73
+ vendor/envjs/test/qunit.js
74
+ vendor/envjs/test/qunit/qunit/qunit.css
75
+ vendor/envjs/test/qunit/qunit/qunit.js
76
+ vendor/envjs/test/scope.rb
77
+ vendor/envjs/test/specs/dist/env.spec.js
78
+ vendor/envjs/test/specs/envjs.spec.css
79
+ vendor/envjs/test/specs/parser/html.js
80
+ vendor/envjs/test/specs/parser/spec.html
81
+ vendor/envjs/test/specs/parser/xml.js
82
+ vendor/envjs/test/specs/qunit.bdd.js
83
+ vendor/envjs/test/specs/qunit.css
84
+ vendor/envjs/test/specs/qunit.js
85
+ vendor/envjs/test/specs/template/spec-0.js
86
+ vendor/envjs/test/specs/template/spec-1.js
87
+ vendor/envjs/test/specs/template/spec.html
88
+ vendor/envjs/test/specs/window/css.js
89
+ vendor/envjs/test/specs/window/dialog.js
90
+ vendor/envjs/test/specs/window/document.js
91
+ vendor/envjs/test/specs/window/event.js
92
+ vendor/envjs/test/specs/window/history.js
93
+ vendor/envjs/test/specs/window/location.js
94
+ vendor/envjs/test/specs/window/navigator.js
95
+ vendor/envjs/test/specs/window/screen.js
96
+ vendor/envjs/test/specs/window/spec.html
97
+ vendor/envjs/test/specs/window/timer.js
98
+ vendor/envjs/test/specs/window/window.js
99
+ vendor/envjs/test/specs/xhr/spec.html
100
+ vendor/envjs/test/specs/xhr/xhr.js
101
+ vendor/envjs/test/test.js
102
+ vendor/envjs/test/unit/dom.js
103
+ vendor/envjs/test/unit/elementmembers.js
104
+ vendor/envjs/test/unit/events.js
105
+ vendor/envjs/test/unit/fixtures/external_script.js
106
+ vendor/envjs/test/unit/iframe.js
107
+ vendor/envjs/test/unit/multi-window.js
108
+ vendor/envjs/test/unit/nu.validator.js
109
+ vendor/envjs/test/unit/onload.js
110
+ vendor/envjs/test/unit/parser.js
111
+ vendor/envjs/test/unit/prototypecompat.js
112
+ vendor/envjs/test/unit/proxy.js
113
+ vendor/envjs/test/unit/scope.js
114
+ vendor/envjs/test/unit/timer.js
115
+ vendor/envjs/test/unit/window.js
116
+ vendor/envjs/test/vendor/jQuery/README
117
+ vendor/envjs/test/vendor/prototype-1.6.0.3.js
@@ -0,0 +1,155 @@
1
+ Harmony
2
+ =======
3
+
4
+ .,ad88888888baa,
5
+ ,d8P""" ""9888ba.
6
+ .a8" ,ad88888888888a
7
+ aP' ,88888888888888888a
8
+ ,8" ,88888888888888888888,
9
+ ,8' (888888888( )888888888,
10
+ ,8' `8888888888888888888888
11
+ 8) `888888888888888888888,
12
+ 8 "8888888888888888888)
13
+ 8 `888888888888888888)
14
+ 8) "8888888888888888
15
+ (b "88888888888888'
16
+ `8, (8) 8888888888888)
17
+ "8a ,888888888888)
18
+ V8, d88888888888"
19
+ `8b, ,d8888888888P'
20
+ `V8a, ,ad8888888888P'
21
+ ""88888888888888888P"
22
+ """"""""""""
23
+
24
+ Summary
25
+ -------
26
+
27
+ Harmony provides a simple DSL to execute javascript + DOM code within ruby.
28
+
29
+ Install
30
+ -------
31
+
32
+ # manually install dependencies until rubygems issue is resolved
33
+ gem install stackdeck
34
+ gem install johnson -v "2.0.0.pre0"
35
+
36
+ gem install harmony
37
+
38
+ Examples
39
+ --------
40
+
41
+ ### Simple Javascript Parsing
42
+
43
+ require 'harmony'
44
+
45
+ page = Harmony::Page.new(<<-HTML)
46
+ <html>
47
+ <head>
48
+ <title>Foo</title>
49
+ </head>
50
+ <body></body>
51
+ </html>
52
+ HTML
53
+
54
+ page.execute_js("1+1") #=> 2
55
+ page.execute_js("document.title") #=> "Foo"
56
+
57
+ The Page object's `#execute_js` method (aliased as `#x` for convenience) takes a
58
+ string of javascript code, executes it and returns the last statement's value
59
+ (just like a ruby method).
60
+
61
+ ### Javascript Unit Tests
62
+
63
+ One interesting use of Harmony is to test your javascript code within your ruby
64
+ application's own tests (test/unit, minitest, RSpec, nanotest, etc). Which
65
+ consequently means that you can now run browser-less, fully command-line
66
+ based, DOM-javascript tests.
67
+
68
+ require 'test/unit'
69
+ require 'harmony'
70
+
71
+ class JavascriptTest < Test::Unit::TestCase
72
+ def setup
73
+ @page = Harmony::Page.new
74
+ @page.load('public/javascripts/foo.js')
75
+ end
76
+
77
+ def test_foo
78
+ assert_equal "world", @page.execute_js(<<-JS)
79
+ foo = new Foo;
80
+ foo.hello();
81
+ JS
82
+ end
83
+ end
84
+
85
+ ### DOM Handling
86
+
87
+ Don't be affraid to throw in your favorite client-side js framework, like
88
+ JQuery or Prototype. And notice that scripts linked to in `<script>` tags will
89
+ automatically get pulled in.
90
+
91
+ require 'harmony'
92
+
93
+ page = Harmony::Page.new(<<-HTML)
94
+ <html>
95
+ <head>
96
+ <script src="javascripts/jquery.js" type="text/javascript"></script>
97
+ </head>
98
+ <body>
99
+ <div id="widget">ohaie</div>
100
+ </body>
101
+ </html>
102
+ HTML
103
+
104
+ page.execute_js("$('#widget').innerHTML") #=> "ohaie"
105
+
106
+ ### Fetching Documents
107
+
108
+ Use `Harmony::Page.fetch(uri)` to create a page from a remote document.
109
+
110
+ require 'harmony'
111
+
112
+ page = Harmony::Page.fetch('http://example.com')
113
+ page.execute_js('document.title') #=> "Example Web Page"
114
+
115
+ `fetch` also accepts "file://" uris.
116
+
117
+ Acknowledgement
118
+ ---------------
119
+
120
+ Harmony is a thin DSL wrapper around three **amazing** libs, [Johnson][1],
121
+ [env.js][30] and [Envjs][2] . The authors of those libs have been doing a huge
122
+ amount of great work for quite a while, so please go recommend them on
123
+ WorkingWithRails right now and/or follow them on github:
124
+
125
+ [jbarnette][3], [tenderlove][4], [smparkes][5], [wycats][6], [matthewd][7], [thatcher][8], [jeresig][9]
126
+
127
+ Special thanks go to [smparkes][10] for his patient help, and for providing the
128
+ last puzzle pieces that made [everything][12] [work][11] [together][13].
129
+
130
+ Links
131
+ -----
132
+ * code: <http://github.com/mynyml/harmony>
133
+ * docs: <http://yardoc.org/docs/mynyml-harmony>
134
+ * wiki: <http://wiki.github.com/mynyml/harmony>
135
+ * bugs: <http://github.com/mynyml/harmony/issues>
136
+
137
+
138
+
139
+ YinYang ASCII art is © Normand Veilleux (nveilleuATemr1.emrDOTca)
140
+
141
+
142
+ [1]: http://github.com/jbarnette/johnson/
143
+ [2]: http://env-js.appspot.com/
144
+ [3]: http://www.workingwithrails.com/person/10668-john-barnette
145
+ [4]: http://github.com/tenderlove/
146
+ [5]: http://www.workingwithrails.com/person/11739-steven-parkes
147
+ [6]: http://www.workingwithrails.com/person/1805-yehuda-katz
148
+ [7]: http://www.workingwithrails.com/person/6221-matthew-draper
149
+ [8]: http://github.com/thatcher/
150
+ [9]: http://ejohn.org/
151
+ [10]: http://github.com/smparkes/
152
+ [11]: http://github.com/smparkes/env-js/commit/49abe259813a505b0761e6d31dde671344b5bc87#L0R279
153
+ [12]: http://groups.google.com/group/envjs/msg/4ac719f7db7912f5
154
+ [13]: http://gemcutter.org/gems/envjs
155
+ [30]: http://github.com/thatcher/env-js
@@ -0,0 +1,39 @@
1
+ def gem_opt
2
+ defined?(Gem) ? "-rubygems" : ""
3
+ end
4
+
5
+ # --------------------------------------------------
6
+ # Tests
7
+ # --------------------------------------------------
8
+ task(:default => "test:all")
9
+
10
+ namespace(:test) do
11
+
12
+ desc "Run all tests"
13
+ task(:all) do
14
+ exit system("ruby #{gem_opt} -I.:lib:test -e'%w( #{Dir['test/**/*_test.rb'].join(' ')} ).each {|p| require p }'")
15
+ end
16
+
17
+ desc "Run all tests on multiple ruby versions (requires rvm)"
18
+ task(:portability) do
19
+ versions = %w( 1.8.6 1.8.7 )
20
+ versions.each do |version|
21
+ system <<-BASH
22
+ bash -c 'source ~/.rvm/scripts/rvm;
23
+ rvm use #{version};
24
+ echo "--------- #{version} ----------";
25
+ rake -s test:all'
26
+ BASH
27
+ end
28
+ end
29
+ end
30
+
31
+ # --------------------------------------------------
32
+ # Docs
33
+ # --------------------------------------------------
34
+ desc "Generate YARD Documentation"
35
+ task :yardoc do
36
+ require 'yard'
37
+ YARD::CLI::Yardoc.run *%w( --no-private --no-highlight -o doc/yard --readme README.md --markup markdown - LICENSE )
38
+ end
39
+
@@ -0,0 +1,25 @@
1
+ # Run me with:
2
+ # $ watchr docs.watchr
3
+
4
+ require 'yard'
5
+ # --------------------------------------------------
6
+ # Rules
7
+ # --------------------------------------------------
8
+ watch( 'lib/.*\.rb' ) { yard }
9
+ watch( 'README.md' ) { yard }
10
+
11
+ # --------------------------------------------------
12
+ # Signal Handling
13
+ # --------------------------------------------------
14
+ Signal.trap('QUIT') { yard } # Ctrl-\
15
+ Signal.trap('INT' ) { abort("\n") } # Ctrl-C
16
+
17
+ # --------------------------------------------------
18
+ # Helpers
19
+ # --------------------------------------------------
20
+ def yard
21
+ print "Updating yardocs... "; STDOUT.flush
22
+ YARD::CLI::Yardoc.run *%w( --no-private --no-highlight -o doc/yard --readme README.md --markup markdown - LICENSE )
23
+ print "done\n"
24
+ end
25
+
@@ -0,0 +1,18 @@
1
+ require 'lib/harmony'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "harmony"
5
+ s.summary = "Javascript + DOM in your ruby, the simple way"
6
+ s.description = "yo dawg, i heard you liked developing web apps while using ruby on the cmd line, so i added javascript and a DOM in your ruby so you could js the dom without browser while you code in your editor"
7
+ s.author = "mynyml"
8
+ s.email = "mynyml@gmail.com"
9
+ s.homepage = "http://github.com/mynyml/harmony"
10
+ s.rubyforge_project = "harmony"
11
+ s.require_path = "lib"
12
+ s.version = Harmony::VERSION
13
+ s.files = File.read("Manifest").strip.split("\n")
14
+
15
+ s.add_dependency 'johnson', '2.0.0.pre0'
16
+ s.add_dependency 'stackdeck' #johnson dependency
17
+ s.add_development_dependency 'minitest'
18
+ end
@@ -0,0 +1,5 @@
1
+ module Harmony
2
+ VERSION = '0.5'
3
+
4
+ autoload :Page, 'harmony/page'
5
+ end
@@ -0,0 +1,123 @@
1
+ require 'pathname'
2
+ require 'tempfile'
3
+
4
+ require 'johnson/tracemonkey'
5
+ require Pathname(__FILE__).dirname.parent.parent + 'vendor/envjs/lib/envjs/runtime'
6
+
7
+ module Harmony
8
+ class Page
9
+
10
+ # Window factory
11
+ #
12
+ # @private
13
+ module Window #:nodoc:
14
+ extend self
15
+
16
+ # Cache the initial runtime. Parsing env.js (done automatically when
17
+ # Envjs::Runtime is extended) takes a while, so we only want to do this
18
+ # once.
19
+ #
20
+ # @private
21
+ BASE_RUNTIME = Johnson::Runtime.new
22
+ BASE_RUNTIME.extend(Envjs::Runtime)
23
+
24
+ def from_uri(uri)
25
+ BASE_RUNTIME.evaluate("window.open('#{uri}')")
26
+ end
27
+
28
+ def from_document(document)
29
+ Tempfile.open('harmony') {|f| f << document; @path = f.path }
30
+ from_uri("file://#{@path}")
31
+ end
32
+
33
+ def blank
34
+ from_uri('about:blank')
35
+ end
36
+ end
37
+
38
+ # Create page from remote document.
39
+ #
40
+ # @example
41
+ #
42
+ # Page.fetch('http://montrealrb.org')
43
+ # Page.fetch('http://localhost:3000')
44
+ # Page.fetch('file:///home/mynyml/www/foo/index.html')
45
+ #
46
+ # @param [String] uri
47
+ # uri to fetch document from
48
+ #
49
+ # @return [Page]
50
+ # new page object preloaded with fetched document
51
+ #
52
+ def self.fetch(uri)
53
+ page = new
54
+ page.instance_variable_set(:@window, Window.from_uri(uri))
55
+ page
56
+ end
57
+
58
+ # Create new page containing given document.
59
+ #
60
+ # @param [String] document
61
+ # HTML document. Defaults to an "about:blank" window, with the basic
62
+ # structure: `<html><head><title></title></head><body></body></html>`
63
+ #
64
+ def initialize(document=nil)
65
+ @window = Window.from_document(document) if document
66
+ end
67
+
68
+ # Load a javascript file in page's context
69
+ #
70
+ # @param [String] path
71
+ # path to js file
72
+ #
73
+ # @return [Page] self
74
+ #
75
+ def load(path)
76
+ window.load(path.to_s)
77
+ self
78
+ end
79
+
80
+ # Evaluate Javascript code within this page's context.
81
+ #
82
+ # @param [String] code
83
+ # javascript code to execute
84
+ #
85
+ # @return [Object]
86
+ # last javascript statement's value, cast to a ruby object
87
+ #
88
+ def execute_js(code)
89
+ window.evaluate(code)
90
+ end
91
+ alias :x :execute_js
92
+
93
+ # DOM document's `window` object. Equivalent to the return value of
94
+ # `page.execute_js('window')`
95
+ #
96
+ # @return [Object]
97
+ # window DOM object
98
+ #
99
+ def window
100
+ @window ||= Window.blank
101
+ end
102
+
103
+ # Convenience method, equivalent to the return value of
104
+ # `page.execute_js('window.document')`
105
+ #
106
+ # @return [Object]
107
+ # document DOM object
108
+ #
109
+ def document
110
+ window.document
111
+ end
112
+
113
+ # Page's document at current state
114
+ #
115
+ # @return [String]
116
+ # document as string
117
+ #
118
+ def to_s
119
+ document.innerHTML
120
+ end
121
+ end
122
+ end
123
+