triannon-client 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/triannon-client.rb +0 -7
- data/lib/triannon-client/configuration.rb +12 -2
- data/lib/triannon-client/triannon_client.rb +12 -10
- data/spec/lib/triannon-client/configuration_spec.rb +14 -1
- data/spec/lib/triannon-client/triannon_client_delete_spec.rb +5 -3
- data/spec/lib/triannon-client/triannon_client_read_spec.rb +19 -6
- data/triannon-client.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fe0f7bdd55fdabe693e858e8eac8237f1260edf
|
4
|
+
data.tar.gz: 3d54efa60a69ea4a16d947fa3fead12bdbf5de1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2901c4382bbfa9d52960d968b0c9af8ba21d385824ebca5229d328530e0f1e6f5e07bd688037a3305e5f43ebe0ccdcecfa276f0196cd95e0eaa82481210fa4c9
|
7
|
+
data.tar.gz: 07a790cde3dc8078be217cd6bdfded6385825db047850148b979acc58617b2e9a8c06b7b2aa99ee7e8333c6faf4895337e1abf3a2e56c421c4376d58acb1b0c0
|
data/README.md
CHANGED
@@ -32,8 +32,11 @@ bundle
|
|
32
32
|
|
33
33
|
Edit a `.env` file to configure the triannon server address etc.
|
34
34
|
(see .env_example); or use a configure block. The configuration
|
35
|
-
for authentication depends on prior triannon server configuration
|
36
|
-
|
35
|
+
for authentication depends on prior triannon server configuration, see
|
36
|
+
- https://github.com/sul-dlss/triannon#configuration
|
37
|
+
|
38
|
+
This example configuration may work with a triannon server running
|
39
|
+
on localhost in the development environment (see below for details).
|
37
40
|
|
38
41
|
```ruby
|
39
42
|
require 'triannon-client'
|
data/lib/triannon-client.rb
CHANGED
@@ -1,13 +1,6 @@
|
|
1
1
|
require 'dotenv'
|
2
2
|
Dotenv.load
|
3
3
|
|
4
|
-
# pry must be available when the client is configured to run in debug mode,
|
5
|
-
# where it will fall into a pry console for rescue blocks.
|
6
|
-
if ENV['DEBUG']
|
7
|
-
require 'pry'
|
8
|
-
require 'pry-doc'
|
9
|
-
end
|
10
|
-
|
11
4
|
# require rest client prior to linkeddata, so the latter can use it.
|
12
5
|
require 'rest-client'
|
13
6
|
# If a proxy is present, RestClient needs explicit configuration to use it.
|
@@ -22,7 +22,7 @@ module TriannonClient
|
|
22
22
|
attr_accessor :client_pass
|
23
23
|
|
24
24
|
def initialize
|
25
|
-
|
25
|
+
self.debug = env_boolean('DEBUG')
|
26
26
|
|
27
27
|
@host = ENV['TRIANNON_HOST'] || 'http://localhost:3000'
|
28
28
|
# @user = ENV['TRIANNON_USER'] || '' # triannon doesn't support basic auth
|
@@ -58,7 +58,17 @@ module TriannonClient
|
|
58
58
|
ENV[var].to_s.upcase == 'TRUE' rescue false
|
59
59
|
end
|
60
60
|
|
61
|
+
def debug=(bool)
|
62
|
+
raise ArgumentError unless [true, false].include?(bool)
|
63
|
+
@debug = bool
|
64
|
+
if @debug
|
65
|
+
# pry must be available when the client is configured to run in debug
|
66
|
+
# mode, where it will fall into a pry console for rescue blocks.
|
67
|
+
require 'pry'
|
68
|
+
require 'pry-doc'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
61
72
|
end
|
62
73
|
|
63
74
|
end
|
64
|
-
|
@@ -3,7 +3,6 @@ module TriannonClient
|
|
3
3
|
# Triannon may not support all content types in RDF::Format.content_types,
|
4
4
|
# but the client code is more generic using this as a reasonable set; this
|
5
5
|
# allows triannon to evolve support for anything supported by RDF::Format.
|
6
|
-
CONTENT_ERROR = 'content_type not found in RDF::Format.content_types'
|
7
6
|
CONTENT_TYPES = RDF::Format.content_types.keys
|
8
7
|
|
9
8
|
JSONLD_TYPE = 'application/ld+json'
|
@@ -90,10 +89,10 @@ module TriannonClient
|
|
90
89
|
# @param id [String] an annotation ID
|
91
90
|
# @response [true|false] true when successful
|
92
91
|
def delete_annotation(id)
|
93
|
-
check_id(id)
|
94
92
|
tries = 0
|
95
93
|
begin
|
96
94
|
tries += 1
|
95
|
+
check_id(id) if tries == 1
|
97
96
|
authenticate if tries == 2
|
98
97
|
response = @container[id].delete
|
99
98
|
# HTTP DELETE response codes: A successful response SHOULD be
|
@@ -165,9 +164,9 @@ module TriannonClient
|
|
165
164
|
# @param content_type [String] HTTP mime type (defaults to 'application/ld+json')
|
166
165
|
# @response [RDF::Graph] RDF::Graph of open annotations (can be empty on failure)
|
167
166
|
def get_annotations(content_type=JSONLD_TYPE)
|
168
|
-
content_type = check_content_type(content_type)
|
169
167
|
g = RDF::Graph.new
|
170
168
|
begin
|
169
|
+
content_type = check_content_type(content_type)
|
171
170
|
response = @container.get({:accept => content_type})
|
172
171
|
g = response2graph(response)
|
173
172
|
rescue => e
|
@@ -192,10 +191,10 @@ module TriannonClient
|
|
192
191
|
# @param content_type [String] HTTP mime type (defaults to 'application/ld+json')
|
193
192
|
# @response [RDF::Graph] RDF::Graph of the annotation (can be empty on failure)
|
194
193
|
def get_annotation(id, content_type=JSONLD_TYPE)
|
195
|
-
check_id(id)
|
196
|
-
content_type = check_content_type(content_type)
|
197
194
|
g = RDF::Graph.new
|
198
195
|
begin
|
196
|
+
check_id(id)
|
197
|
+
content_type = check_content_type(content_type)
|
199
198
|
response = @container[id].get({:accept => content_type})
|
200
199
|
g = response2graph(response)
|
201
200
|
rescue => e
|
@@ -238,10 +237,10 @@ module TriannonClient
|
|
238
237
|
unless response.is_a? RestClient::Response
|
239
238
|
raise ArgumentError, 'response2graph only accepts a RestClient::Response'
|
240
239
|
end
|
241
|
-
content_type = response.headers[:content_type]
|
242
|
-
content_type = check_content_type(content_type)
|
243
240
|
g = RDF::Graph.new
|
244
241
|
begin
|
242
|
+
content_type = response.headers[:content_type]
|
243
|
+
content_type = check_content_type(content_type)
|
245
244
|
case content_type
|
246
245
|
when /ld\+json/
|
247
246
|
g = RDF::Graph.new.from_jsonld(response.body)
|
@@ -283,13 +282,16 @@ module TriannonClient
|
|
283
282
|
|
284
283
|
def check_content_type(content_type)
|
285
284
|
type = content_type.split(';').first # strip off any parameters
|
286
|
-
|
285
|
+
unless CONTENT_TYPES.include? type
|
286
|
+
msg = "#{type} not found in RDF::Format.content_types"
|
287
|
+
raise ArgumentError, msg
|
288
|
+
end
|
287
289
|
type
|
288
290
|
end
|
289
291
|
|
290
292
|
def check_id(id)
|
291
|
-
|
292
|
-
raise ArgumentError,
|
293
|
+
invalid = ! id.instance_of?(String) || id.empty?
|
294
|
+
raise ArgumentError, "Invalid ID: #{id}" if invalid
|
293
295
|
end
|
294
296
|
|
295
297
|
def json_payloads
|
@@ -21,10 +21,23 @@ module TriannonClient
|
|
21
21
|
end
|
22
22
|
|
23
23
|
describe '#debug=' do
|
24
|
-
it 'can set value' do
|
24
|
+
it 'can set boolean value' do
|
25
25
|
config.debug = true
|
26
26
|
expect(config.debug).to be_truthy
|
27
27
|
end
|
28
|
+
it 'loads pry in debug mode' do
|
29
|
+
config.debug = true
|
30
|
+
defined?(Pry) == true
|
31
|
+
end
|
32
|
+
it 'does not load pry outside debug mode' do
|
33
|
+
config.debug = false
|
34
|
+
defined?(Pry) == false
|
35
|
+
end
|
36
|
+
it 'raises ArgumentError for non-boolean values' do
|
37
|
+
expect{config.debug='abc'}.to raise_error(ArgumentError)
|
38
|
+
expect{config.debug=nil}.to raise_error(ArgumentError)
|
39
|
+
expect{config.debug=0}.to raise_error(ArgumentError)
|
40
|
+
end
|
28
41
|
end
|
29
42
|
|
30
43
|
describe '#host' do
|
@@ -65,10 +65,12 @@ RSpec.shared_examples "delete annotations" do |auth_required|
|
|
65
65
|
expect(tc).to receive(:check_id)
|
66
66
|
tc.delete_annotation('checking_anno_id')
|
67
67
|
end
|
68
|
-
it '
|
68
|
+
it 'logs ArgumentError for an invalid annotation ID' do
|
69
69
|
expect_any_instance_of(RestClient::Resource).not_to receive(:delete)
|
70
|
-
expect
|
71
|
-
expect
|
70
|
+
expect(tc.config.logger).to receive(:error).with(/Invalid ID/).exactly(3).times
|
71
|
+
expect(tc.delete_annotation('')).to be false
|
72
|
+
expect(tc.delete_annotation(0)).to be false
|
73
|
+
expect(tc.delete_annotation(nil)).to be false
|
72
74
|
end
|
73
75
|
it 'uses RestClient::Resource.delete to DELETE a valid annotation ID' do
|
74
76
|
expect_any_instance_of(RestClient::Resource).to receive(:delete)
|
@@ -30,6 +30,8 @@ describe 'TriannonClientREAD', :vcr do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def cannot_get_anno_with_content_type(content_type)
|
33
|
+
expect(tc).to receive(:check_content_type)
|
34
|
+
expect(tc).not_to receive(:response2graph)
|
33
35
|
graph = tc.get_annotation(@anno[:id], content_type)
|
34
36
|
graph_is_empty(graph)
|
35
37
|
end
|
@@ -199,6 +201,12 @@ describe 'TriannonClientREAD', :vcr do
|
|
199
201
|
|
200
202
|
describe "annotation by ID" do
|
201
203
|
|
204
|
+
def invalid_annotation_id(id)
|
205
|
+
expect(tc.config.logger).to receive(:error).with(/Invalid ID/)
|
206
|
+
g = tc.get_annotation(id)
|
207
|
+
graph_is_empty(g)
|
208
|
+
end
|
209
|
+
|
202
210
|
context 'using default content type' do
|
203
211
|
|
204
212
|
it 'checks the annotation ID' do
|
@@ -206,14 +214,14 @@ describe 'TriannonClientREAD', :vcr do
|
|
206
214
|
graph = tc.get_annotation(@anno[:id])
|
207
215
|
graph_contains_statements(graph)
|
208
216
|
end
|
209
|
-
it '
|
210
|
-
|
217
|
+
it 'returns empty graph and logs exception with a nil ID' do
|
218
|
+
invalid_annotation_id(nil)
|
211
219
|
end
|
212
|
-
it '
|
213
|
-
|
220
|
+
it 'returns empty graph and logs exception with an integer ID' do
|
221
|
+
invalid_annotation_id(0)
|
214
222
|
end
|
215
|
-
it '
|
216
|
-
|
223
|
+
it 'returns empty graph and logs exception with an empty string ID' do
|
224
|
+
invalid_annotation_id('')
|
217
225
|
end
|
218
226
|
it 'requests an open annotation by ID, using JSON-LD content' do
|
219
227
|
jsonld = TriannonClient::TriannonClient::JSONLD_TYPE
|
@@ -254,6 +262,10 @@ describe 'TriannonClientREAD', :vcr do
|
|
254
262
|
end # using default content type
|
255
263
|
|
256
264
|
context 'using custom content type' do
|
265
|
+
it 'raises ArgumentError for unsupported content types' do
|
266
|
+
expect{tc.send(:check_content_type,'abc')}.to raise_error(ArgumentError)
|
267
|
+
end
|
268
|
+
|
257
269
|
# Content types could be supported for RDF::Format.content_types.keys,
|
258
270
|
# but not all of them are supported. The supported content types for
|
259
271
|
# triannon are defined in
|
@@ -268,6 +280,7 @@ describe 'TriannonClientREAD', :vcr do
|
|
268
280
|
# xml as: ["application/xml", "text/xml", "application/x-xml"]
|
269
281
|
# html
|
270
282
|
|
283
|
+
|
271
284
|
# The client supports any format in RDF::Format.content_types, so the
|
272
285
|
# following are not included (as of July 2015):
|
273
286
|
# "application/json" => true,
|
data/triannon-client.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: triannon-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darren Weber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv
|