Module: CollectionSpace::Helpers

Included in:
Client
Defined in:
lib/collectionspace/client/helpers.rb

Overview

Helper methods for client requests

Instance Method Summary collapse

Instance Method Details

#all(path, options = {}) ⇒ Object

get ALL records at path by paging through record set



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/collectionspace/client/helpers.rb', line 5

def all(path, options = {})
  list_type, list_item = get_list_types(path)
  iterations = (count(path).to_f / config.page_size).ceil
  return [] unless iterations.positive?

  Enumerator::Lazy.new(0...iterations) do |yielder, i|
    response = request('GET', path, options.merge(query: { pgNum: i }))
    unless response.result.success?
      raise CollectionSpace::RequestError, response.result.body
    end

    items_in_page = response.parsed[list_type].fetch('itemsInPage', 0).to_i
    list_items = items_in_page.positive? ? response.parsed[list_type][list_item] : []
    list_items = [list_items] if items_in_page == 1

    yielder << list_items.shift until list_items.empty?
  end
end

#count(path) ⇒ Object



24
25
26
27
28
# File 'lib/collectionspace/client/helpers.rb', line 24

def count(path)
  list_type, = get_list_types(path)
  response   = request('GET', path, query: { pgNum: 0, pgSz: 1 })
  response.parsed[list_type]['totalItems'].to_i if response.result.success?
end

#get_list_types(path) ⇒ Object



30
31
32
33
34
35
# File 'lib/collectionspace/client/helpers.rb', line 30

def get_list_types(path)
  {
    'accounts' => %w[accounts_common_list account_list_item],
    'relations' => %w[relations_common_list relation_list_item],
  }.fetch(path, %w[abstract_common_list list_item])
end

#prepare_query(query, options = {}) ⇒ Object



37
38
39
40
# File 'lib/collectionspace/client/helpers.rb', line 37

def prepare_query(query, options = {})
  query_string = "#{query.type}:#{query.field} #{query.expression}"
  options.merge(query: { as: query_string })
end

#search(query, options = {}) ⇒ Object



42
43
44
45
# File 'lib/collectionspace/client/helpers.rb', line 42

def search(query, options = {})
  options = prepare_query(query, options)
  request 'GET', query.path, options
end