class DocRaptor

Constants

VERSION

Attributes

download_key[RW]
status_id[RW]

Public Class Methods

api_key(key = nil) click to toggle source
# File lib/doc_raptor.rb, line 15
def self.api_key(key = nil)
  default_options[:api_key] = key ? key : default_options[:api_key] || ENV["DOCRAPTOR_API_KEY"]
  default_options[:api_key] || raise(DocRaptorError::NoApiKeyProvidedError.new("No API key provided"))
end
create(options = { }) { |f, response| ... } click to toggle source

when given a block, hands the block a TempFile of the resulting document otherwise, just returns the response

# File lib/doc_raptor.rb, line 31
def self.create(options = { })
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  if options[:document_content].blank? && options[:document_url].blank?
    raise DocRaptorError::NoContentError.new("must supply :document_content or :document_url")
  end

  default_options = {
    :name                       => "default",
    :document_type              => "pdf",
    :test                       => false,
    :async                      => false,
    :raise_exception_on_failure => false
  }
  options = default_options.merge(options)
  raise_exception_on_failure = options[:raise_exception_on_failure]
  options.delete :raise_exception_on_failure

  # HOTFIX
  # convert safebuffers to plain old strings so the gsub'ing that has to occur
  # for url encoding works
  # Broken by: https://github.com/rails/rails/commit/1300c034775a5d52ad9141fdf5bbdbb9159df96a#activesupport/lib/active_support/core_ext/string/output_safety.rb
  # Discussion: https://github.com/rails/rails/issues/1555
  if defined?(ActiveSupport) && defined?(ActiveSupport::SafeBuffer)
    options.map{|k,v| options[k] = options[k].to_str if options[k].is_a?(ActiveSupport::SafeBuffer)}
  end
  # /HOTFIX

  response = post("/docs", :body => {:doc => options}, :basic_auth => {:username => api_key})

  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentCreationFailure.new response.body, response.code
  end

  if response.success? && options[:async]
    self.status_id = response.parsed_response["status_id"]
  end

  if block_given?
    ret_val = nil
    Tempfile.open("docraptor", :encoding => "ascii-8bit") do |f|
      f.sync = true
      f.write(response.body)
      f.rewind

      ret_val = yield f, response
    end
    ret_val
  else
    response
  end
end
create!(options = {}) click to toggle source
# File lib/doc_raptor.rb, line 24
def self.create!(options = {})
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  self.create(options.merge({:raise_exception_on_failure => true}))
end
disable_agent_tracking() click to toggle source
# File lib/doc_raptor.rb, line 20
def self.disable_agent_tracking
  default_options[:headers].delete("User-Agent")
end
download(key = self.download_key, raise_exception_on_failure = false) { |f, response| ... } click to toggle source
# File lib/doc_raptor.rb, line 157
def self.download(key = self.download_key, raise_exception_on_failure = false)
  response = get("/download/#{key}")

  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentDownloadFailure.new response.body, response.code
  end

  if block_given?
    ret_val = nil
    Tempfile.open("docraptor") do |f|
      f.sync = true
      f.write(response.body)
      f.rewind

      ret_val = yield f, response
    end
    ret_val
  else
    response
  end
end
download!(key = self.download_key) click to toggle source
# File lib/doc_raptor.rb, line 153
def self.download!(key = self.download_key)
  self.download(key, true)
end
list_doc_logs(options = { }) click to toggle source
# File lib/doc_raptor.rb, line 113
def self.list_doc_logs(options = { })
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  default_options = {
    :page                       => 1,
    :per_page                   => 100,
    :raise_exception_on_failure => false,
    :output_format              => "xml",
  }

  options = default_options.merge(options)
  output_format = options.delete(:output_format)
  raise_exception_on_failure = options.delete(:raise_exception_on_failure)

  response = get("/doc_logs.#{output_format}", :query => options, :basic_auth => { :username => api_key })
  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentListingFailure.new response.body, response.code
  end

  response
end
list_doc_logs!(options = { }) click to toggle source
# File lib/doc_raptor.rb, line 108
def self.list_doc_logs!(options = { })
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  self.list_doc_logs(options.merge(:raise_exception_on_failure => true))
end
list_docs(options = { }) click to toggle source
# File lib/doc_raptor.rb, line 88
def self.list_docs(options = { })
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  default_options = {
    :page                       => 1,
    :per_page                   => 100,
    :raise_exception_on_failure => false,
    :output_format              => "xml",
  }
  options = default_options.merge(options)
  output_format = options.delete(:output_format)
  raise_exception_on_failure = options.delete(:raise_exception_on_failure)

  response = get("/docs.#{output_format}", :query => options, :basic_auth => { :username => api_key })
  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentListingFailure.new response.body, response.code
  end

  response
end
list_docs!(options = { }) click to toggle source
# File lib/doc_raptor.rb, line 83
def self.list_docs!(options = { })
  raise ArgumentError.new "please pass in an options hash" unless options.is_a? Hash
  self.list_docs(options.merge(:raise_exception_on_failure => true))
end
status(id = self.status_id, raise_exception_on_failure = false) click to toggle source
# File lib/doc_raptor.rb, line 138
def self.status(id = self.status_id, raise_exception_on_failure = false)
  response = get("/status/#{id}", :basic_auth => { :username => api_key }, :output => 'json')

  if raise_exception_on_failure && !response.success?
    raise DocRaptorException::DocumentStatusFailure.new response.body, response.code
  end

  json = response.parsed_response
  if json['status'] == 'completed'
    self.download_key = json['download_url'].match(/.*?\/download\/(.+)/)[1]
    json['download_key'] = self.download_key
  end
  json
end
status!(id = self.status_id) click to toggle source
# File lib/doc_raptor.rb, line 134
def self.status!(id = self.status_id)
  self.status(id, true)
end