too_rendermonkey 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. data/Gemfile +15 -0
  2. data/Gemfile.lock +88 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +104 -0
  5. data/Rakefile +55 -0
  6. data/VERSION +1 -0
  7. data/install.rb +1 -0
  8. data/lib/generators/too_rendermonkey/USAGE +8 -0
  9. data/lib/generators/too_rendermonkey/templates/too_rendermonkey.rb +8 -0
  10. data/lib/generators/too_rendermonkey/too_rendermonkey_generator.rb +11 -0
  11. data/lib/too_rendermonkey.rb +7 -0
  12. data/lib/too_rendermonkey/exceptions.rb +6 -0
  13. data/lib/too_rendermonkey/pdf_generator.rb +76 -0
  14. data/lib/too_rendermonkey/railtie.rb +31 -0
  15. data/lib/too_rendermonkey/too_rendermonkey_css.rb +18 -0
  16. data/test/rails_test/.gitignore +4 -0
  17. data/test/rails_test/Gemfile +7 -0
  18. data/test/rails_test/Gemfile.lock +89 -0
  19. data/test/rails_test/README +256 -0
  20. data/test/rails_test/Rakefile +7 -0
  21. data/test/rails_test/app/controllers/application_controller.rb +3 -0
  22. data/test/rails_test/app/controllers/pdf_render_controller.rb +12 -0
  23. data/test/rails_test/app/helpers/application_helper.rb +2 -0
  24. data/test/rails_test/app/views/layouts/application.html.erb +14 -0
  25. data/test/rails_test/app/views/layouts/reports_layout.pdf.erb +14 -0
  26. data/test/rails_test/app/views/reports/report.pdf.erb +1 -0
  27. data/test/rails_test/config.ru +4 -0
  28. data/test/rails_test/config/application.rb +42 -0
  29. data/test/rails_test/config/boot.rb +13 -0
  30. data/test/rails_test/config/database.yml +22 -0
  31. data/test/rails_test/config/environment.rb +5 -0
  32. data/test/rails_test/config/environments/development.rb +26 -0
  33. data/test/rails_test/config/environments/production.rb +49 -0
  34. data/test/rails_test/config/environments/test.rb +35 -0
  35. data/test/rails_test/config/initializers/backtrace_silencers.rb +7 -0
  36. data/test/rails_test/config/initializers/inflections.rb +10 -0
  37. data/test/rails_test/config/initializers/mime_types.rb +5 -0
  38. data/test/rails_test/config/initializers/secret_token.rb +7 -0
  39. data/test/rails_test/config/initializers/session_store.rb +8 -0
  40. data/test/rails_test/config/initializers/too_rendermonkey.rb +8 -0
  41. data/test/rails_test/config/locales/en.yml +5 -0
  42. data/test/rails_test/config/routes.rb +59 -0
  43. data/test/rails_test/db/schema.rb +15 -0
  44. data/test/rails_test/db/seeds.rb +7 -0
  45. data/test/rails_test/doc/README_FOR_APP +2 -0
  46. data/test/rails_test/lib/tasks/.gitkeep +0 -0
  47. data/test/rails_test/public/404.html +26 -0
  48. data/test/rails_test/public/422.html +26 -0
  49. data/test/rails_test/public/500.html +26 -0
  50. data/test/rails_test/public/favicon.ico +0 -0
  51. data/test/rails_test/public/images/rails.png +0 -0
  52. data/test/rails_test/public/index.html +239 -0
  53. data/test/rails_test/public/javascripts/application.js +2 -0
  54. data/test/rails_test/public/javascripts/controls.js +965 -0
  55. data/test/rails_test/public/javascripts/dragdrop.js +974 -0
  56. data/test/rails_test/public/javascripts/effects.js +1123 -0
  57. data/test/rails_test/public/javascripts/prototype.js +6001 -0
  58. data/test/rails_test/public/javascripts/rails.js +175 -0
  59. data/test/rails_test/public/robots.txt +5 -0
  60. data/test/rails_test/public/stylesheets/.gitkeep +0 -0
  61. data/test/rails_test/public/stylesheets/reports_pdf.css +3 -0
  62. data/test/rails_test/script/rails +6 -0
  63. data/test/rails_test/test/functional/abc.pdf +0 -0
  64. data/test/rails_test/test/functional/pdf_render_test.rb +83 -0
  65. data/test/rails_test/test/test_helper.rb +15 -0
  66. data/test/rails_test/test/unit/initializer_test.rb +23 -0
  67. data/test/rails_test/test/unit/pdf_generator_test.rb +97 -0
  68. data/test/rails_test/test/unit/too_rendermonkey_css_test.rb +21 -0
  69. data/test/rails_test/test/unit/too_rendermonkey_test.rb +15 -0
  70. data/test/rails_test/vendor/plugins/.gitkeep +0 -0
  71. data/too_rendermonkey.gemspec +160 -0
  72. data/uninstall.rb +1 -0
  73. metadata +265 -0
