too_rendermonkey 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
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
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem "rest-client", "~> 1.6.1"
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.1"
12
+ gem "rcov", ">= 0"
13
+ gem "rails", "~> 3.0.3"
14
+ gem "sqlite3"
15
+ end
@@ -0,0 +1,88 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ actionmailer (3.0.3)
6
+ actionpack (= 3.0.3)
7
+ mail (~> 2.2.9)
8
+ actionpack (3.0.3)
9
+ activemodel (= 3.0.3)
10
+ activesupport (= 3.0.3)
11
+ builder (~> 2.1.2)
12
+ erubis (~> 2.6.6)
13
+ i18n (~> 0.4)
14
+ rack (~> 1.2.1)
15
+ rack-mount (~> 0.6.13)
16
+ rack-test (~> 0.5.6)
17
+ tzinfo (~> 0.3.23)
18
+ activemodel (3.0.3)
19
+ activesupport (= 3.0.3)
20
+ builder (~> 2.1.2)
21
+ i18n (~> 0.4)
22
+ activerecord (3.0.3)
23
+ activemodel (= 3.0.3)
24
+ activesupport (= 3.0.3)
25
+ arel (~> 2.0.2)
26
+ tzinfo (~> 0.3.23)
27
+ activeresource (3.0.3)
28
+ activemodel (= 3.0.3)
29
+ activesupport (= 3.0.3)
30
+ activesupport (3.0.3)
31
+ arel (2.0.4)
32
+ builder (2.1.2)
33
+ erubis (2.6.6)
34
+ abstract (>= 1.0.0)
35
+ ffi (1.0.1)
36
+ rake (>= 0.8.7)
37
+ git (1.2.5)
38
+ i18n (0.4.2)
39
+ jeweler (1.5.1)
40
+ bundler (~> 1.0.0)
41
+ git (>= 1.2.5)
42
+ rake
43
+ mail (2.2.10)
44
+ activesupport (>= 2.3.6)
45
+ i18n (~> 0.4.1)
46
+ mime-types (~> 1.16)
47
+ treetop (~> 1.4.8)
48
+ mime-types (1.16)
49
+ polyglot (0.3.1)
50
+ rack (1.2.1)
51
+ rack-mount (0.6.13)
52
+ rack (>= 1.0.0)
53
+ rack-test (0.5.6)
54
+ rack (>= 1.0)
55
+ rails (3.0.3)
56
+ actionmailer (= 3.0.3)
57
+ actionpack (= 3.0.3)
58
+ activerecord (= 3.0.3)
59
+ activeresource (= 3.0.3)
60
+ activesupport (= 3.0.3)
61
+ bundler (~> 1.0)
62
+ railties (= 3.0.3)
63
+ railties (3.0.3)
64
+ actionpack (= 3.0.3)
65
+ activesupport (= 3.0.3)
66
+ rake (>= 0.8.7)
67
+ thor (~> 0.14.4)
68
+ rake (0.8.7)
69
+ rcov (0.9.9)
70
+ rest-client (1.6.1)
71
+ mime-types (>= 1.16)
72
+ sqlite3 (0.1.1)
73
+ ffi (>= 0.6.3)
74
+ thor (0.14.4)
75
+ treetop (1.4.9)
76
+ polyglot (>= 0.3.1)
77
+ tzinfo (0.3.23)
78
+
79
+ PLATFORMS
80
+ ruby
81
+
82
+ DEPENDENCIES
83
+ bundler (~> 1.0.0)
84
+ jeweler (~> 1.5.1)
85
+ rails (~> 3.0.3)
86
+ rcov
87
+ rest-client (~> 1.6.1)
88
+ sqlite3
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,104 @@
1
+ # TooRendermonkey
2
+
3
+ ## PDF template generation plugin for Ruby on Rails
4
+
5
+ ### Master tracks Rails 3. Branch Rails 2 tracks >= Rails 2.3.5
6
+
7
+ This plugin allows the generation of pdf files from controllers using the pdf mime type. This plugin will not generate a pdf but will render the pages specified for the pdf format with the extension .pdf.erb as html and forward the request on to the RendermonkeyToo API which will generate this page as a PDF. This is useful for having a separate server generate PDF files from your Ruby on Rails application. This allows for custom PDF files to be generated for the specified page.
8
+
9
+ ### Installation as gem
10
+ gem install too_rendermonkey
11
+ rails generate too_rendermonkey
12
+
13
+ ### Installation as plugin
14
+ rails plugin install git://github.com/amkirwan/too_rendermonkey.git
15
+ rails generate too_rendermonkey
16
+
17
+ #In your Rails project Gemfile add
18
+ gem 'too_rendermonkey', :path=>'vendor/plugins/too_rendermonkey'
19
+
20
+
21
+ Generate will install a file in config/initializers/too_rendermonkey.rb. The following params must be specified in this file to generate PDFs using the RendermonkeyToo API.
22
+
23
+ * URI of the RendermonkeyToo instance which is running
24
+ * api_key given from the RendermonkeyToo API
25
+ * hash_key (secret key) given from the RendermonkeyToo API. This key is used to hash all the params sent to the RendermonkeyToo API when generating a PDF. Keep this key secure.
26
+
27
+
28
+ ### Example
29
+
30
+ Setup the Rails initializer config/initializers/too_rendermonkey.rb after running script/generate too_rendermonkey. Use your API info from RendermonkeyToo to configure. Make sure to keep the hash_key secure. Anytime you edit this file you will need to restart the server.
31
+
32
+ TooRendermonkey.config = {
33
+ :uri => "http://localhost:4567/generate",
34
+ :api_key => "abcdefg",
35
+ :hash_key => "abcdefg123456789"
36
+ }
37
+
38
+ In your controller within the action that you want to generate a PDF you would need to specify the following
39
+ *render :pdf, :pdf_template and :pdf_layout need to be specified*
40
+
41
+ class ExampleController < ApplicationController
42
+ def show
43
+ respond_to do |format|
44
+ format.html #index.html.erb
45
+ format.pdf do
46
+ render :pdf => "Phone List",
47
+ :pdf_layout => "reports.pdf.erb",
48
+ :pdf_template => "reports/phone_list.pdf.erb",
49
+ :render_options => {
50
+ :header_right => 'Page [page] of [toPage]',
51
+ :grayscale => true
52
+ }
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ In your view code you can specify the PDF path like in the example below. This will generate a PDF request.
59
+ <div>
60
+ <ul>
61
+ <li class="reports">Department Phone List</li>
62
+ <li class="reports-type"><%= link_to "Web", phone_list_reports_path %></li>
63
+ <li class="reports-type"><%= link_to "PDF", phone_list_reports_path(:pdf) %></li>
64
+ </ul>
65
+ </div>
66
+
67
+ For the PDF templates you will need to include the css within the document so that it can be formatted correctly since the document is being sent to a server to be generated.
68
+
69
+ Use the RendermonkeyToo helper *stylesheet_tag_pdf* within your pdf_layout to accomplish this. Specify the name of the pdf file and it will automatically read in your css into the document.
70
+
71
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
72
+ <head>
73
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
74
+ <%= stylesheet_tag_pdf "reports_pdf" %>
75
+ </head>
76
+
77
+ The pdf_template can have links to partial .html.erb files so you do not need to duplicate all your pages. You must however add in a .html extension to any partials you are includeing.
78
+
79
+ <div class="list-wrap">
80
+ <%= render(:partial => "col_one_subheading.html", :object => "Faculty") %>
81
+ <div class="section-wrap">
82
+ <%= render(:partial => "report_column_one.html", :object => @all_faculty_column_one) %>
83
+ <%= render(:partial => "report_column_two.html", :object => @all_faculty_column_two) %>
84
+ <%= render(:partial => "report_column_three.html", :object => @all_faculty_column_three) %>
85
+ </div>
86
+ </div>
87
+
88
+ ### Plugin Params - Troubleshooting
89
+
90
+ When generating a request the plugin sends the following params to the TooRendermonkey API
91
+
92
+ * api_key (your api_key)
93
+ * timestamp (Time request generated, iso8601)
94
+ * page - (HTML to render to PDF)
95
+ * name - (Name of PDF file) Only Param that is optional
96
+ * signature - (SHA256 HMAC generated using your hash_key plus all the params of the current request )
97
+
98
+ If the request generated takes longer than 5 minutes to arrive at the Rendermonkey API server it will be rejected. This is to prevent someone from attempting to replay your request.
99
+
100
+ All of this will be handled for you by the plugin. If there are any errors with your params the Rendermonkey API will respond with a message stating the reason for failure.
101
+
102
+
103
+
104
+ Copyright (c) 2010 [Anthony Kirwan], released under the MIT license
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "too_rendermonkey"
16
+ gem.homepage = "http://github.com/amkirwan/too_rendermonkey"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Forward HTML renderer to RendemonkeyToo server}
19
+ gem.email = "amkirwan@gmail.com"
20
+ gem.authors = ["Anthony Kirwan"]
21
+ gem.description = %Q{This plugin allows the generation of pdf files from controllers using the pdf mime type.
22
+ This plugin will not generate a pdf but will render the pages specified for the pdf format
23
+ with the extension .pdf.erb as html and forward the request on to the RendermonkeyToo API
24
+ which will generate this page as a PDF. This is useful for having a separate server generate
25
+ PDF files from your Ruby on Rails application. This allows for custom PDF files to be generated
26
+ for the specified page.}
27
+ gem.add_runtime_dependency 'rest-client', '~> 1.6.1'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ require 'rcov/rcovtask'
39
+ Rcov::RcovTask.new do |test|
40
+ test.libs << 'test'
41
+ test.pattern = 'test/**/*_test.rb'
42
+ test.verbose = true
43
+ end
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "too_rendermonkey #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates a initializer to specify the api_key and hash_key(secret key) to generate PDF requests on the rendermonkey_too server.
3
+
4
+ Example:
5
+ rails generate too_rendermonkey
6
+
7
+ This will create:
8
+ config/initializers/too_rendermonkey.rb
@@ -0,0 +1,8 @@
1
+ # You probably do not want to keep this in your repo when using the secret_key
2
+
3
+ ## Generator Path
4
+ TooRendermonkey.configure = {
5
+ #:uri => "https://rendermonkey-too.heroku.com/generate",
6
+ #:api_key => "example1234example",
7
+ #:hash_key => "example1234example"
8
+ }
@@ -0,0 +1,11 @@
1
+ class TooRendermonkeyGenerator < Rails::Generators::Base
2
+
3
+ def self.source_root
4
+ @source_root ||= File.expand_path('../templates', __FILE__)
5
+ end
6
+
7
+ def copy_config_files
8
+ copy_file("too_rendermonkey.rb", "config/initializers/too_rendermonkey.rb")
9
+ end
10
+
11
+ end
@@ -0,0 +1,7 @@
1
+ module TooRendermonkey
2
+ require 'too_rendermonkey/railtie' if defined?(Rails)
3
+
4
+ class << self
5
+ attr_accessor :configure
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module TooRendermonkey
2
+
3
+ class ConfigureError < StandardError
4
+ end
5
+
6
+ end
@@ -0,0 +1,76 @@
1
+ require 'openssl'
2
+ require 'digest/sha2'
3
+ require 'base64'
4
+ require 'time'
5
+ require 'rest-client'
6
+ require 'exceptions'
7
+
8
+ module PDFGenerator
9
+
10
+ attr_reader :pdf_params
11
+
12
+ def make_pdf_erb(pdf_name, options = {})
13
+ check_configure
14
+ options[:pdf_layout] ||= false
15
+ options[:pdf_template] ||= File.join(controller_path, action_name)
16
+ page = render_to_string(:template => options[:pdf_template], :layout => options[:pdf_layout])
17
+ params = generate_params(pdf_name, options, page)
18
+ begin
19
+ response = RestClient.post TooRendermonkey.configure[:uri], params
20
+ send_data response, :type => 'application/pdf', :disposition => "#{response.headers[:content_disposition]}"
21
+ rescue => e
22
+ logger.info '*'*15 + "ERROR GENERATING PDF: #{e.http_body}" + '*'*15
23
+ render :file => "public/500.html", :status => 500
24
+ end
25
+ end
26
+
27
+ def check_configure
28
+ keys = %w(uri api_key hash_key)
29
+ keys.each do |key|
30
+ raise TooRendermonkey::ConfigureError, "Configure Error: #{key.to_s}" unless TooRendermonkey.configure.has_key?(key.to_sym)
31
+ end
32
+ end
33
+
34
+ def generate_params(pdf_name, options, page)
35
+ @pdf_params = { "name" => pdf_name,
36
+ "page" => page,
37
+ "api_key" => TooRendermonkey.configure[:api_key],
38
+ "timestamp" => Time.now.utc.iso8601 }
39
+ process_render_options(options)
40
+ @pdf_params["signature"] = generate_signature(@pdf_params)
41
+ @pdf_params
42
+ end
43
+
44
+ def process_render_options(options)
45
+ if !options[:render_options].nil? && !options[:render_options].empty?
46
+ options[:render_options].each do |key, value|
47
+ @pdf_params[key.to_s] = value.to_s
48
+ end
49
+ end
50
+ end
51
+
52
+ def generate_signature(params)
53
+ c_q = canonical_querystring(params)
54
+
55
+ if TooRendermonkey.configure[:hash_key].size == 44
56
+ #logger.info '*'*10 + "Using SHA256"
57
+ hashtype = 'SHA256'
58
+ elsif TooRendermonkey.configure[:hash_key].size == 89
59
+ #logger.info '*'*10 + "Using SHA512"
60
+ hashtype = 'SHA512'
61
+ elsif TooRendermonkey.configure[:hash_key].size == 28
62
+ #logger.info '*'*10 + "Using SHA1"
63
+ hashtype = 'SHA1'
64
+ else
65
+ raise TooRendermonkey::ConfigureError, "Could not match has_key type"
66
+ end
67
+
68
+ digest = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new(hashtype), TooRendermonkey.configure[:hash_key], c_q)
69
+ Base64.encode64(digest).chomp
70
+ end
71
+
72
+ def canonical_querystring(params)
73
+ params.sort.collect do |key, value| [key.to_s, value.to_s].join('=') end.join('&')
74
+ end
75
+
76
+ end
@@ -0,0 +1,31 @@
1
+ require 'too_rendermonkey'
2
+ require 'logger'
3
+
4
+ $:.unshift File.join(File.dirname(__FILE__))
5
+ require "pdf_generator"
6
+ require "too_rendermonkey_css"
7
+
8
+
9
+ module Rails
10
+ module TooRendermonkey
11
+ class Railtie < ::Rails::Railtie
12
+ initializer "add pdf renderer" do
13
+ ActionController::Renderers.add :pdf do |pdf_name, options|
14
+ begin
15
+ make_pdf_erb(pdf_name, options)
16
+ rescue => e
17
+ logger.info e.message
18
+ render :file => "public/500.html", :status => 500
19
+ end
20
+ end
21
+ Mime::Type.register 'application/pdf', :pdf
22
+ end
23
+
24
+ config.to_prepare do
25
+ ActionController::Base.send :include, PDFGenerator
26
+ ActionView::Base.send :include, TooRendermonkeyCss
27
+ end
28
+
29
+ end
30
+ end
31
+ end