Class: EtrieveContentApi::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/etrieve_content_api/handler.rb

Constant Summary collapse

API_PATH =
'api'.freeze
DOCUMENTS_PATH =
[API_PATH, 'documents'].join('/').freeze
DOCUMENT_METADATA_PARAMS =
%i[
  area_code
  document_type_code
  field_code
  field_value
  fields
  limit
  offset
  q
].freeze
PAGE_CONTENT_PARAMS =
%i[dpi height width include_annotations].freeze
DOCUMENT_CONTENT_PARAMS =
%i[include_annotations].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_config) ⇒ Handler

Returns a new instance of Handler.



23
24
25
26
# File 'lib/etrieve_content_api/handler.rb', line 23

def initialize(connection_config)
  @config = connection_config
  @connection = Connection.new(@config)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



21
22
23
# File 'lib/etrieve_content_api/handler.rb', line 21

def connection
  @connection
end

Instance Method Details

#all_document_metadata(query: {}, headers: {}, per_request: 25, loop_max: 10, &block) ⇒ Object

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



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/etrieve_content_api/handler.rb', line 48

def (
  query: {}, headers: {}, per_request: 25, loop_max: 10, &block
)
  query[:limit] = per_request
  out = []
  loop_max.times do
    docs, resp_headers = (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

#document_content(document_id, query: {}, headers: {}, &block) ⇒ Object

Get content of a document query:

include_annotations: include all annotations? true/false


65
66
67
68
69
70
71
72
# 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) ⇒ Object

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


38
39
40
41
42
43
44
# File 'lib/etrieve_content_api/handler.rb', line 38

def (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) ⇒ Object

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



90
91
92
93
94
# File 'lib/etrieve_content_api/handler.rb', line 90

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) ⇒ Object

Process content from #get and parse JSON from body.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/etrieve_content_api/handler.rb', line 97

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) ⇒ Object

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


80
81
82
83
84
85
86
87
# 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