@@ -0,0 +1,175 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+ })();
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
@@ -0,0 +1,3 @@
1
+ .bold {
2
+ font-weight: bold;
3
+ }
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,83 @@
1
+ require 'test_helper'
2
+ require 'rest-client'
3
+
4
+ class PdfRenderTest < ActionController::TestCase
5
+
6
+ def setup
7
+ @controller = PDFRenderController.new
8
+ @request = ActionController::TestRequest.new
9
+ @response = ActionController::TestResponse.new
10
+ @filename = "abc"
11
+
12
+ TooRendermonkey.configure = {
13
+ :uri => "http://localhost:4567/generate",
14
+ :api_key => "835a3161dc4e71b",
15
+ :hash_key => "sQQTe93eWcpV4Gr5HDjKUh8vu2aNDOvn3+suH1Tc4P4="
16
+ }
17
+ end
18
+
19
+ def teardown
20
+ @controller = nil
21
+ @request = nil
22
+ @response = nil
23
+ end
24
+
25
+ def request_success
26
+ stub_request(:post, "http://localhost:4567/generate").to_return(
27
+ :body => File.open('./test/functional/abc.pdf'),
28
+ :status => 200,
29
+ :headers => { 'Content-Length' => File.open('./test/functional/abc.pdf').size.to_s,
30
+ 'Content-Disposition' => "attachment; filename=#{@filename}"})
31
+ end
32
+
33
+ def request_fail
34
+ stub_request(:post, "http://localhost:4567/generate").to_return(
35
+ :status => 412,
36
+ :body => "Incorrect or missing parameters")
37
+ end
38
+
39
+ test "should render with pdf format" do
40
+ request_success
41
+ get :render_pdf, :format => :pdf
42
+ assert_response :success
43
+ end
44
+
45
+ test "should render with css" do
46
+ css_style = '<style type="text/css">
47
+ .bold { font-weight: bold; }
48
+ </style>'
49
+ request_success
50
+ get :render_pdf, :format => :pdf
51
+ assert @controller.pdf_params["page"].gsub(/\s+/, "").include?(css_style.gsub(/\s+/, ""))
52
+ end
53
+
54
+ # Failures
55
+ test "should should error message from server on failed request" do
56
+ request_fail
57
+ begin
58
+ RestClient.post "http://localhost:4567/generate", {}
59
+ raise
60
+ rescue RestClient::PreconditionFailed => e
61
+ assert_equal e.http_body, "Incorrect or missing parameters"
62
+ end
63
+ end
64
+
65
+ test "should render 500 on error" do
66
+ request_fail
67
+ get :render_pdf, :format => :pdf
68
+ assert_response :error
69
+ end
70
+
71
+ test "should render 500 on missing configure key" do
72
+ TooRendermonkey.configure.delete(:api_key)
73
+ get :render_pdf, :format => :pdf
74
+ assert_response :error
75
+ end
76
+
77
+ test "should render 500 on incorrect hash_key" do
78
+ TooRendermonkey.configure[:hash_key] = "abcdefg"
79
+ get :render_pdf, :format => :pdf
80
+ assert_response :error
81
+ end
82
+
83
+ end
@@ -0,0 +1,15 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rails/test_help'
4
+ require 'webmock/test_unit'
5
+ require 'logger'
6
+
7
+ class ActiveSupport::TestCase
8
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
9
+ #
10
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
11
+ # -- they do not yet inherit this setting
12
+ fixtures :all
13
+
14
+ # Add more helper methods to be used by all tests here...
15
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+ require 'active_support'
3
+
4
+ class InitializerTest < ActiveSupport::TestCase
5
+
6
+ test "too rendermonkey initialize" do
7
+ assert_equal 'application/pdf', Mime.const_get(:PDF)
8
+ end
9
+
10
+ test "PDFGenerator included in ActionController::Base" do
11
+ assert ActionController::Base.included_modules.include?(PDFGenerator)
12
+ end
13
+
14
+ test "TooRendermonkeyCss included in ActionView::Base" do
15
+ assert ActionView::Base.included_modules.include?(TooRendermonkeyCss)
16
+ end
17
+
18
+ test "too_rendermonkey.rb initializer file is loaded" do
19
+ assert "Hash", TooRendermonkey.configure.class.to_s
20
+ assert_equal TooRendermonkey.configure.length, 3
21
+ end
22
+
23
+ end
@@ -0,0 +1,97 @@
1
+ require 'test_helper'
2
+
3
+ class PDFGeneratorTest < ActiveSupport::TestCase
4
+ include PDFGenerator
5
+
6
+ ## add required tests
7
+ ## add configure tests
8
+
9
+ def setup
10
+ @pdf_name = "pdf name"
11
+ @options = { :pdf_layout => "reports_layout.pdf.erb", :pdf_template => "reports/report.pdf.erb",
12
+ :render_options => {
13
+ :header_right => 'Page [page] of [toPage]',
14
+ :grayscale => true,
15
+ :page_size => 'Letter'}
16
+ }
17
+ @page = "<html><head><head><body><b>Hello</b> World</body></html>"
18
+
19
+ TooRendermonkey.configure = {
20
+ :uri => "http://localhost:4567/generate",
21
+ :api_key => "835a3161dc4e71b",
22
+ :hash_key => "sQQTe93eWcpV4Gr5HDjKUh8vu2aNDOvn3+suH1Tc4P4="
23
+ }
24
+ end
25
+
26
+ test "check configuration has values" do
27
+ generate_params(@pdf_name, @options, @page)
28
+
29
+ assert TooRendermonkey.configure[:uri], "Configure has uri"
30
+ assert TooRendermonkey.configure[:api_key], "Configure has api_key"
31
+ assert TooRendermonkey.configure[:hash_key], "Configure has hash_key"
32
+ end
33
+
34
+ test "should raise configure error on missing key" do
35
+ TooRendermonkey.configure.delete(:hash_key)
36
+ exception = assert_raise(TooRendermonkey::ConfigureError) { check_configure }
37
+ assert_match "Configure Error: hash_key", exception.message
38
+ end
39
+
40
+ test "should generate default params" do
41
+ pdf_params = generate_params(@pdf_name, @options, @page)
42
+ assert_equal @pdf_name, pdf_params["name"]
43
+ assert_equal @page, pdf_params["page"]
44
+ assert_equal TooRendermonkey.configure[:api_key], pdf_params["api_key"]
45
+ assert_in_delta Time.now.utc, Time.parse(pdf_params["timestamp"]), 300
46
+ assert_equal 44, pdf_params["signature"].length,
47
+ end
48
+
49
+ test "should process render options" do
50
+ pdf_params = generate_params(@pdf_name, @options, @page)
51
+ assert_equal 'Page [page] of [toPage]', pdf_params["header_right"]
52
+ assert_equal "true", pdf_params["grayscale"]
53
+ assert_equal "Letter", pdf_params["page_size"]
54
+ end
55
+
56
+ test "pdf_params all keys should be strings" do
57
+ pdf_params = generate_params(@pdf_name, @options, @page)
58
+ pdf_params.each_key { |key| assert_equal String, key.class, "Key is not a String" }
59
+ end
60
+
61
+ test "pdf_params all values should be strings" do
62
+ pdf_params = generate_params(@pdf_name, @options, @page)
63
+ pdf_params.each_value { |value| assert_equal String, value.class, "Value is not a String" }
64
+ end
65
+
66
+ test "canonical_querystring" do
67
+ pdf_params = generate_params(@pdf_name, @options, @page)
68
+ pdf_params["timestamp"] = "2010-12-07T04:37:59Z"
69
+ pdf_params.delete("signature")
70
+ match_string = "api_key=835a3161dc4e71b&grayscale=true&header_right=Page [page] of [toPage]&name=pdf name&page=<html><head><head><body><b>Hello</b> World</body></html>&page_size=Letter&timestamp=2010-12-07T04:37:59Z"
71
+ assert_equal match_string, canonical_querystring(pdf_params)
72
+ end
73
+
74
+ test "generate signature SHA256" do
75
+ # returned in base64
76
+ assert_equal 44, generate_signature(@options).length
77
+ end
78
+
79
+ test "generate signature SHA512" do
80
+ # returned in base64
81
+ TooRendermonkey.configure[:hash_key] = "sQQTe93eWcpV4Gr5HDjKUh8vu2aNDOvn3+suH1Tc4P4sQQTe93eWcpV4Gr5HDjKUh8vu2aNDOvn3+suH1Tc4P4aa="
82
+ assert_equal 89, generate_signature(@options).length
83
+ end
84
+
85
+ test "generate signature SHA1" do
86
+ # returned in base64
87
+ TooRendermonkey.configure[:hash_key] = "sQQTe93eWcpV4Gr5HDjKUh8vu2a="
88
+ assert_equal 28, generate_signature(@options).length
89
+ end
90
+
91
+ test "should raise TooRendermonkey::ConfigureError on incorrect hash_key length" do
92
+ TooRendermonkey.configure[:hash_key] = "sQQTe93eWcpV4Gr5HDjKUh8vu2aN="
93
+ exception = assert_raise(TooRendermonkey::ConfigureError) { generate_signature(@options) }
94
+ assert_match "Could not match has_key type", exception.message
95
+ end
96
+
97
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ class TooRendermonkeyCssTest < ActiveSupport::TestCase
4
+ include TooRendermonkeyCss
5
+
6
+ def setup
7
+ Rails.logger =Logger.new(STDOUT)
8
+ end
9
+
10
+ test "should return css style that is html safe" do
11
+ css_file = ActionView::Base.new.stylesheet_tag_pdf("reports_pdf")
12
+ assert lambda {css_file.html_safe?}
13
+ end
14
+
15
+ test "should return empty css if it cannot find the file" do
16
+ css_file = ActionView::Base.new.stylesheet_tag_pdf("does_not_exist.pdf")
17
+ assert_equal "<style type=\"text/css\"></style>", css_file.to_s
18
+ assert lambda {css_file.html_safe?}
19
+ end
20
+
21
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class TooRendermonkeyTest < ActiveSupport::TestCase
4
+
5
+ test "should be able to write to configure" do
6
+ TooRendermonkey.configure = "Foobar"
7
+ assert TooRendermonkey.configure, "Foobar"
8
+ end
9
+
10
+
11
+
12
+ end
13
+
14
+
15
+