collectionspace-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +6 -0
- data/collectionspace-client.gemspec +31 -0
- data/examples/demo.rb +25 -0
- data/examples/export.rb +31 -0
- data/examples/purge-empty-vocabularies.rb +16 -0
- data/examples/search.rb +67 -0
- data/lib/collectionspace/client.rb +21 -0
- data/lib/collectionspace/client/client.rb +41 -0
- data/lib/collectionspace/client/configuration.rb +28 -0
- data/lib/collectionspace/client/helpers.rb +126 -0
- data/lib/collectionspace/client/request.rb +49 -0
- data/lib/collectionspace/client/response.rb +20 -0
- data/lib/collectionspace/client/search.rb +19 -0
- data/lib/collectionspace/client/version.rb +5 -0
- metadata +190 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1766746ac50ad7d2afad39df5c685fff98ef8e03
|
4
|
+
data.tar.gz: 437a9ac5f9a4e71ab15fbd4cb676f9a8154372c5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f6247a252fddb9f618b0a3950ab61d554648da17ac0e2fb2d9f4cbb57f4a61c89d15cbd786760a67812d1483a841d98dfc284740c1269d4adcedbbff6a44d8c9
|
7
|
+
data.tar.gz: c6304daeab5d53794fd38e82f73236643ded111a89556fbe011474adcbe088c63759d1126d963ccf592525811ef776cccbd6b5e1ae5f9c5a76b411df8531ac9f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Mark Cooper
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
CollectionSpace Client
|
2
|
+
===
|
3
|
+
|
4
|
+
CollectionSpace API client.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
---
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'collectionspace-client'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install collectionspace-client
|
22
|
+
|
23
|
+
Usage
|
24
|
+
---
|
25
|
+
|
26
|
+
Basic usage:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'collectionspace/client'
|
30
|
+
|
31
|
+
config = CollectionSpace::Configuration.new({
|
32
|
+
base_uri: "https://cspace.muesum.org/cspace-services",
|
33
|
+
username: "admin@cspace.muesum.org",
|
34
|
+
password: "Administrator",
|
35
|
+
})
|
36
|
+
|
37
|
+
client = CollectionSpace::Client.new(config)
|
38
|
+
|
39
|
+
result = client.post_blob_uri "http://cspace.muesum.org/assets/mueseum.png"
|
40
|
+
|
41
|
+
raise "=(" if result.status_code != 201
|
42
|
+
|
43
|
+
search_args = {
|
44
|
+
path: "blobs",
|
45
|
+
type: "blobs_common",
|
46
|
+
field: "name",
|
47
|
+
expression: "ILIKE '%museum.png%'",
|
48
|
+
}
|
49
|
+
|
50
|
+
query = CollectionSpace::Search.new.from_hash search_args
|
51
|
+
|
52
|
+
result = client.search(query)
|
53
|
+
|
54
|
+
if result.status_code == 200
|
55
|
+
ap result.parsed['abstract_common_list']
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
See the sample scripts within the `examples` directory for more.
|
60
|
+
|
61
|
+
Development
|
62
|
+
---
|
63
|
+
|
64
|
+
To run the examples:
|
65
|
+
|
66
|
+
```bash
|
67
|
+
bundle exec ruby examples/demo.rb
|
68
|
+
```
|
69
|
+
|
70
|
+
Any script placed in the examples directory with a `my_` prefix are ignored by git. Follow the convention used by the existing scripts to bootstrap and experiment away.
|
71
|
+
|
72
|
+
To run the tests:
|
73
|
+
|
74
|
+
```bash
|
75
|
+
bundle exec rake
|
76
|
+
```
|
77
|
+
|
78
|
+
Contributing
|
79
|
+
---
|
80
|
+
|
81
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lyrasis/collectionspace-client.
|
82
|
+
|
83
|
+
License
|
84
|
+
---
|
85
|
+
|
86
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
87
|
+
|
88
|
+
---
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'collectionspace/client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "collectionspace-client"
|
8
|
+
spec.version = CollectionSpace::Client::VERSION
|
9
|
+
spec.authors = ["Mark Cooper"]
|
10
|
+
spec.email = ["mark.cooper@lyrasis.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{CollectionSpace API client.}
|
13
|
+
spec.homepage = "https://github.com/lyrasis/collectionspace-client.git"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "httparty"
|
22
|
+
spec.add_dependency "json"
|
23
|
+
spec.add_dependency "nokogiri"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec"
|
28
|
+
spec.add_development_dependency "vcr"
|
29
|
+
spec.add_development_dependency "webmock"
|
30
|
+
spec.add_development_dependency "awesome_print"
|
31
|
+
end
|
data/examples/demo.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'awesome_print'
|
3
|
+
require 'collectionspace/client'
|
4
|
+
|
5
|
+
# CREATE CLIENT WITH DEFAULT (DEMO) CONFIGURATION -- BE NICE!!!
|
6
|
+
client = CollectionSpace::Client.new
|
7
|
+
client.config.throttle = 1
|
8
|
+
|
9
|
+
# GET REQUEST FOR CONDITIONCHECK RECORDS AND PRINT THE PARSED RESPONSE AND XML
|
10
|
+
response = client.get('conditionchecks')
|
11
|
+
if response.status_code == 200
|
12
|
+
ap response.parsed
|
13
|
+
ap response.xml
|
14
|
+
end
|
15
|
+
|
16
|
+
# GET ALL ACQUISITIONS AND PRINT TO CONSOLE
|
17
|
+
acquisitions = client.all('acquisitions')
|
18
|
+
ap acquisitions
|
19
|
+
|
20
|
+
# GET ALL INTAKE RECORDS AND PROCESS PER PAGE (INSTEAD OF WAITING FOR ALL)
|
21
|
+
client.all('intakes') do |item|
|
22
|
+
uri = item["uri"]
|
23
|
+
intake = client.get uri
|
24
|
+
ap intake.parsed
|
25
|
+
end
|
data/examples/export.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'awesome_print'
|
3
|
+
require 'collectionspace/client'
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
# MONKEY PATCHING FOR RAILS LIKE 3.days.ago
|
7
|
+
class Fixnum
|
8
|
+
SECONDS_IN_DAY = 24 * 60 * 60
|
9
|
+
|
10
|
+
def days
|
11
|
+
self * SECONDS_IN_DAY
|
12
|
+
end
|
13
|
+
|
14
|
+
def ago
|
15
|
+
Time.now - self
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# CREATE CLIENT WITH DEFAULT (DEMO) CONFIGURATION -- BE NICE!!!
|
20
|
+
client = CollectionSpace::Client.new
|
21
|
+
client.config.throttle = 1
|
22
|
+
|
23
|
+
# GET ALL CATALOGING RECORDS AND DUMP THE XML IF UPDATED SINCE 3 DAYS AGO
|
24
|
+
client.all('collectionobjects') do |item|
|
25
|
+
updated = Date.parse item["updatedAt"]
|
26
|
+
if updated > Date.parse(14.days.ago.to_s)
|
27
|
+
collectionobject = client.get item["uri"]
|
28
|
+
# TO EXPORT WRITE THE BODY TO FILE, HERE WE JUST PRINT IT
|
29
|
+
ap collectionobject.body # OR .xml.to_s
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'awesome_print'
|
3
|
+
require 'collectionspace/client'
|
4
|
+
|
5
|
+
# CREATE CLIENT WITH DEFAULT (DEMO) CONFIGURATION -- BE NICE!!!
|
6
|
+
client = CollectionSpace::Client.new
|
7
|
+
client.config.throttle = 1
|
8
|
+
|
9
|
+
client.all('vocabularies') do |item|
|
10
|
+
uri = item["uri"]
|
11
|
+
if client.count("#{uri}/items") == 0
|
12
|
+
puts "Purging empty vocabulary:\t#{item['displayName']} (#{item['csid']})"
|
13
|
+
# YOU WOULD UNCOMMENT THIS TO ACTUALLY PERFORM THE PURGE ...
|
14
|
+
# client.delete uri
|
15
|
+
end
|
16
|
+
end
|
data/examples/search.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'awesome_print'
|
3
|
+
require 'collectionspace/client'
|
4
|
+
|
5
|
+
# CREATE CLIENT WITH DEFAULT (DEMO) CONFIGURATION -- BE NICE!!!
|
6
|
+
client = CollectionSpace::Client.new
|
7
|
+
|
8
|
+
# EXAMPLE NESTED ATTRIBUTE SEARCH
|
9
|
+
search_args = {
|
10
|
+
path: "collectionobjects",
|
11
|
+
type: "collectionobjects_common",
|
12
|
+
field: 'titleGroupList/*1/title',
|
13
|
+
expression: "ILIKE '%blue%'",
|
14
|
+
}
|
15
|
+
|
16
|
+
query = CollectionSpace::Search.new.from_hash search_args
|
17
|
+
ap client.search(query).parsed
|
18
|
+
|
19
|
+
# SEARCH AND REFORMAT RESULTS
|
20
|
+
|
21
|
+
# assume retrieved for collectionobject i.e.: AttributeMap.where(type: 'collectionobject')
|
22
|
+
attribute_map = [
|
23
|
+
{ field: 'title', key: 'collectionobjects_common', nested_key: 'title', with: nil },
|
24
|
+
{ field: 'title_type', key: 'collectionobjects_common', nested_key: 'titleType', with: nil },
|
25
|
+
{ field: 'display_date', key: 'collectionobjects_common', nested_key: 'dateDisplayDate', with: nil },
|
26
|
+
{ field: 'object_production_person_group', key: 'collectionobjects_common', nested_key: 'objectProductionPersonGroup', with: 'objectProductionPerson' },
|
27
|
+
{ field: 'content_persons', key: 'collectionobjects_common', nested_key: 'contentPersons', with: 'contentPerson' },
|
28
|
+
{ field: 'responsible_department', key: 'collectionobjects_common', nested_key: 'responsibleDepartment', with: nil },
|
29
|
+
{ field: 'created_by', key: 'collectionspace_core', nested_key: 'createdBy', with: nil },
|
30
|
+
{ field: 'created_at', key: 'collectionspace_core', nested_key: 'createdAt', with: nil },
|
31
|
+
{ field: 'updated_at', key: 'collectionspace_core', nested_key: 'updatedAt', with: nil },
|
32
|
+
]
|
33
|
+
|
34
|
+
search_args = {
|
35
|
+
path: "collectionobjects",
|
36
|
+
type: "collectionspace_core",
|
37
|
+
field: 'updatedAt',
|
38
|
+
expression: ">= TIMESTAMP '2015-12-17T00:00:00'",
|
39
|
+
}
|
40
|
+
|
41
|
+
query = CollectionSpace::Search.new.from_hash search_args
|
42
|
+
|
43
|
+
result = client.search(query)
|
44
|
+
if result.status_code == 200
|
45
|
+
data = result.parsed["abstract_common_list"]["list_item"]
|
46
|
+
data.each do |item|
|
47
|
+
record = client.get(item["uri"]).parsed
|
48
|
+
|
49
|
+
attributes = {}
|
50
|
+
attribute_map.each do |map|
|
51
|
+
if map[:with]
|
52
|
+
as = client.deep_find(record, map[:key], map[:nested_key])
|
53
|
+
values = []
|
54
|
+
if as.is_a? Array
|
55
|
+
values = as.map { |a| client.strip_refname( client.deep_find(a, map[:with]) ) }
|
56
|
+
elsif as.is_a? Hash and as[ map[:with] ]
|
57
|
+
values = as[ map[:with] ].is_a?(Array) ? as[ map[:with] ].map { |a| client.strip_refname(a) } : [ client.strip_refname(as[ map[:with] ]) ]
|
58
|
+
end
|
59
|
+
attributes[map[:field]] = values
|
60
|
+
else
|
61
|
+
attributes[map[:field]] = client.deep_find(record, map[:key], map[:nested_key])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
# PRINT REFORMATTED RESULTS
|
65
|
+
ap attributes
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
# mixins required first
|
6
|
+
require "collectionspace/client/helpers"
|
7
|
+
|
8
|
+
require "collectionspace/client/client"
|
9
|
+
require "collectionspace/client/configuration"
|
10
|
+
require "collectionspace/client/request"
|
11
|
+
require "collectionspace/client/response"
|
12
|
+
require "collectionspace/client/search"
|
13
|
+
require "collectionspace/client/version"
|
14
|
+
|
15
|
+
module CollectionSpace
|
16
|
+
|
17
|
+
class ArgumentError < Exception ; end
|
18
|
+
class PayloadError < Exception ; end
|
19
|
+
class RequestError < Exception ; end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module CollectionSpace
|
2
|
+
|
3
|
+
class Client
|
4
|
+
include DeepFind
|
5
|
+
include Helpers
|
6
|
+
attr_reader :config
|
7
|
+
|
8
|
+
def initialize(config = Configuration.new)
|
9
|
+
raise "Invalid configuration object" unless config.kind_of? CollectionSpace::Configuration
|
10
|
+
@config = config
|
11
|
+
end
|
12
|
+
|
13
|
+
def get(path, options = {})
|
14
|
+
request 'GET', path, options
|
15
|
+
end
|
16
|
+
|
17
|
+
def post(path, payload)
|
18
|
+
raise PayloadError.new if Nokogiri::XML(payload).errors.any?
|
19
|
+
request 'POST', path, { body: payload }
|
20
|
+
end
|
21
|
+
|
22
|
+
def put(path, payload)
|
23
|
+
raise PayloadError.new if Nokogiri::XML(payload).errors.any?
|
24
|
+
request 'PUT', path, { body: payload }
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(path)
|
28
|
+
request 'DELETE', path
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def request(method, path, options = {})
|
34
|
+
sleep config.throttle
|
35
|
+
result = Request.new(config, method, path, options).execute
|
36
|
+
Response.new result
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module CollectionSpace
|
2
|
+
|
3
|
+
class Configuration
|
4
|
+
|
5
|
+
def defaults
|
6
|
+
{
|
7
|
+
base_uri: "http://demo.collectionspace.org:8180/cspace-services",
|
8
|
+
username: "admin@core.collectionspace.org",
|
9
|
+
password: "Administrator",
|
10
|
+
page_size: 50,
|
11
|
+
include_deleted: false,
|
12
|
+
throttle: 0,
|
13
|
+
verify_ssl: true,
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(settings = {})
|
18
|
+
settings = defaults.merge(settings)
|
19
|
+
settings.each do |property, value|
|
20
|
+
next unless defaults.keys.include? property
|
21
|
+
instance_variable_set("@#{property}", value)
|
22
|
+
self.class.send(:attr_accessor, property)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
module CollectionSpace
|
2
|
+
|
3
|
+
# http://www.garrettqmartin.com/2015/02/03/finding-deeply-nested-hash-keys/
|
4
|
+
module DeepFind
|
5
|
+
def deep_find(obj, key, nested_key = nil)
|
6
|
+
return obj[key] if obj.respond_to?(:key?) && obj.key?(key)
|
7
|
+
if obj.is_a? Enumerable
|
8
|
+
found = nil
|
9
|
+
obj.find { |*a| found = deep_find(a.last, key) }
|
10
|
+
if nested_key
|
11
|
+
deep_find(found, nested_key)
|
12
|
+
else
|
13
|
+
found
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Helpers
|
20
|
+
|
21
|
+
# get ALL records at path by paging through record set
|
22
|
+
# can pass block to act on each page of results
|
23
|
+
def all(path, options = {}, &block)
|
24
|
+
all = []
|
25
|
+
list_type, list_item = get_list_types(path)
|
26
|
+
|
27
|
+
result = request('GET', path, options)
|
28
|
+
raise RequestError.new result.status if result.status_code != 200 or result.parsed[list_type].nil?
|
29
|
+
|
30
|
+
total = result.parsed[list_type]['totalItems'].to_i
|
31
|
+
items = result.parsed[list_type]['itemsInPage'].to_i
|
32
|
+
return all if total == 0
|
33
|
+
|
34
|
+
pages = (total / config.page_size) + 1
|
35
|
+
(0 .. pages - 1).each do |i|
|
36
|
+
options[:query][:pgNum] = i
|
37
|
+
result = request('GET', path, options)
|
38
|
+
raise RequestError.new result.status if result.status_code != 200
|
39
|
+
list_items = result.parsed[list_type][list_item]
|
40
|
+
list_items = [ list_items ] if items == 1
|
41
|
+
list_items.each { |item| yield item if block_given? }
|
42
|
+
all.concat list_items
|
43
|
+
end
|
44
|
+
all
|
45
|
+
end
|
46
|
+
|
47
|
+
def count(path)
|
48
|
+
# make sure path is only 1 level deep?
|
49
|
+
count = nil
|
50
|
+
result = request('GET', path, { query: { pgSz: 1 } })
|
51
|
+
count = result.parsed['abstract_common_list']['totalItems'].to_i if result.status_code == 200
|
52
|
+
count
|
53
|
+
end
|
54
|
+
|
55
|
+
# create blob record by external url
|
56
|
+
def post_blob_url(url)
|
57
|
+
raise ArgumentError.new("Invalid blob URL #{url}") unless URI.parse(url).scheme =~ /^https?/
|
58
|
+
request 'POST', "blobs", {
|
59
|
+
query: { "blobUri" => url },
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def post_relationship(type_a, csid_a, type_b, csid_b)
|
64
|
+
# requires an erb template
|
65
|
+
# request 'POST', "relations", { body: payload }
|
66
|
+
# flip for reciprocal relationship
|
67
|
+
# request 'POST', "relations", { body: payload }
|
68
|
+
end
|
69
|
+
|
70
|
+
def search(query, options = {})
|
71
|
+
options = prepare_query(query, options)
|
72
|
+
request "GET", query.path, options
|
73
|
+
end
|
74
|
+
|
75
|
+
def search_all(query, options = {}, &block)
|
76
|
+
options = prepare_query(query, options)
|
77
|
+
all query.path, options, &block
|
78
|
+
end
|
79
|
+
|
80
|
+
def strip_refname(refname)
|
81
|
+
refname.match(/('.*')/)[0].delete("'")
|
82
|
+
end
|
83
|
+
|
84
|
+
# parsed record and map to get restructured object
|
85
|
+
def to_object(record, attribute_map)
|
86
|
+
attributes = {}
|
87
|
+
attribute_map.each do |map|
|
88
|
+
if map["with"]
|
89
|
+
as = deep_find(record, map["key"], map["nested_key"])
|
90
|
+
values = []
|
91
|
+
if as.is_a? Array
|
92
|
+
values = as.map { |a| strip_refname( deep_find(a, map["with"]) ) }
|
93
|
+
elsif as.is_a? Hash and as[ map["with"] ]
|
94
|
+
values = as[ map["with"] ].is_a?(Array) ? as[ map["with"] ].map { |a| strip_refname(a) } : [ strip_refname(as[ map["with"] ]) ]
|
95
|
+
end
|
96
|
+
attributes[map["field"]] = values
|
97
|
+
else
|
98
|
+
attributes[map["field"]] = deep_find(record, map["key"], map["nested_key"])
|
99
|
+
end
|
100
|
+
end
|
101
|
+
attributes
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def get_list_types(path)
|
107
|
+
list_type, list_item = nil
|
108
|
+
if path == 'relations'
|
109
|
+
list_type = 'relations_common_list'
|
110
|
+
list_item = 'relation_list_item'
|
111
|
+
else
|
112
|
+
list_type = 'abstract_common_list'
|
113
|
+
list_item = 'list_item'
|
114
|
+
end
|
115
|
+
return list_type, list_item
|
116
|
+
end
|
117
|
+
|
118
|
+
def prepare_query(query, options = {})
|
119
|
+
query_string = "#{query.type}:#{query.field} #{query.expression}"
|
120
|
+
options = options.merge({ query: { as: query_string } })
|
121
|
+
options
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module CollectionSpace
|
2
|
+
|
3
|
+
class Request
|
4
|
+
include HTTParty
|
5
|
+
attr_reader :config, :headers, :method, :path, :options
|
6
|
+
|
7
|
+
def default_headers(method = :get)
|
8
|
+
headers = {
|
9
|
+
delete: {},
|
10
|
+
get: {},
|
11
|
+
post: {
|
12
|
+
"Content-Type" => "application/xml",
|
13
|
+
"Content-Length" => "nnnn",
|
14
|
+
},
|
15
|
+
put: {
|
16
|
+
"Content-Type" => "application/xml",
|
17
|
+
"Content-Length" => "nnnn",
|
18
|
+
}
|
19
|
+
}
|
20
|
+
headers[method]
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(config, method = "GET", path = "", options = {})
|
24
|
+
@config = config
|
25
|
+
@method = method.downcase.to_sym
|
26
|
+
@path = path
|
27
|
+
|
28
|
+
@auth = {
|
29
|
+
username: config.username,
|
30
|
+
password: config.password,
|
31
|
+
}
|
32
|
+
|
33
|
+
@options = options
|
34
|
+
@options[:basic_auth] = @auth
|
35
|
+
@options[:headers] = options[:headers] ? default_headers(@method).merge(options[:headers]) : default_headers
|
36
|
+
@options[:verify] = config.verify_ssl
|
37
|
+
@options[:query] = {} unless options.has_key? :query
|
38
|
+
|
39
|
+
self.class.base_uri config.base_uri
|
40
|
+
self.class.default_params wf_deleted: config.include_deleted, pgSz: config.page_size
|
41
|
+
end
|
42
|
+
|
43
|
+
def execute
|
44
|
+
self.class.send method, "/#{path}", options
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module CollectionSpace
|
2
|
+
|
3
|
+
class Response
|
4
|
+
attr_reader :result, :parsed, :body, :headers, :status, :status_code, :xml
|
5
|
+
|
6
|
+
def initialize(result)
|
7
|
+
# throw error
|
8
|
+
@result = result
|
9
|
+
@parsed = result.parsed_response
|
10
|
+
@body = result.body
|
11
|
+
@headers = result.headers
|
12
|
+
@status = result.response
|
13
|
+
@status_code = result.code.to_i
|
14
|
+
|
15
|
+
@xml = (@status_code.to_s =~ /^2/ and @body =~ /<?xml/) ? Nokogiri::XML(@body) : nil
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CollectionSpace
|
2
|
+
|
3
|
+
class Search
|
4
|
+
attr_accessor :path, :type, :field, :expression
|
5
|
+
|
6
|
+
def initialize(path: nil, type: nil, field: nil, expression: nil)
|
7
|
+
@path, @type, @field, @expression = path, type, field, expression
|
8
|
+
end
|
9
|
+
|
10
|
+
def from_hash(query)
|
11
|
+
query.each do |property, value|
|
12
|
+
instance_variable_set("@#{property}", value)
|
13
|
+
end
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: collectionspace-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Cooper
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: nokogiri
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: awesome_print
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- mark.cooper@lyrasis.org
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".travis.yml"
|
149
|
+
- Gemfile
|
150
|
+
- LICENSE.txt
|
151
|
+
- README.md
|
152
|
+
- Rakefile
|
153
|
+
- collectionspace-client.gemspec
|
154
|
+
- examples/demo.rb
|
155
|
+
- examples/export.rb
|
156
|
+
- examples/purge-empty-vocabularies.rb
|
157
|
+
- examples/search.rb
|
158
|
+
- lib/collectionspace/client.rb
|
159
|
+
- lib/collectionspace/client/client.rb
|
160
|
+
- lib/collectionspace/client/configuration.rb
|
161
|
+
- lib/collectionspace/client/helpers.rb
|
162
|
+
- lib/collectionspace/client/request.rb
|
163
|
+
- lib/collectionspace/client/response.rb
|
164
|
+
- lib/collectionspace/client/search.rb
|
165
|
+
- lib/collectionspace/client/version.rb
|
166
|
+
homepage: https://github.com/lyrasis/collectionspace-client.git
|
167
|
+
licenses:
|
168
|
+
- MIT
|
169
|
+
metadata: {}
|
170
|
+
post_install_message:
|
171
|
+
rdoc_options: []
|
172
|
+
require_paths:
|
173
|
+
- lib
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
requirements: []
|
185
|
+
rubyforge_project:
|
186
|
+
rubygems_version: 2.2.2
|
187
|
+
signing_key:
|
188
|
+
specification_version: 4
|
189
|
+
summary: CollectionSpace API client.
|
190
|
+
test_files: []
|