archivesspace-client 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2087a90bf94b94f918d9b1453442e8e7a682ef75a76092fcd7dbe63d21816b83
4
- data.tar.gz: 3f8e168c3e7a04e6dbb67e3bef8227f77d46ea3b9c009f08eddd24de3441f736
3
+ metadata.gz: 6ea50bb167b4debf8781434efa1ff7b06d077a8ab869f02c30ea8d1cd66de3c9
4
+ data.tar.gz: 83f63ad1f11fc35ca89cdabe871a1e1fa91f935e2e6e284f038b6db2c0257065
5
5
  SHA512:
6
- metadata.gz: f6977881509e613d26e24bbfa7b20204d20aac113a3d3297d798ba3b5c2f0cc673d217d77f9c161a906229a69a8710cdf9745e9c96b27262f8b0f2ac20583e51
7
- data.tar.gz: 2480484be2b5ec0379b3c7c280e8473897a9930cb5a6abc2ec7b28decb81722478e70703a1d8bd38992866d6adaf94492e06480679126213bf500205e948a749
6
+ metadata.gz: faa0086ae6ecd33700e3cf16c41b7457ebf094eb4a5e79109c3999ce0e916f8924450cb6ef8bcb4a761f0d6ed3a1c12a457dec4cd5738429a8f80fb249303fbe
7
+ data.tar.gz: '09a6fca39d7846875715216e8b10d5d29b776b433c37abef3490b5964e62deca9de5f01e69f868a476cceb11c7ceaa43032cfb906f9fff35da10b43e0b3582e7'
data/README.md CHANGED
@@ -87,6 +87,35 @@ client.get('digital_objects') # instead of "repositories/2/digital_objects" etc.
87
87
  client.config.base_repo = ""
88
88
  ```
89
89
 
90
+ ## Templates
91
+
92
+ Templates are an optional feature that can help simplify the effort of creating
93
+ json payloads for ArchivesSpace. Rather than construct the json programatically
94
+ according to the schemas a `.erb` template can be used to generate payloads
95
+ instead which are transformed to json automatically. There are a small number of
96
+ templates provided with the client, but you can create your own and access them
97
+ by setting the `ARCHIVESSPACE_CLIENT_TEMPLATES_PATH` envvar. A particularly simple
98
+ template might look like:
99
+
100
+ ```erb
101
+ {
102
+ "digital_object_id": "<%= data[:digital_object_id] %>",
103
+ "title": "<%= data[:title] %>"
104
+ }
105
+ ```
106
+
107
+ Practically speaking there isn't much benefit to this example, but in the case of
108
+ a more complex record structure where you want to populate deeply nested elements
109
+ using a flat file structure (like csv) this can be a very convenient way of
110
+ assembling the payload. To process a template:
111
+
112
+ ```ruby
113
+ data = { repo_code: 'ABC', name: 'ABC Archive', agent_contact_name: 'ABC Admin' }
114
+ json = ArchivesSpace::Template.process(:repository_with_agent, data)
115
+ response = client.post('/repositories/with_agent', json)
116
+ puts response.result.success? ? '=)' : '=('
117
+ ```
118
+
90
119
  ## Development
91
120
 
92
121
  To run the examples start a local instance of ArchivesSpace then:
@@ -110,7 +139,7 @@ bundle exec rake
110
139
  Bump version in `lib/archivesspace/client/version.rb` then:
111
140
 
112
141
  ```bash
113
- VERSION=0.1.7
142
+ VERSION=0.1.8
114
143
  gem build archivesspace-client
115
144
  git add . && git commit -m "Bump to $VERSION"
116
145
  git tag v$VERSION
@@ -4,14 +4,13 @@ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
4
  require 'awesome_print'
5
5
  require 'archivesspace/client'
6
6
 
7
- username = 'mrx'
8
- password = '123456'
7
+ username = 'admin'
8
+ password = 'admin'
9
9
 
10
10
  # default client connection: localhost:8089, admin, admin
11
11
  client = ArchivesSpace::Client.new.login
12
12
  begin
13
- client.password_reset username, password
14
- puts "Successfully updated password for #{username}."
13
+ puts client.password_reset(username, password).parsed
15
14
  rescue StandardError => e
16
15
  puts "Failed to update password for #{username},\n#{e.message}"
17
16
  end
@@ -17,7 +17,6 @@ config = ArchivesSpace::Configuration.new(
17
17
  }
18
18
  )
19
19
 
20
- # default client connection: localhost:8089, admin, admin
21
20
  client = ArchivesSpace::Client.new(config).login
22
21
 
23
22
  ap ArchivesSpace::Template.list # view available templates
@@ -25,7 +24,7 @@ ap ArchivesSpace::Template.list # view available templates
25
24
  repo_data = {
26
25
  repo_code: 'XYZ',
27
26
  name: 'XYZ Archive',
28
- agent_contact_name: 'John Doe'
27
+ agent_contact_name: 'XYZ Admin'
29
28
  }
30
29
 
