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

#create_document(area_code: '', document_name: '', field_values: [], headers: {}, &block) ⇒ Object

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: ''}


93
94
95
96
97
98
99
100
101
# 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) ⇒ 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: 
  )
  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



123
124
125
126
127
# 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) ⇒ Object

Process content from #get and parse JSON from body.



130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ 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

#post(path = '', params: {}, headers: {}, &block) ⇒ Object

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



144
145
146
# 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) ⇒ Object

Process content from #post and parse JSON from body.



149
150
151
152
153
154
155
156
157
158
159
160
# 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) ⇒ Object

Sets the contents of an existing document

Raises:

  • (AttributeMissingError)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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