uri_service 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/uri_service/client.rb +99 -74
- data/lib/uri_service/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d86291e7f55d8b02a1bda38a3ce2442c57369ed
|
4
|
+
data.tar.gz: 1646c0c6ac51556df8823c9c23db7e1a64682692
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b9a6958a9f48b28a238d79d66ac490e24317d1ffffaa4bfa8d5a29d6fc075b75bcae0c6ff8d6963f61937f499288b69543adfc59d7b2539ee644c254cf90cf6
|
7
|
+
data.tar.gz: 3c50a6fd5cdf0d0f16092a8fd5be1e83a2af3a8d689cb9b580a339067374e1fc0dbcbe23836cfc7130d71e0b745d9cfa5d1cf170851aac951df09f247b352988
|
data/lib/uri_service/client.rb
CHANGED
@@ -92,22 +92,23 @@ class UriService::Client
|
|
92
92
|
##################
|
93
93
|
|
94
94
|
def create_vocabulary(string_key, display_label)
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
95
|
+
self.handle_database_disconnect do
|
96
|
+
if string_key.to_s == 'all'
|
97
|
+
# Note: There isn't currently a use case for searching across 'all' vocabularies, but I'm leaving this restriction as a placeholder in case that changes.
|
98
|
+
raise UriService::InvalidVocabularyStringKeyError, 'The value "all" is a reserved word and cannot be used as the string_key value for a vocabulary.'
|
99
|
+
end
|
100
|
+
unless string_key =~ ALPHANUMERIC_UNDERSCORE_KEY_REGEX
|
101
|
+
raise UriService::InvalidVocabularyStringKeyError, "Invalid key (can only include lower case letters, numbers or underscores, but cannot start with an underscore): " + string_key
|
102
|
+
end
|
103
|
+
|
104
|
+
@db.transaction do
|
105
|
+
begin
|
106
|
+
@db[UriService::VOCABULARIES].insert(string_key: string_key, display_label: display_label)
|
107
|
+
rescue Sequel::UniqueConstraintViolation
|
108
|
+
raise UriService::ExistingVocabularyStringKeyError, "A vocabulary already exists with string key: " + string_key
|
109
|
+
end
|
108
110
|
end
|
109
111
|
end
|
110
|
-
|
111
112
|
end
|
112
113
|
|
113
114
|
# Creates a new term.
|
@@ -136,35 +137,37 @@ class UriService::Client
|
|
136
137
|
end
|
137
138
|
|
138
139
|
def create_term_impl(vocabulary_string_key, value, term_uri, additional_fields, is_local)
|
140
|
+
self.handle_database_disconnect do
|
139
141
|
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
end
|
149
|
-
validate_additional_fields(additional_fields) # This method call raises an error if an invalid additional_field key is supplied
|
150
|
-
|
151
|
-
@db.transaction do
|
152
|
-
begin
|
153
|
-
@db[UriService::TERMS].insert(
|
154
|
-
is_local: is_local,
|
155
|
-
uri: term_uri,
|
156
|
-
uri_hash: Digest::SHA256.hexdigest(term_uri),
|
157
|
-
value: value,
|
158
|
-
vocabulary_string_key: vocabulary_string_key,
|
159
|
-
additional_fields: JSON.generate(additional_fields)
|
160
|
-
)
|
161
|
-
send_term_to_solr(vocabulary_string_key, value, term_uri, additional_fields, is_local)
|
162
|
-
rescue Sequel::UniqueConstraintViolation
|
163
|
-
raise UriService::ExistingUriError, "A term already exists with uri: " + term_uri + " (conflict found via uri_hash check)"
|
142
|
+
additional_fields.stringify_keys!
|
143
|
+
|
144
|
+
#Ensure that vocabulary with vocabulary_string_key exists
|
145
|
+
if self.find_vocabulary(vocabulary_string_key).nil?
|
146
|
+
raise UriService::NonExistentVocabularyError, "There is no vocabulary with string key: " + vocabulary_string_key
|
147
|
+
end
|
148
|
+
unless term_uri =~ VALID_URI_REGEX
|
149
|
+
raise UriService::InvalidUriError, "Invalid URI supplied: #{term_uri}, with result #{(VALID_URI_REGEX.match(term_uri)).to_s}"
|
164
150
|
end
|
151
|
+
validate_additional_fields(additional_fields) # This method call raises an error if an invalid additional_field key is supplied
|
152
|
+
|
153
|
+
@db.transaction do
|
154
|
+
begin
|
155
|
+
@db[UriService::TERMS].insert(
|
156
|
+
is_local: is_local,
|
157
|
+
uri: term_uri,
|
158
|
+
uri_hash: Digest::SHA256.hexdigest(term_uri),
|
159
|
+
value: value,
|
160
|
+
vocabulary_string_key: vocabulary_string_key,
|
161
|
+
additional_fields: JSON.generate(additional_fields)
|
162
|
+
)
|
163
|
+
send_term_to_solr(vocabulary_string_key, value, term_uri, additional_fields, is_local)
|
164
|
+
rescue Sequel::UniqueConstraintViolation
|
165
|
+
raise UriService::ExistingUriError, "A term already exists with uri: " + term_uri + " (conflict found via uri_hash check)"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
return generate_frozen_term_hash(vocabulary_string_key, value, term_uri, additional_fields, is_local)
|
165
170
|
end
|
166
|
-
|
167
|
-
return generate_frozen_term_hash(vocabulary_string_key, value, term_uri, additional_fields, is_local)
|
168
171
|
end
|
169
172
|
|
170
173
|
def generate_frozen_term_hash(vocabulary_string_key, value, uri, additional_fields, is_local)
|
@@ -245,7 +248,9 @@ class UriService::Client
|
|
245
248
|
################
|
246
249
|
|
247
250
|
def find_vocabulary(vocabulary_string_key)
|
248
|
-
|
251
|
+
self.handle_database_disconnect do
|
252
|
+
@db[UriService::VOCABULARIES].where(string_key: vocabulary_string_key).first
|
253
|
+
end
|
249
254
|
end
|
250
255
|
|
251
256
|
def find_term_by_uri(uri)
|
@@ -310,8 +315,10 @@ class UriService::Client
|
|
310
315
|
|
311
316
|
# Lists vocabularies alphabetically (by string key) and supports paging through results.
|
312
317
|
def list_vocabularies(limit=10, start=0)
|
313
|
-
|
314
|
-
|
318
|
+
self.handle_database_disconnect do
|
319
|
+
db_rows = @db[UriService::VOCABULARIES].order(:string_key).limit(limit, start)
|
320
|
+
return db_rows.map{|row| row.except(:id).stringify_keys!}
|
321
|
+
end
|
315
322
|
end
|
316
323
|
|
317
324
|
# Lists terms alphabetically and supports paging through results.
|
@@ -342,15 +349,19 @@ class UriService::Client
|
|
342
349
|
##################
|
343
350
|
|
344
351
|
def delete_vocabulary(vocabulary_string_key)
|
345
|
-
|
352
|
+
self.handle_database_disconnect do
|
353
|
+
@db[UriService::VOCABULARIES].where(string_key: vocabulary_string_key).delete
|
354
|
+
end
|
346
355
|
end
|
347
356
|
|
348
357
|
def delete_term(term_uri, commit=true)
|
349
|
-
|
350
|
-
@db
|
351
|
-
|
352
|
-
|
353
|
-
|
358
|
+
self.handle_database_disconnect do
|
359
|
+
@db.transaction do
|
360
|
+
@db[UriService::TERMS].where(uri: term_uri).delete
|
361
|
+
@rsolr_pool.with do |rsolr|
|
362
|
+
rsolr.delete_by_query('uri:' + UriService.solr_escape(term_uri))
|
363
|
+
rsolr.commit if commit
|
364
|
+
end
|
354
365
|
end
|
355
366
|
end
|
356
367
|
end
|
@@ -360,40 +371,54 @@ class UriService::Client
|
|
360
371
|
##################
|
361
372
|
|
362
373
|
def update_vocabulary(string_key, new_display_label)
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
374
|
+
self.handle_database_disconnect do
|
375
|
+
dataset = @db[UriService::VOCABULARIES].where(string_key: string_key)
|
376
|
+
raise UriService::NonExistentVocabularyError, "No vocabulary found with string_key: " + string_key if dataset.count == 0
|
377
|
+
|
378
|
+
@db.transaction do
|
379
|
+
dataset.update(display_label: new_display_label)
|
380
|
+
end
|
368
381
|
end
|
369
382
|
end
|
370
383
|
|
371
384
|
# opts format: {:value => 'new value', :additional_fields => {'key' => 'value'}}
|
372
385
|
def update_term(term_uri, opts, merge_additional_fields=true)
|
373
|
-
|
374
|
-
|
386
|
+
self.handle_database_disconnect do
|
387
|
+
dataset = @db[UriService::TERMS].where(uri: term_uri)
|
388
|
+
raise UriService::NonExistentUriError, "No term found with uri: " + term_uri if dataset.count == 0
|
389
|
+
|
390
|
+
term_db_row = dataset.first
|
375
391
|
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
new_additional_fields = opts[:additional_fields]
|
392
|
+
new_value = opts[:value] || term_db_row[:value]
|
393
|
+
new_additional_fields = term_db_row[:additional_fields].nil? ? {} : JSON.parse(term_db_row[:additional_fields])
|
394
|
+
|
395
|
+
unless opts[:additional_fields].nil?
|
396
|
+
if merge_additional_fields
|
397
|
+
new_additional_fields.merge!(opts[:additional_fields])
|
398
|
+
new_additional_fields.delete_if { |k, v| v.nil? } # Delete nil values. This is a way to clear data in additional_fields.
|
399
|
+
else
|
400
|
+
new_additional_fields = opts[:additional_fields]
|
401
|
+
end
|
387
402
|
end
|
403
|
+
validate_additional_fields(new_additional_fields)
|
404
|
+
|
405
|
+
@db.transaction do
|
406
|
+
dataset.update(value: new_value, additional_fields: JSON.generate(new_additional_fields))
|
407
|
+
self.send_term_to_solr(term_db_row[:vocabulary_string_key], new_value, term_uri, new_additional_fields, term_db_row[:is_local])
|
408
|
+
end
|
409
|
+
|
410
|
+
return generate_frozen_term_hash(term_db_row[:vocabulary_string_key], new_value, term_uri, new_additional_fields, term_db_row[:is_local])
|
388
411
|
end
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
412
|
+
end
|
413
|
+
|
414
|
+
def handle_database_disconnect
|
415
|
+
tries ||= 3
|
416
|
+
begin
|
417
|
+
yield
|
418
|
+
rescue Sequel::DatabaseDisconnectError
|
419
|
+
tries -= 1
|
420
|
+
retry unless tries == 0
|
394
421
|
end
|
395
|
-
|
396
|
-
return generate_frozen_term_hash(term_db_row[:vocabulary_string_key], new_value, term_uri, new_additional_fields, term_db_row[:is_local])
|
397
422
|
end
|
398
423
|
|
399
424
|
end
|
data/lib/uri_service/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uri_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eric O'Hanlon
|
@@ -202,3 +202,4 @@ specification_version: 4
|
|
202
202
|
summary: A service for registering local URIs and performing both local and remote
|
203
203
|
URI lookups.
|
204
204
|
test_files: []
|
205
|
+
has_rdoc:
|