Module: CollectionSpace::Helpers
- Included in:
- Client
- Defined in:
- lib/collectionspace/client/helpers.rb
Overview
Helper methods for client requests
Instance Method Summary collapse
-
#add_batch_job(name, template, data = {}, params = { pgSz: 100 }) ⇒ Object
add / update batch job.
-
#add_report(data = {}, params = { pgSz: 100 }) ⇒ Object
add / update reports.
-
#all(path, options = {}) ⇒ Object
get ALL records at path by paging through record set.
- #count(path) ⇒ Object
-
#domain ⇒ Object
get the tenant domain from a system required top level authority (person).
-
#find(type:, value:, subtype: nil, field: nil, schema: 'common', sort: nil, operator: '=') ⇒ Object
find procedure or object by type and id find authority/vocab term by type, subtype, and refname rubocop:disable Metrics/ParameterLists.
- #find_relation(subject_csid:, object_csid:, rel_type: nil) ⇒ Object
- #get_list_types(path) ⇒ Object
- #keyword_search(type:, value:, subtype: nil, sort: nil) ⇒ Object
- #reindex_full_text(doctype, csids = []) ⇒ Object
- #reset_media_blob(id, url) ⇒ Object
- #run_job(name, template, invoke_template, data = {}) ⇒ Object
- #search(query, params = {}) ⇒ Object
- #service(type:, subtype: '') ⇒ Object
Instance Method Details
#add_batch_job(name, template, data = {}, params = { pgSz: 100 }) ⇒ Object
add / update batch job
7 8 9 10 11 |
# File 'lib/collectionspace/client/helpers.rb', line 7 def add_batch_job(name, template, data = {}, params = { pgSz: 100 }) payload = Template.process(template, data) response = get('batch', { query: params }) create_or_update(response, 'batch', 'name', name, payload) end |
#add_report(data = {}, params = { pgSz: 100 }) ⇒ Object
add / update reports
14 15 16 17 18 |
# File 'lib/collectionspace/client/helpers.rb', line 14 def add_report(data = {}, params = { pgSz: 100 }) payload = Template.process('report', data) response = get('reports', { query: params }) create_or_update(response, 'reports', 'name', data[:name], payload) end |
#all(path, options = {}) ⇒ Object
get ALL records at path by paging through record set
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/collectionspace/client/helpers.rb', line 21 def all(path, = {}) 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, .merge(query: { pgNum: i })) raise CollectionSpace::RequestError, response.result.body unless response.result.success? 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
38 39 40 41 42 43 44 |
# File 'lib/collectionspace/client/helpers.rb', line 38 def count(path) list_type, = get_list_types(path) response = request('GET', path, query: { pgNum: 0, pgSz: 1 }) raise CollectionSpace::RequestError, response.result.body unless response.result.success? response.parsed[list_type]['totalItems'].to_i end |
#domain ⇒ Object
get the tenant domain from a system required top level authority (person)
47 48 49 50 51 52 53 54 |
# File 'lib/collectionspace/client/helpers.rb', line 47 def domain path = 'personauthorities' response = request('GET', path, query: { pgNum: 0, pgSz: 1 }) raise CollectionSpace::RequestError, response.result.body unless response.result.success? refname = response.parsed.dig(*get_list_types(path), 'refName') CollectionSpace::RefName.parse(refname)[:domain] end |
#find(type:, value:, subtype: nil, field: nil, schema: 'common', sort: nil, operator: '=') ⇒ Object
find procedure or object by type and id find authority/vocab term by type, subtype, and refname rubocop:disable Metrics/ParameterLists
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/collectionspace/client/helpers.rb', line 59 def find(type:, value:, subtype: nil, field: nil, schema: 'common', sort: nil, operator: '=') service = CollectionSpace::Service.get(type: type, subtype: subtype) field ||= service[:term] # this will be set if it is an authority or vocabulary, otherwise nil field ||= service[:identifier] search_args = CollectionSpace::Search.new.from_hash( path: service[:path], namespace: "#{service[:ns_prefix]}_#{schema}", field: field, expression: "#{operator} '#{value.gsub(/'/, '\\\\\'')}'" ) search(search_args, sortBy: CollectionSpace::Search::DEFAULT_SORT) end |
#find_relation(subject_csid:, object_csid:, rel_type: nil) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/collectionspace/client/helpers.rb', line 76 def find_relation(subject_csid:, object_csid:, rel_type: nil) if rel_type get('relations', query: { 'sbj' => subject_csid, 'obj' => object_csid, 'prd' => rel_type }) else warn( "No rel_type specified, so multiple types of relations between #{subject_csid} and #{object_csid} may be returned", uplevel: 1 ) get('relations', query: { 'sbj' => subject_csid, 'obj' => object_csid }) end end |
#get_list_types(path) ⇒ Object
[View source]
88 89 90 91 92 93 |
# File 'lib/collectionspace/client/helpers.rb', line 88 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 |
#keyword_search(type:, value:, subtype: nil, sort: nil) ⇒ Object
[View source]
139 140 141 142 143 |
# File 'lib/collectionspace/client/helpers.rb', line 139 def keyword_search(type:, value:, subtype: nil, sort: nil) service = CollectionSpace::Service.get(type: type, subtype: subtype) = prepare_keyword_query(value, { sortBy: CollectionSpace::Search::DEFAULT_SORT }) request 'GET', service[:path], end |
#reindex_full_text(doctype, csids = []) ⇒ Object
[View source]
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/collectionspace/client/helpers.rb', line 95 def reindex_full_text(doctype, csids = []) if csids.any? run_job( 'Reindex Full Text', :reindex_full_text, :reindex_by_csids, { doctype: doctype, csids: csids } ) else run_job( 'Reindex Full Text', :reindex_full_text, :reindex_by_doctype, { doctype: doctype } ) end end |
#reset_media_blob(id, url) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/collectionspace/client/helpers.rb', line 107 def reset_media_blob(id, url) raise CollectionSpace::ArgumentError, "Not a valid url #{url}" unless URI.parse(url).instance_of? URI::HTTPS response = find(type: 'media', value: id, field: 'identificationNumber') raise CollectionSpace::RequestError, response.result.body unless response.result.success? found = response.parsed total = found['abstract_common_list']['totalItems'].to_i raise CollectionSpace::NotFoundError, "Media #{id} not found" if total.zero? raise CollectionSpace::DuplicateIdFound, "Found multiple media records for #{id}" unless total == 1 media_uri = found['abstract_common_list']['list_item']['uri'] blob_csid = found['abstract_common_list']['list_item']['blobCsid'] delete("/blobs/#{blob_csid}") if blob_csid payload = Template.process(:reset_media_blob, { id: id }) put(media_uri, payload, query: { 'blobUri' => url }) end |
#run_job(name, template, invoke_template, data = {}) ⇒ Object
[View source]
127 128 129 130 131 132 |
# File 'lib/collectionspace/client/helpers.rb', line 127 def run_job(name, template, invoke_template, data = {}) payload = Template.process(invoke_template, data) job = add_batch_job(name, template) path = job.parsed['document']['collectionspace_core']['uri'] post(path, payload) end |
#search(query, params = {}) ⇒ Object
[View source]
134 135 136 137 |
# File 'lib/collectionspace/client/helpers.rb', line 134 def search(query, params = {}) = prepare_query(query, params) request 'GET', query.path, end |
#service(type:, subtype: '') ⇒ Object
[View source]
145 146 147 |
# File 'lib/collectionspace/client/helpers.rb', line 145 def service(type:, subtype: '') CollectionSpace::Service.get(type: type, subtype: subtype) end |