31
30
  user_data = {
@@ -35,11 +34,11 @@ user_data = {
35
34
  }
36
35
  user_password = '123456'
37
36
 
38
- repository = ArchivesSpace::Template.process_template(:repository_with_agent, repo_data)
37
+ repository = ArchivesSpace::Template.process(:repository_with_agent, repo_data)
39
38
 
40
39
  begin
41
40
  response = client.post('/repositories/with_agent', repository)
42
- if response.status_code.to_s =~ /^2/
41
+ if response.result.success?
43
42
  repository = client.repositories.find { |r| r['repo_code'] == 'XYZ' }
44
43
  ap repository
45
44
  ap client.delete(repository['uri'])
@@ -47,9 +46,9 @@ begin
47
46
  ap response.parsed
48
47
  end
49
48
 
50
- user = ArchivesSpace::Template.process_template(:user, user_data)
49
+ user = ArchivesSpace::Template.process(:user, user_data)
51
50
  response = client.post('users', user, { password: user_password })
52
- if response.status_code.to_s =~ /^2/
51
+ if response.result.success?
53
52
  user = client.users.find { |r| r['username'] == 'lmessi' }
54
53
  ap user
55
54
  ap client.delete user['uri']
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
+ require 'awesome_print'
5
+ require 'archivesspace/client'
6
+
7
+ # official sandbox
8
+ config = ArchivesSpace::Configuration.new(
9
+ {
10
+ base_uri: 'http://sandbox.archivesspace.org/api',
11
+ base_repo: '',
12
+ username: 'admin',
13
+ password: 'admin',
14
+ page_size: 50,
15
+ throttle: 0,
16
+ verify_ssl: false
17
+ }
18
+ )
19
+
20
+ client = ArchivesSpace::Client.new(config).login
21
+
22
+ user_data = {
23
+ username: 'bde',
24
+ name: 'BDE',
25
+ is_admin: false
26
+ }
27
+
28
+ client.post(
29
+ 'users',
30
+ ArchivesSpace::Template.process(:user, user_data),
31
+ { password: '123456' }
32
+ )
33
+
34
+ users_with_roles = {
35
+ 'bde' => ['repository-basic-data-entry']
36
+ }
37
+
38
+ begin
39
+ client.config.base_repo = "repositories/2"
40
+ results = client.group_user_assignment users_with_roles
41
+ ap results.map(&:parsed)
42
+ rescue ArchivesSpace::RequestError => e
43
+ puts e.message
44
+ end
@@ -18,11 +18,11 @@ module ArchivesSpace
18
18
  end
19
19
 
20
20
  def post(path, payload, params = {})
21
- request 'POST', path, { body: payload.to_json, query: params }
21
+ request 'POST', path, { body: payload, query: params }
22
22
  end
23
23
 
24
24
  def put(path, payload, params = {})
25
- request 'PUT', path, { body: payload.to_json, query: params }
25
+ request 'PUT', path, { body: payload, query: params }
26
26
  end
27
27
 
28
28
  def delete(path)
@@ -49,9 +49,9 @@ module ArchivesSpace
49
49
  get 'version'
50
50
  end
51
51
 
52
- def batch_import(payload, params = {})
53
- # create "batch_import", payload, params
54
- end
52
+ # def batch_import(payload, params = {})
53
+ # # TODO: create "batch_import", payload, params
54
+ # end
55
55
 
56
56
  def digital_objects(options = {})
57
57
  all('digital_objects', options)
@@ -61,30 +61,30 @@ module ArchivesSpace
61
61
  all('groups', options)
62
62
  end
63
63
 
64
- def group_user_assignment(users_with_roles, params = { with_members: true })
64
+ def group_user_assignment(users_with_roles)
65
65
  updated = []
66
66
  groups.each do |group|
67
- changed = false
67
+ group = get("groups/#{uri_to_id(group['uri'])}").parsed
68
+ update = false
68
69
 
69
70
  users_with_roles.each do |user, roles|
70
- if roles.include? group['group_code']
71
- unless group['member_usernames'].include? user
72
- group['member_usernames'] << user
73
- changed = true
74
- end
75
- else
76
- if group['member_usernames'].include? user
71
+ # should the user still belong to this group?
72
+ if group['member_usernames'].include?(user)
73
+ unless roles.include? group['group_code']
77
74
  group['member_usernames'].delete user
78
- changed = true
75
+ update = true
79
76
  end
77
+ # should the user be added to this group?
78
+ elsif roles.include? group['group_code']
79
+ group['member_usernames'] << user
80
+ update = true
80
81
  end
81
82
  end
82
83
 
83
- next unless changed
84
+ next unless update
84
85
 
85
- id = group['uri'].split('/')[-1]
86
- response = post("/groups/#{id}", group, params)
87
- updated << response.parsed
86
+ response = post("/groups/#{uri_to_id(group['uri'])}", group.to_json)
87
+ updated << response
88
88
  end
89
89
  updated
90
90
  end
@@ -105,7 +105,7 @@ module ArchivesSpace
105
105
  user = all('users').find { |u| u['username'] == username }
106
106
  raise RequestError, user.status unless user
107
107
 
108
- post(user['uri'], user, { password: password })
108
+ post(user['uri'], user.to_json, { password: password })
109
109
  end
110
110
 
111
111
  def repositories(options = {})
@@ -118,8 +118,12 @@ module ArchivesSpace
118
118
  all('resources', options)
119
119
  end
120
120
 
121
- def search(params)
122
- # get "search", params
121
+ # def search(params)
122
+ # # TODO: get "search", params
123
+ # end
124
+
125
+ def uri_to_id(uri)
126
+ uri.split('/').last
123
127
  end
124
128
 
125
129
  def users(options = {})
@@ -2,10 +2,9 @@
2
2
 
3
3
  module ArchivesSpace
4
4
  class Response
5
- attr_reader :result, :parsed, :body, :headers, :status, :status_code, :xml
5
+ attr_reader :result, :parsed, :body, :headers, :status, :status_code
6
6
 
7
7
  def initialize(result)
8
- # throw error
9
8
  @result = result
10
9
  @parsed = result.parsed_response
11
10
  @body = result.body
@@ -3,21 +3,24 @@
3
3
  module ArchivesSpace
4
4
  module Template
5
5
  def self.list
6
- []
6
+ Dir.glob File.join(templates_path, '*.erb')
7
7
  end
8
8
 
9
- def self.process_template(template, data)
10
- t = ERB.new(read_template(template))
9
+ def self.process(template, data)
10
+ t = ERB.new(read(template))
11
11
  r = t.result(binding).gsub(/\n+/, "\n")
12
- JSON.parse(r)
12
+ JSON.parse(r).to_json
13
13
  end
14
14
 
15
- def self.read_template(file)
15
+ def self.read(file)
16
16
  File.read("#{templates_path}/#{file}.json.erb")
17
17
  end
18
18
 
19
19
  def self.templates_path
20
- File.join(File.dirname(File.expand_path(__FILE__)), 'templates')
20
+ ENV.fetch(
21
+ 'ARCHIVESSPACE_CLIENT_TEMPLATES_PATH',
22
+ File.join(File.dirname(File.expand_path(__FILE__)), 'templates')
23
+ )
21
24
  end
22
25
  end
23
26
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ArchivesSpace
4
4
  class Client
5
- VERSION = '0.1.7'
5
+ VERSION = '0.1.8'
6
6
  end
7
7
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ArchivesSpace::Template do
6
+ it 'can list the default templates' do
7
+ templates = ArchivesSpace::Template.list
8
+ expect(templates).to_not be_empty
9
+ expect(templates).to include(/repository_with_agent.*erb/)
10
+ end
11
+
12
+ it 'can change the path when template envvar is set' do
13
+ expect(ArchivesSpace::Template.templates_path).to match(
14
+ /#{File.join('lib', 'archivesspace', 'client', 'templates')}/
15
+ )
16
+ ENV['ARCHIVESSPACE_CLIENT_TEMPLATES_PATH'] = '/path/to/nowhere'
17
+ expect(ArchivesSpace::Template.templates_path).to eq '/path/to/nowhere'
18
+ ENV.delete('ARCHIVESSPACE_CLIENT_TEMPLATES_PATH')
19
+ end
20
+
21
+ it 'can process a template' do
22
+ data = { repo_code: 'ABC', name: 'ABC Archive', agent_contact_name: 'ABC Admin' }
23
+ json = JSON.parse(ArchivesSpace::Template.process(:repository_with_agent, data))
24
+ expect(json['repository']['repo_code']).to eq data[:repo_code]
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: archivesspace-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Cooper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-02 00:00:00.000000000 Z
11
+ date: 2021-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -155,6 +155,7 @@ files:
155
155
  - examples/password_reset.rb
156
156
  - examples/repo_and_user.rb
157
157
  - examples/test_connection.rb
158
+ - examples/user_groups.rb
158
159
  - lib/archivesspace/client.rb
159
160
  - lib/archivesspace/client/client.rb
160
161
  - lib/archivesspace/client/configuration.rb
@@ -169,6 +170,7 @@ files:
169
170
  - lib/archivesspace/client/version.rb
170
171
  - spec/archivesspace/client_spec.rb
171
172
  - spec/archivesspace/configuration_spec.rb
173
+ - spec/archivesspace/templates_spec.rb
172
174
  - spec/fixtures/cassettes/backend_version.yml
173
175
  - spec/spec_helper.rb
174
176
  homepage: ''
@@ -190,12 +192,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
192
  - !ruby/object:Gem::Version
191
193
  version: '0'
192
194
  requirements: []
193
- rubygems_version: 3.1.4
195
+ rubygems_version: 3.1.6
194
196
  signing_key:
195
197
  specification_version: 4
196
198
  summary: Interact with ArchivesSpace via the API.
197
199
  test_files:
198
200
  - spec/archivesspace/client_spec.rb
199
201
  - spec/archivesspace/configuration_spec.rb
202
+ - spec/archivesspace/templates_spec.rb
200
203
  - spec/fixtures/cassettes/backend_version.yml
201
204
  - spec/spec_helper.rb