module Caterpillar

Constants

APIError
NoApiKeyError
NoSourceError
VERSION

Attributes

api_key[RW]
api_version[RW]
base_uri[RW]

Public Class Methods

configure() { |self| ... } click to toggle source
# File lib/caterpillar-ruby.rb, line 14
def configure
  @api_version = 'v1'
  @base_uri = 'https://api.caterpillar.io'

  yield self
end
create(options = {}) { |f, response| ... } click to toggle source
# File lib/caterpillar-ruby.rb, line 21
def create(options = {})
  raise NoApiKeyError.new('API Key is blank') if api_key.blank?
  raise NoSourceError.new('Source is blank') if options[:source].blank?

  options.deep_modify_keys!(:camelize)

  body = {
    source: options.delete(:source),
    data: {
      apiKey: api_key
    }.merge!(options)
  }.to_json

  response = HTTParty.post("#{base_uri}/#{api_version}/documents/convert",
    body: body,
    headers: { 'Content-Type' => 'application/json' }
  ).force_encoding('ISO-8859-1').encode('UTF-8')

  parsed_response = response.is_caterpillar_json?
  raise APIError.new(parsed_response['message']) if parsed_response.present? && parsed_response['status'] == 0

  if block_given?
    return_value = nil

    Tempfile.open('caterpillar') do |f|
      f.sync = true
      f.write(response)
      f.rewind

      return_value = yield f, response
    end

    return_value
  else
    response
  end
end