Skip to content

Instantly share code, notes, and snippets.

@shanemcd
Forked from janxious/async_ruby_example.rb
Created August 25, 2014 20:27
Show Gist options
  • Save shanemcd/0312d0a2423db47c5f7f to your computer and use it in GitHub Desktop.
Save shanemcd/0312d0a2423db47c5f7f to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'doc_raptor'
TEN_SECONDS = 10
FIVE_MINUTES = 300
DocRaptor.api_key "YOUR_API_KEY_HERE"
pdf_html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en"><body><a href="http://google.com">google</a></body></html>'
def pretty_time; Time.now.strftime("%I:%M:%S %p"); end
def print_status(status = {}); puts "#{pretty_time} - Status: #{status['status']}";end
response = DocRaptor.create(:document_content => pdf_html,
:name => "doc_raptor_sample.pdf",
:document_type => "pdf",
:test => true,
:async => true)
status_id = response["status_id"]
# This timeout is 5 minutes.
# If there is no load on the background processors, this test should take (far) less than 20s. It's possible production will be backed up with real jobs, hence the timeout.
timeout_time = Time.now + FIVE_MINUTES
status = {'status' => 'queued'}
print_status(status)
while !(['completed','failed'].include? status['status']) && Time.now <= timeout_time
sleep TEN_SECONDS
status = DocRaptor.status(status_id)
print_status(status)
end
if status['status'] == 'completed'
file = DocRaptor.download(status['download_key'])
File.open("doc_raptor_sample.pdf", "w+b") do |f|
f.write file.response.body
end
puts "#{pretty_time} - File downloaded to doc_raptor_sample.pdf"
end
require 'rubygems'
require 'doc_raptor'
DocRaptor.api_key "YOUR_API_KEY_HERE"
xls_html = "<table name='My First Sheet'><tr><td>Cell 1</td><td>Cell 2</td></tr></table>"
File.open("doc_raptor_sample.xls", "w+b") do |f|
f.write DocRaptor.create(:document_content => xls_html,
:name => "docraptor_sample.xls",
:document_type => "xls",
:test => true)
end
pdf_html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en"><body><a href="http://google.com">google</a></body></html>'
File.open("doc_raptor_sample.pdf", "w+b") do |f|
f.write DocRaptor.create(:document_content => pdf_html,
:name => "docraptor_sample.pdf",
:document_type => "pdf",
:test => true)
end
class Doc
API_KEY = "YOUR_API_KEY_HERE"
require 'rubygems'
require 'httparty'
include HTTParty
base_uri "https://docraptor.com"
# returns a string that is the document data
def self.create(document_information)
post("/docs", :body => {:doc => document_information}, :basic_auth => {:username => API_KEY})
end
end
xls_xml = "<table name='My First Sheet'><tr><td>Cell 1</td><td>Cell 2</td></tr></table>"
File.open("doc_raptor_sample.xls", "wb") do |f|
f.write Doc.create(:document_content => xls_xml,
:name => "do_craptor_sample.xls",
:document_type => "xls",
:test => true)
end
pdf_html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en"><body><a href="http://google.com">google</a></body></html>'
File.open("doc_raptor_sample.pdf", "wb") do |f|
f.write Doc.create(:document_content => pdf_html,
:name => "doc_raptor_sample.pdf",
:document_type => "pdf",
:test => true)
end
%table{ :name => "Excel Example" }
%tr{ :style => "font-weight:bold;" }
%td Document Name
%td Document Type
%td Test Mode
%td Result Url
- @examples.each do |example|
%tr
%td=example.document_name
%td=example.document_type
%td=example.test
%td{ :style => "color:blue;" }=path_to_url(example.document.url)
#...
Rails::Initializer.run do |config|
#...
end
DocRaptor.api_key "YOUR_API_KEY_HERE"
class ExamplesController < ApplicationController
def index
@examples = Example.all
# Don't forget to reigster the xls/pdf mime types in config/initializers/mime_types.rb
respond_to do |format|
format.html
format.xls { doc_raptor_send }
format.pdf { doc_raptor_send }
end
end
def doc_raptor_send(options = { })
default_options = {
:name => controller_name,
:document_type => request.format.to_sym,
:test => ! Rails.env.production?,
}
options = default_options.merge(options)
options[:document_content] ||= render_to_string
ext = options[:document_type].to_sym
response = DocRaptor.create(options)
if response.code == 200
send_data response, :filename => "#{options[:name]}.#{ext}", :type => ext
else
render :inline => response.body, :status => response.code
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment