class EtrieveContentApi::Handler

Constants

API_PATH
DOCUMENTS_PATH
DOCUMENT_CONTENT_PARAMS
DOCUMENT_METADATA_PARAMS
PAGE_CONTENT_PARAMS

Attributes

connection[R]

Public Class Methods

new(connection_config) click to toggle source
# File lib/etrieve_content_api/handler.rb, line 23
def initialize(connection_config)
  @config = connection_config
  @connection = Connection.new(@config)
end

Public Instance Methods

all_document_metadata( query: {}, headers: {}, per_request: 25, loop_max: 10, &block ) click to toggle source

Calls document_metadata in a loop retrieving per_request number of documents until all matching docs are retrieved or loop_max is reached

# File lib/etrieve_content_api/handler.rb, line 48
def all_document_metadata(
  query: {}, headers: {}, per_request: 25, loop_max: 10, &block
)
  query[:limit] = per_request
  out = []
  loop_max.times do
    docs, resp_headers = document_metadata(query: query, headers: headers, &block)
    docs.empty? ? break : out << docs
    break if resp_headers[:x_hasmore] == 'False'
    query[:offset] = (query[:offset] || 0) + per_request
  end
  out.flatten
end
create_document(area_code: '', document_name: '', field_values: [], headers: {}, &block) click to toggle source

Creates a new document, returns JSON results of operation, including new document ID key fields should be passed as an array of hashes:

{fieldCode: '', value: '', parentFieldCode: ''}
# File lib/etrieve_content_api/handler.rb, line 93
def create_document(area_code: '', document_name: '', field_values: [], headers: {}, &block)
  headers = headers.empty? ? { "Content-Type" => "application/json" } : headers
  params = {
    areaCode: area_code,
    documentTypeCode: document_name,
    fieldValues: field_values
  }.to_json
  post_json DOCUMENTS_PATH, params: params, headers: headers, &block
end
document_content(document_id, query: {}, headers: {}, &block) click to toggle source

Get content of a document query:

include_annotations: include all annotations? true/false
# File lib/etrieve_content_api/handler.rb, line 65
def document_content(document_id, query: {}, headers: {}, &block)
  path = [DOCUMENTS_PATH, document_id, 'contents'].join('/')
  query_s = encoded_query(
    query: query,
    keys_allowed: DOCUMENT_CONTENT_PARAMS
  )
  get path, query: query_s, headers: headers, &block
end
document_metadata(query: {}, headers: {}, &block) click to toggle source

Get information for one or more documents query:

q: simple search
area_code: document area
document_type_code: document type
field_code: field name for exact match of value in field_value
field_value: field value for field_code
limit: number of items to return
offset: number of items to skip
fields: comma-delimited list of fields to include
# File lib/etrieve_content_api/handler.rb, line 38
def document_metadata(query: {}, headers: {}, &block)
  query_s = encoded_query(
    query: query,
    keys_allowed: DOCUMENT_METADATA_PARAMS
  )
  get_json DOCUMENTS_PATH, query: query_s, headers: headers, &block
end
get(path = '', query: '', headers: {}, &block) click to toggle source

Format a request and pass it on to the connection's get method

# File lib/etrieve_content_api/handler.rb, line 123
def get(path = '', query: '', headers: {}, &block)
  query = query.empty? ? nil : query
  path = [path, query].compact.join('?')
  connection.get path, headers: headers, &block
end
get_json(path = '', query: '', headers: {}, &block) click to toggle source

Process content from get and parse JSON from body.

# File lib/etrieve_content_api/handler.rb, line 130
def get_json(path = '', query: '', headers: {}, &block)
  r = get path, query: query, headers: headers, &block
  return { message: r } unless r.respond_to?(:body)

  json = begin
    JSON.parse(r.body)
  rescue JSON::ParserError
    {}
  end

  [json, r.headers]
end
page_content(document_id, page: 1, query: {}, headers: {}, &block) click to toggle source

Get an image of a specific page of a document query:

height: max height
width: max width
dpi: dots per inch
include_annotations: include all annotations? true/false
# File lib/etrieve_content_api/handler.rb, line 80
def page_content(document_id, page: 1, query: {}, headers: {}, &block)
  path = [DOCUMENTS_PATH, document_id, 'contents', page].join('/')
  query_s = encoded_query(
    query: query,
    keys_allowed: PAGE_CONTENT_PARAMS
  )
  get path, query: query_s, headers: headers, &block
end
post(path = '', params: {}, headers: {}, &block) click to toggle source

Format a request and pass it on to the connection's post method

# File lib/etrieve_content_api/handler.rb, line 144
def post(path = '', params: {}, headers: {}, &block)
  connection.post path, payload: params, headers: headers, &block
end
post_json(path = '', params: {}, headers: {}, &block) click to toggle source

Process content from post and parse JSON from body.

# File lib/etrieve_content_api/handler.rb, line 149
def post_json(path = '', params: {}, headers: {}, &block)
  r = post path, params: params, headers: headers, &block
  return { message: r } unless r.respond_to?(:body)

  json = begin
    JSON.parse(r.body)
  rescue JSON::ParserError
    {}
  end

  [json, r.headers]
end
set_document_content(document_id, file_path, headers: {}, &block) click to toggle source

Sets the contents of an existing document

# File lib/etrieve_content_api/handler.rb, line 104
def set_document_content(document_id, file_path, headers: {}, &block)
  file = begin
      File.open(file_path)
    rescue Errno::ENOENT
    end
  raise AttributeMissingError, 'Valid file or local filepath is required to submit a document.' unless file.is_a?(File)

  path = [DOCUMENTS_PATH, document_id, 'contents'].join('/')
  headers = headers.merge({
              'X-File-Attributes' => {
                'filename' => File.basename(file.path),
                'contenttype' => MIME::Types.type_for(file.path).first.content_type
              }.to_json,
              'Content-Length' => File.size(file.path)
            })
  post_json path, params: file, headers: headers
end

Private Instance Methods

camelize(str) click to toggle source
# File lib/etrieve_content_api/handler.rb, line 183
def camelize(str)
  str.to_s.split('_').map(&:capitalize).join.sub(/^[A-Z]/, &:downcase)
end
encoded_query(query: {}, keys_allowed: :all) click to toggle source
# File lib/etrieve_content_api/handler.rb, line 164
def encoded_query(query: {}, keys_allowed: :all)
  filtered_params = slice_hash(
    query, keys_allowed
  ).select do |_k, v|
    !v.nil? && !v.to_s.empty?
  end
  renamed_params = filtered_params.inject({}) do |h, (k, v)|
    h[camelize(k)] = v
    h
  end
  URI.encode_www_form(renamed_params)
end
slice_hash(orig_hash, keys_allowed = :all) click to toggle source

Keep only specified keys

# File lib/etrieve_content_api/handler.rb, line 178
def slice_hash(orig_hash, keys_allowed = :all)
  return orig_hash if keys_allowed == :all
  orig_hash.select { |k, _v| keys_allowed.include?(k) }
end