Module: PDFGenerator

Defined in:
lib/too_rendermonkey/pdf_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pdf_paramsObject (readonly)

Returns the value of attribute pdf_params.



10
11
12
# File 'lib/too_rendermonkey/pdf_generator.rb', line 10

def pdf_params
  @pdf_params
end

Instance Method Details

#canonical_querystring(params) ⇒ Object



72
73
74
# File 'lib/too_rendermonkey/pdf_generator.rb', line 72

def canonical_querystring(params)
  params.sort.collect do |key, value| [key.to_s, value.to_s].join('=') end.join('&')
end

#check_configureObject



27
28
29
30
31
32
# File 'lib/too_rendermonkey/pdf_generator.rb', line 27

def check_configure 
  keys = %w(uri api_key hash_key)     
  keys.each do |key|
     raise TooRendermonkey::ConfigureError, "Configure Error: #{key.to_s}" unless TooRendermonkey.configure.has_key?(key.to_sym)
  end   
end

#generate_params(pdf_name, options, page) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/too_rendermonkey/pdf_generator.rb', line 34

def generate_params(pdf_name, options, page)
  @pdf_params = { "name" => pdf_name,
                  "page" => page,
                  "api_key" => TooRendermonkey.configure[:api_key],
                  "timestamp" => Time.now.utc.iso8601 }
  process_render_options(options)
  @pdf_params["signature"] = generate_signature(@pdf_params)
  @pdf_params
end

#generate_signature(params) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/too_rendermonkey/pdf_generator.rb', line 52

def generate_signature(params)
  c_q = canonical_querystring(params)

  if TooRendermonkey.configure[:hash_key].size == 44
		#logger.info '*'*10 + "Using SHA256"
		hashtype = 'SHA256'
	elsif TooRendermonkey.configure[:hash_key].size == 89
		#logger.info '*'*10 + "Using SHA512"
		hashtype = 'SHA512'		
	elsif  TooRendermonkey.configure[:hash_key].size == 28
		#logger.info '*'*10 + "Using SHA1"
		hashtype = 'SHA1'		
	else 
		raise TooRendermonkey::ConfigureError, "Could not match has_key type"
	end

  digest = OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new(hashtype), TooRendermonkey.configure[:hash_key], c_q)
  Base64.encode64(digest).chomp 
end

#make_pdf_erb(pdf_name, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/too_rendermonkey/pdf_generator.rb', line 12

def make_pdf_erb(pdf_name, options = {})  
  check_configure   
  options[:pdf_layout] ||= false
  options[:pdf_template] ||= File.join(controller_path, action_name)
  page = render_to_string(:template => options[:pdf_template], :layout => options[:pdf_layout])
  params = generate_params(pdf_name, options, page)     
  begin
    response = RestClient.post TooRendermonkey.configure[:uri], params
    send_data response, :type => 'application/pdf', :disposition => "#{response.headers[:content_disposition]}"
  rescue => e     
    logger.info '*'*15 + "ERROR GENERATING PDF: #{e.http_body}" + '*'*15 
    render :file => "public/500.html", :status => 500
  end
end

#process_render_options(options) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/too_rendermonkey/pdf_generator.rb', line 44

def process_render_options(options) 
  if !options[:render_options].nil? && !options[:render_options].empty?
    options[:render_options].each do |key, value|
      @pdf_params[key.to_s] = value.to_s
    end
  end
end