module DocmagoClient

Constants

VERSION

Attributes

api_key[W]
base_uri[W]
logger[W]

Public Class Methods

api_key() click to toggle source
# File lib/docmago_client.rb, line 25
def api_key
  @api_key || ENV['DOCMAGO_API_KEY']
end
base_uri() click to toggle source
# File lib/docmago_client.rb, line 21
def base_uri
  @base_uri || ENV['DOCMAGO_URL'] || 'https://docmago.com/api'
end
create(options = {}) click to toggle source

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

# File lib/docmago_client.rb, line 32
def self.create(options = {})
  raise ArgumentError, 'please pass in an options hash' unless options.is_a? Hash
  if options[:content].nil? || options[:content].empty?
    raise DocmagoClient::Error::NoContentError.new, 'must supply :content'
  end

  default_options = {
    name: 'default',
    type: 'pdf',
    integrity_check: true
  }

  options = default_options.merge(options)

  if options[:zip_resources]
    tmp_dir = Dir.mktmpdir
    begin
      resource_archiver = HTMLResourceArchiver.new(options)
      options[:content] = File.new(resource_archiver.create_zip("#{tmp_dir}/document.zip"))
      options.delete :assets

      response = Typhoeus.post "#{base_uri}/documents", body: {
        auth_token: api_key,
        document: options.slice(:content, :name, :type, :test_mode)
      }
    ensure
      FileUtils.remove_entry_secure tmp_dir
    end
  else
    response = Typhoeus.post "#{base_uri}/documents", body: {
      auth_token: api_key,
      document: options.slice(:content, :name, :type, :test_mode)
    }
  end

  if options[:integrity_check] && response.headers['X-Docmago-Checksum'] != Digest::MD5.hexdigest(response.body)
    raise DocmagoClient::Error::IntegrityCheckError.new, 'File corrupt (invalid MD5 checksum)'
  end

  response
end
logger() click to toggle source
# File lib/docmago_client.rb, line 17
def logger
  @logger ||= Logger.new($stdout)
end