Class: CollectionSpace::Client

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/collectionspace/client/client.rb,
lib/collectionspace/client/version.rb

Overview

CollectionSpace client

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#add_batch, #add_batch_job, #add_report, #all, #authority_doctypes, #count, #domain, #find, #find_relation, #get_list_types, #keyword_search, #reindex_full_text, #reset_media_blob, #run_job, #search, #service

Constructor Details

#initialize(config = Configuration.new) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/collectionspace/client/client.rb', line 9

def initialize(config = Configuration.new)
  unless config.is_a? CollectionSpace::Configuration
    raise CollectionSpace::ArgumentError, "Invalid configuration object"
  end

  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/collectionspace/client/client.rb', line 7

def config
  @config
end

Instance Method Details

#can_authenticate?Boolean

User is required to be authenticated in order to access accounts

endpoint. We cannot distinguish between unknown user and known
user with bad password because CollectionSpace returns a 401
error regardless of reason for
non-authentication/authorization

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
# File 'lib/collectionspace/client/client.rb', line 22

def can_authenticate?
  return false unless 

  response = get("accounts/0/accountperms")
  response.result.success? &&
    response.parsed.respond_to?(:dig) &&
    response.parsed.dig("account_permission", "account",
      "userId") == config.username
end

#delete(path) ⇒ Object



57
58
59
# File 'lib/collectionspace/client/client.rb', line 57

def delete(path)
  request "DELETE", path
end

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



32
33
34
# File 'lib/collectionspace/client/client.rb', line 32

def get(path, options = {})
  request "GET", path, options
end

#post(path, payload, options = {}) ⇒ Object



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

def post(path, payload, options = {})
  check_payload(payload)
  request "POST", path, {body: payload}.merge(options)
end

#post_file(file, options = {}) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
# File 'lib/collectionspace/client/client.rb', line 41

def post_file(file, options = {})
  file = File.expand_path(file)
  raise ArgumentError, "cannot find file #{file}" unless File.exist? file

  request "POST", "blobs", {
    body: {
      file: File.open(file)
    }
  }.merge(options)
end

#put(path, payload, options = {}) ⇒ Object



52
53
54
55
# File 'lib/collectionspace/client/client.rb', line 52

def put(path, payload, options = {})
  check_payload(payload)
  request "PUT", path, {body: payload}.merge(options)
end

#versionObject



61
62
63
64
65
66
67
68
# File 'lib/collectionspace/client/client.rb', line 61

def version
  Struct.new(:api, :client, :ui, keyword_init: true)
    .new(
      api: get_api_version,
      client: VERSION,
      ui: CollectionSpace::UiVersion.call(self)
    )
end