qa 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +284 -2
  3. data/app/controllers/qa/linked_data_terms_controller.rb +127 -0
  4. data/config/authorities/linked_data/agrovoc.json +61 -0
  5. data/config/authorities/linked_data/loc.json +46 -0
  6. data/config/authorities/linked_data/oclc_fast.json +78 -0
  7. data/config/initializers/linked_data_authorities.rb +17 -0
  8. data/config/routes.rb +3 -0
  9. data/lib/qa.rb +15 -0
  10. data/lib/qa/authorities.rb +2 -0
  11. data/lib/qa/authorities/base.rb +1 -1
  12. data/lib/qa/authorities/crossref.rb +16 -0
  13. data/lib/qa/authorities/crossref/generic_authority.rb +63 -0
  14. data/lib/qa/authorities/geonames.rb +2 -1
  15. data/lib/qa/authorities/linked_data.rb +10 -0
  16. data/lib/qa/authorities/linked_data/config.rb +80 -0
  17. data/lib/qa/authorities/linked_data/config/search_config.rb +170 -0
  18. data/lib/qa/authorities/linked_data/config/term_config.rb +186 -0
  19. data/lib/qa/authorities/linked_data/find_term.rb +148 -0
  20. data/lib/qa/authorities/linked_data/generic_authority.rb +49 -0
  21. data/lib/qa/authorities/linked_data/rdf_helper.rb +102 -0
  22. data/lib/qa/authorities/linked_data/search_query.rb +143 -0
  23. data/lib/qa/version.rb +1 -1
  24. data/spec/controllers/linked_data_terms_controller_spec.rb +202 -0
  25. data/spec/fixtures/authorities/linked_data/lod_full_config.json +111 -0
  26. data/spec/fixtures/authorities/linked_data/lod_lang_defaults.json +54 -0
  27. data/spec/fixtures/authorities/linked_data/lod_lang_multi_defaults.json +54 -0
  28. data/spec/fixtures/authorities/linked_data/lod_lang_no_defaults.json +52 -0
  29. data/spec/fixtures/authorities/linked_data/lod_lang_param.json +66 -0
  30. data/spec/fixtures/authorities/linked_data/lod_min_config.json +49 -0
  31. data/spec/fixtures/authorities/linked_data/lod_search_only_config.json +55 -0
  32. data/spec/fixtures/authorities/linked_data/lod_sort.json +27 -0
  33. data/spec/fixtures/authorities/linked_data/lod_term_only_config.json +59 -0
  34. data/spec/fixtures/funders-find-response.json +1 -0
  35. data/spec/fixtures/funders-noquery.json +1 -0
  36. data/spec/fixtures/funders-noresults.json +1 -0
  37. data/spec/fixtures/funders-result.json +1 -0
  38. data/spec/fixtures/journals-find-response-two-issn.json +1 -0
  39. data/spec/fixtures/journals-find-response.json +1 -0
  40. data/spec/fixtures/journals-noquery.json +1 -0
  41. data/spec/fixtures/journals-noresults.json +1 -0
  42. data/spec/fixtures/journals-result.json +705 -0
  43. data/spec/fixtures/lod_agrovoc_query_many_results.json +1 -0
  44. data/spec/fixtures/lod_agrovoc_query_no_results.json +1 -0
  45. data/spec/fixtures/lod_agrovoc_term_found.rdf.xml +217 -0
  46. data/spec/fixtures/lod_lang_search_en.rdf.xml +42 -0
  47. data/spec/fixtures/lod_lang_search_enfr.rdf.xml +48 -0
  48. data/spec/fixtures/lod_lang_search_enfrde.rdf.xml +54 -0
  49. data/spec/fixtures/lod_lang_search_fr.rdf.xml +42 -0
  50. data/spec/fixtures/lod_lang_term_en.rdf.xml +65 -0
  51. data/spec/fixtures/lod_lang_term_enfr.rdf.xml +71 -0
  52. data/spec/fixtures/lod_lang_term_enfr_noalt.rdf.xml +69 -0
  53. data/spec/fixtures/lod_lang_term_enfrde.rdf.xml +79 -0
  54. data/spec/fixtures/lod_lang_term_fr.rdf.xml +65 -0
  55. data/spec/fixtures/lod_loc_term_found.rdf.xml +262 -0
  56. data/spec/fixtures/lod_oclc_all_query_3_results.rdf.xml +142 -0
  57. data/spec/fixtures/lod_oclc_personalName_query_3_results.rdf.xml +128 -0
  58. data/spec/fixtures/lod_oclc_query_no_results.rdf.xml +13 -0
  59. data/spec/fixtures/lod_oclc_term_found.rdf.xml +51 -0
  60. data/spec/lib/authorities/crossref_spec.rb +180 -0
  61. data/spec/lib/authorities/geonames_spec.rb +2 -2
  62. data/spec/lib/authorities/linked_data/config_spec.rb +143 -0
  63. data/spec/lib/authorities/linked_data/find_term_spec.rb +5 -0
  64. data/spec/lib/authorities/linked_data/generic_authority_spec.rb +580 -0
  65. data/spec/lib/authorities/linked_data/search_config_spec.rb +385 -0
  66. data/spec/lib/authorities/linked_data/search_query_spec.rb +79 -0
  67. data/spec/lib/authorities/linked_data/term_config_spec.rb +419 -0
  68. data/spec/routing/linked_data_route_spec.rb +35 -0
  69. data/spec/spec_helper.rb +2 -0
  70. metadata +184 -39
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aaf2ea321ec50c2790d269dee43856176fcc22e1
4
- data.tar.gz: 18d617298ba9675024a9003c2f4183040cc535c8
3
+ metadata.gz: 571cac6a8c4ec9ddf8a61a7d54ea857481af0a1c
4
+ data.tar.gz: e33cead405236f73a1dc443ee11c48a132f64b30
5
5
  SHA512:
6
- metadata.gz: cec86fa96e3c2c767eeb8b7a7866a3269d975659bc872f5acacde03448385584351420b3c4c6f8f4a6275b8b9b7a8cf9259b027e8c1212a286877ceabcdf156d
7
- data.tar.gz: 3a1d18ec438ac477ab6b22a4f08db1b14dc83d8b348a36d754530f1ba5b799747b0a5ffea9b3f8864fd131289e917d76de45b2aa7685f96e350dbee6aab863ee
6
+ metadata.gz: ccd4534d37546bbfc12514de7ba0058668fc1f380d6eab4a9889c45d34ea035df73dc6e2977a14ae273d215c24ff02b6228e82593aa8c51f3a5bb333e6112f28
7
+ data.tar.gz: af6074497e2bfeafdf3f9c9fd71d13e45c7917889084d241174059d5e3062697589286edb4f6260603414ec0d22d771683695d91e26a8b390d03bfbbd21dbce5
data/README.md CHANGED
@@ -1,10 +1,38 @@
1
1
  # Questioning Authority
2
2
 
3
- [![Build Status](https://travis-ci.org/projecthydra-labs/questioning_authority.png?branch=master)](https://travis-ci.org/projecthydra-labs/questioning_authority) [![Gem Version](https://badge.fury.io/rb/qa.png)](http://badge.fury.io/rb/qa)
4
- [![Coverage Status](https://coveralls.io/repos/github/projecthydra-labs/questioning_authority/badge.svg?branch=master)](https://coveralls.io/github/projecthydra-labs/questioning_authority?branch=master)
3
+ [![Build Status](https://travis-ci.org/projecthydra/questioning_authority.png?branch=master)](https://travis-ci.org/projecthydra/questioning_authority) [![Gem Version](https://badge.fury.io/rb/qa.png)](http://badge.fury.io/rb/qa)
4
+ [![Coverage Status](https://coveralls.io/repos/github/projecthydra/questioning_authority/badge.svg?branch=master)](https://coveralls.io/github/projecthydra/questioning_authority?branch=master)
5
5
 
6
6
  You should question your authorities.
7
7
 
8
+ ----
9
+ ## Table of Contents
10
+
11
+ * [What does this do?](#what-does-this-do)
12
+ * [How does it work?](#how-does-it-work)
13
+ * [Sub-Authorities](#sub-authorities)
14
+ * [How do I use this?](#how-do-i-use-this)
15
+ * [Examples](#examples)
16
+ * [JSON Results](#json-results)
17
+ * [Authority Sources information](#authority-sources-information)
18
+ * [FAST](#fast)
19
+ * [Geonames](#geonames)
20
+ * [Adding your own authorities](#adding-your-own-authorities)
21
+ * [Local Sub-Authorities](#local-sub-authorities)
22
+ * [In YAML files](#in-yaml-files)
23
+ * [In database tables](#in-database-tables)
24
+ * [Medical Subject Headings (Mesh)](#medical-subject-headings-mesh)
25
+ * [Linked Open Data (LOD) Authorities](#linked-open-data-lod-authorities)
26
+ * [Configuring a LOD Authority](#configuring-a-lod-authority)
27
+ * [Query](#query)
28
+ * [Find term](#find-term)
29
+ * [Add javascript to support autocomplete](#add-javascript-to-support-autocomplete)
30
+ * [Developer Notes](#developer-notes)
31
+ * [Compatibility](#compatibility)
32
+ * [Help](#help)
33
+
34
+ ----
35
+
8
36
  ## What does this do?
9
37
 
10
38
  Provides a set of uniform RESTful routes to query any controlled vocabulary or set of authority terms.
@@ -333,6 +361,260 @@ This may take a few minutes to finish.
333
361
 
334
362
  **Note:** Updating the tables with new terms is currently not supported.
335
363
 
364
+ ### Linked Open Data (LOD) Authorities
365
+
366
+ You will need to add gems that process the type of linked data returned for the authorities you use.
367
+
368
+ To cover all possible formats, include the [ruby-rdf/linkeddata](https://github.com/ruby-rdf/linkeddata) gem.
369
+
370
+ ```
371
+ gem 'linkeddata'
372
+ ```
373
+
374
+ This gem is included in QA for development and testing of QA, but is not automatically included in the released gem.
375
+ Additionally, it is unlikely that you will need all the formats included by that gem. You may want to select only those
376
+ gems that are for the formats you need supported.
377
+
378
+ See all gems in [linkeddata.gemspec](https://github.com/ruby-rdf/linkeddata/blob/develop/linkeddata.gemspec).
379
+
380
+ For example, if you know the authorites you are working with support rdf-xml, you can include the following gem instead of linkeddata.
381
+
382
+ ```
383
+ gem 'rdf-rdfxml'
384
+ ```
385
+
386
+ #### Configuring a LOD Authority
387
+
388
+ Access to LOD authorities can be configured. Currently, a configuration exists in QA for OCLC Fast Linked Data, Library of
389
+ Congress (terms only), and Agrovoc. Look for configuration files in
390
+ [/config/authorities/linked_data](https://github.com/projecthydra/questioning_authority/tree/master/config/authorities/linked_data).
391
+
392
+ Example configuration...
393
+
394
+ ```json
395
+ {
396
+ "term": {
397
+ "url": {
398
+ "@context": "http://www.w3.org/ns/hydra/context.jsonld",
399
+ "@type": "IriTemplate",
400
+ "template": "http://id.worldcat.org/fast/{?term_id}/rdf.xml",
401
+ "variableRepresentation": "BasicRepresentation",
402
+ "mapping": [
403
+ {
404
+ "@type": "IriTemplateMapping",
405
+ "variable": "term_id",
406
+ "property": "hydra:freetextQuery",
407
+ "required": true
408
+ }
409
+ ]
410
+ },
411
+ "qa_replacement_patterns": {
412
+ "term_id": "term_id"
413
+ },
414
+ "language": ["en","fr"]
415
+ "term_id": "ID",
416
+ "results": {
417
+ "id_predicate": "http://purl.org/dc/terms/identifier",
418
+ "label_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel",
419
+ "altlabel_predicate": "http://www.w3.org/2004/02/skos/core#altLabel",
420
+ "sameas_predicate": "http://schema.org/sameAs"
421
+ }
422
+ },
423
+ "search": {
424
+ "url": {
425
+ "@context": "http://www.w3.org/ns/hydra/context.jsonld",
426
+ "@type": "IriTemplate",
427
+ "template": "http://experimental.worldcat.org/fast/search?query={?subauth}+all+%22{?query}%22&sortKeys=usage&maximumRecords={?maximumRecords}",
428
+ "variableRepresentation": "BasicRepresentation",
429
+ "mapping": [
430
+ {
431
+ "@type": "IriTemplateMapping",
432
+ "variable": "query",
433
+ "property": "hydra:freetextQuery",
434
+ "required": true
435
+ },
436
+ {
437
+ "@type": "IriTemplateMapping",
438
+ "variable": "subauth",
439
+ "property": "hydra:freetextQuery",
440
+ "required": false,
441
+ "default": "cql.any"
442
+ },
443
+ {
444
+ "@type": "IriTemplateMapping",
445
+ "variable": "maximumRecords",
446
+ "property": "hydra:freetextQuery",
447
+ "required": false,
448
+ "default": "20"
449
+ }
450
+ ]
451
+ },
452
+ "qa_replacement_patterns": {
453
+ "query": "query",
454
+ "subauth": "subauth"
455
+ },
456
+ "language": ["en"]
457
+ "results": {
458
+ "id_predicate": "http://purl.org/dc/terms/identifier",
459
+ "label_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel",
460
+ "sort_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel"
461
+ },
462
+ "subauthorities": {
463
+ "topic": "oclc.topic",
464
+ "geographic": "oclc.geographic",
465
+ "event_name": "oclc.eventName",
466
+ "personal_name": "oclc.personalName",
467
+ "corporate_name": "oclc.corporateName",
468
+ "uniform_title": "oclc.uniformTitle",
469
+ "period": "oclc.period",
470
+ "form": "oclc.form",
471
+ "alt_lc": "oclc.altlc"
472
+ }
473
+ }
474
+ }
475
+ ```
476
+
477
+ NOTES:
478
+ * term: (optional) is used to define how to request term information from the authority and how to interpret results.
479
+ * url: (required) templated link representation of the authority API URL and mapping of parameters for requesting term information from the authority
480
+ * template: is the authority API URL with placeholders for substitution parameters in the form {?var_name}
481
+ * NOTE: {?term_id} (required) and {?subauth} (optional) are expected to match to QA params (see qa_replacement_patterns to match QA params with mapping variables)
482
+ * Additional substitutions can be made in the authority API if supported by the authority by adding additional mappings. Search has an example with maximumRecords.
483
+ * variable: should match a replacement pattern in the template (e.g. variable: maximumRecords ==> {?maximumRecords}
484
+ * required: true | false (NOTE: Not enforced at this time.)
485
+ * default: provide a default value that will be used if not specified
486
+ * See (documentation of templated-links)[http://www.hydra-cg.com/spec/latest/core/#templated-links] for more information.
487
+ * qa_replacement_patterns: identifies which mapping variables are being used for term_id and subauth.
488
+ * NOTE: The URL to make a term request via QA always uses term_id and subauth as the param names. qa_replacement_patters allows the url template to use a different variable name for pattern replacement.
489
+ * language: (optional) values: array of en | fr | etc. -- identify language to use to include in results, filtering out triples of other languages
490
+ * NOTE: Some authoritys' API URL allows language to be specified as a parameter. In that case, use pattern replacement to add the language to the API URL to prevent alternate languages from being returned in the results.
491
+ * NOTE: At this writing, only label and altlabel are filtered.
492
+ * term_id: (optional) values: ID (default) | URI - This tells apps whether `__TERM_ID__` replacement is expecting an ID or URI.
493
+ * results: (required) lists predicates to select out for normalization in the hash results
494
+ * id_predicate: (optional)
495
+ * label_predicate: (required)
496
+ * altlabel_predicate: (optional)
497
+ * sameas_predicate: (optional)
498
+ * narrower_predicate: (optional)
499
+ * broader_predicate: (optional)
500
+ * subauthorities: (optional)
501
+ * subauthority name (e.g. topic:, personal_name:, corporate_name, etc.) Value for {?subauth} are limited to the values in the list of subauthorities.
502
+
503
+ * search: (optional) is used to define how to send a query to the authority and how to interpret results.
504
+ * url: (required) templated link representation of the authority API URL and mapping of parameters for sending a query to the authority
505
+ * template: is the authority API URL with placeholders for substitution parameters in the form {?var_name}
506
+ * NOTE: {?query} (required) and {?subauth} (optional) are expected to match to QA params (see qa_replacement_patterns to match QA params with mapping variables)
507
+ * Additional substitutions can be made in the authority API if supported by the authority by adding additional mappings. Search has an example with maximumRecords.
508
+ * variable: should match a replacement pattern in the template (e.g. variable: maximumRecords ==> {?maximumRecords}
509
+ * required: true | false (NOTE: Not enforced at this time.)
510
+ * default: provide a default value that will be used if not specified
511
+ * See (documentation of templated-links)[http://www.hydra-cg.com/spec/latest/core/#templated-links] for more information.
512
+ * qa_replacement_patterns: identifies which mapping variables are being used for term_id and subauth.
513
+ * NOTE: The URL to make a term request via QA always uses term_id and subauth as the param names. qa_replacement_patters allows the url template to use a different variable name for pattern replacement.
514
+ * language: (optional) values: array of en | fr | etc. -- identify language to use to include in results, filtering out triples of other languages
515
+ * NOTE: Some authoritys' API URL allows language to be specified as a parameter. In that case, use pattern replacement to add the language to the API URL to prevent alternate languages from being returned in the results.
516
+ * NOTE: At this writing, only label and altlabel are filtered.
517
+ * results: (required) lists predicates to normalize and include in json results
518
+ * id_predicate: (optional)
519
+ * label_predicate: (required)
520
+ * altlabel_predicate: (optional)
521
+ * subauthorities: (optional)
522
+ * subauthority name (e.g. topic:, personal_name:, corporate_name, etc.) Value for {?subauth} are limited to the values in the list of subauthorities.
523
+
524
+
525
+ ##### Add new configuration
526
+ You can add linked data authorities by adding configuration files to your rails app in `Rails.root/config/authorities/linked_data/YOUR_AUTH.json`
527
+
528
+ ##### Modify existing configuration
529
+ To modify one of the QA supplied configurations, copy it to your app in `Rails.root/config/authorities/linked_data/YOUR_AUTH.json`. Make your modifications to the json configuration file in your app.
530
+
531
+ #### Query
532
+ To query OCLC Fast Linked Data service by code...
533
+
534
+ ```ruby
535
+ # Search OCLC Fast all sub-authorities with default value for number of results to return
536
+ lda = Qa::Authorities::LinkedData::GenericAuthority.new(:OCLC_FAST)
537
+ ld_results = lda.search "Cornell University"
538
+
539
+ # Search OCLC Fast all sub-authorities passing in value for number of results to return
540
+ lda = Qa::Authorities::LinkedData::GenericAuthority.new(:OCLC_FAST)
541
+ ld_results = lda.search "Cornell University",{"maximumRecords" => "5"}
542
+
543
+ # Search OCLC Fast Corporate Name sub-authority passing in value for number of results to return
544
+ lda = Qa::Authorities::LinkedData::GenericAuthority.new(:OCLC_FAST,'corporate_name')
545
+ ld_results = lda.search "Cornell University",{"maximumRecords" => "3"}
546
+ ```
547
+
548
+ or by URL when QA is an installed gem in an app...
549
+
550
+ ```
551
+ http://localhost:3000/qa/search/linked_data/oclc_fast?q=Cornell&maximumRecords=3
552
+ ```
553
+
554
+ Returns results in the format...
555
+
556
+ ```json
557
+ [{"uri":"http://id.worldcat.org/fast/530369","id":"530369","label":"Cornell University"},
558
+ {"uri":"http://id.worldcat.org/fast/5140","id":"5140","label":"Cornell, Joseph"},
559
+ {"uri":"http://id.worldcat.org/fast/557490","id":"557490","label":"New York State School of Industrial and Labor Relations"}]
560
+ ```
561
+
562
+ NOTE: For some authorities, the uri and id will both be the uri.
563
+
564
+ and with subauthority...
565
+
566
+ ```
567
+ http://localhost:3000/qa/search/linked_data/oclc_fast/personal_name?q=Cornell&maximumRecords=3
568
+ ```
569
+
570
+ returning results...
571
+
572
+ ```json
573
+ [{"uri":"http://id.worldcat.org/fast/5140","id":"5140","label":"Cornell, Joseph"},
574
+ {"uri":"http://id.worldcat.org/fast/72456","id":"72456","label":"Cornell, Sarah Maria, 1802-1832"},
575
+ {"uri":"http://id.worldcat.org/fast/409667","id":"409667","label":"Cornell, Ezra, 1807-1874"}]
576
+ ```
577
+
578
+ #### Find term
579
+ To find a single term in OCLC Fast Linked Data service by code...
580
+
581
+ ```ruby
582
+ # Search OCLC Fast all sub-authorities with default value for number of results to return
583
+ lda = Qa::Authorities::LinkedData::GenericAuthority.new(:OCLC_FAST_ALL)
584
+ ld_results = lda.find 530369
585
+ ```
586
+
587
+ or by URL when QA is an installed gem in an app...
588
+
589
+ ```
590
+ http://localhost:3000/qa/show/linked_data/oclc_fast/530369
591
+ ```
592
+
593
+ Returns results in the format...
594
+
595
+ ```json
596
+ {"uri":"http://id.worldcat.org/fast/530369",
597
+ "id":"530369","label":"Cornell University",
598
+ "altlabel":["Ithaca (N.Y.). Cornell University","Kornelʹskii universitet","Kʻang-nai-erh ta hsüeh"],
599
+ "sameas":["http://id.loc.gov/authorities/names/n79021621","https://viaf.org/viaf/126293486"],
600
+ "predicates":{
601
+ "http://purl.org/dc/terms/identifier":"530369",
602
+ "http://www.w3.org/2004/02/skos/core#inScheme":["http://id.worldcat.org/fast/ontology/1.0/#fast","http://id.worldcat.org/fast/ontology/1.0/#facet-Corporate"],
603
+ "http://www.w3.org/1999/02/22-rdf-syntax-ns#type":"http://schema.org/Organization",
604
+ "http://www.w3.org/2004/02/skos/core#prefLabel":"Cornell University",
605
+ "http://schema.org/name":["Cornell University","Ithaca (N.Y.). Cornell University","Kornelʹskii universitet","Kʻang-nai-erh ta hsüeh"],
606
+ "http://www.w3.org/2004/02/skos/core#altLabel":["Ithaca (N.Y.). Cornell University","Kornelʹskii universitet","Kʻang-nai-erh ta hsüeh"],
607
+ "http://schema.org/sameAs":["http://id.loc.gov/authorities/names/n79021621","https://viaf.org/viaf/126293486"]}}
608
+ ```
609
+
610
+ NOTE: All predicates with the URI as the subject will be included under "predicates" key. The selected keys are determined by the configuration file and can be one or more of id_predicate, label_predicate (required), altlabel_predicate, sameas_predicate, narrower_predicate, or broader_predicate.
611
+
612
+ #### Add javascript to support autocomplete
613
+
614
+ See [Using with autocomplete in Sufia](https://github.com/projecthydra/questioning_authority/wiki/Using-with-autocomplete-in-Sufia) in the wiki documentation for QA.
615
+
616
+
617
+
336
618
  # Developer Notes
337
619
 
338
620
  [How to Contribute](./CONTRIBUTING.md)
@@ -0,0 +1,127 @@
1
+ # This controller is used for all requests to linked data authorities. It will verify params and figure
2
+ # out which linked data authority to query based on the 'vocab' param.
3
+
4
+ class Qa::LinkedDataTermsController < ApplicationController
5
+ before_action :check_authority, :init_authority
6
+ before_action :check_search_subauthority, :check_query_param, only: :search
7
+ before_action :check_show_subauthority, :check_id_param, only: :show
8
+
9
+ # Provide a warning if there is a request for all terms.
10
+ def index
11
+ logger.warn 'Linked data authorities do not support retrieving all terms.'
12
+ head :not_found
13
+ end
14
+
15
+ # Return a list of terms based on a query
16
+ # @see Qa::Authorities::LinkedData::SearchQuery#search
17
+ def search
18
+ begin
19
+ terms = @authority.search(query, subauth: subauthority, language: language, replacements: replacement_params)
20
+ rescue Qa::ServiceUnavailable
21
+ logger.warn "Service Unavailable - Search query #{query} unsuccessful for#{subauth_warn_msg} authority #{vocab_param}"
22
+ head :not_found
23
+ return
24
+ rescue RDF::FormatError
25
+ logger.warn "RDF Format Error - Results from search query #{query} for#{subauth_warn_msg} authority #{vocab_param} was not identified as a valid RDF format. You may need to include the linkeddata gem."
26
+ head :not_found
27
+ return
28
+ end
29
+ render json: terms
30
+ end
31
+
32
+ # Return all the information for a given term
33
+ # @see Qa::Authorities::LinkedData::FindTerm#find
34
+ def show
35
+ begin
36
+ term = @authority.find(id, subauth: subauthority, language: language, replacements: replacement_params)
37
+ rescue Qa::TermNotFound
38
+ logger.warn "Term Not Found - Fetch term #{id} unsuccessful for#{subauth_warn_msg} authority #{vocab_param}"
39
+ head :not_found
40
+ return
41
+ rescue Qa::ServiceUnavailable
42
+ logger.warn "Service Unavailable - Fetch term #{id} unsuccessful for#{subauth_warn_msg} authority #{vocab_param}"
43
+ head :not_found
44
+ return
45
+ rescue RDF::FormatError
46
+ logger.warn "RDF Format Error - Results from fetch term #{id} for#{subauth_warn_msg} authority #{vocab_param} was not identified as a valid RDF format. You may need to include the linkeddata gem."
47
+ head :not_found
48
+ return
49
+ end
50
+ render json: term
51
+ end
52
+
53
+ private
54
+
55
+ def check_authority
56
+ if params[:vocab].nil? || !params[:vocab].size.positive?
57
+ logger.warn "Required param 'vocab' is missing or empty"
58
+ head :not_found
59
+ end
60
+ end
61
+
62
+ def check_search_subauthority
63
+ return if subauthority.nil?
64
+ unless @authority.search_subauthority?(subauthority)
65
+ logger.warn "Unable to initialize linked data search sub-authority '#{subauthority}' for authority '#{vocab_param}'"
66
+ head :not_found
67
+ end
68
+ end
69
+
70
+ def check_show_subauthority
71
+ return if subauthority.nil?
72
+ unless @authority.term_subauthority?(subauthority)
73
+ logger.warn "Unable to initialize linked data term sub-authority '#{subauthority}' for authority '#{vocab_param}'"
74
+ head :not_found
75
+ end
76
+ end
77
+
78
+ def init_authority
79
+ @authority = Qa::Authorities::LinkedData::GenericAuthority.new(vocab_param)
80
+ rescue Qa::InvalidLinkedDataAuthority => e
81
+ logger.warn e.message
82
+ head :not_found
83
+ end
84
+
85
+ def vocab_param
86
+ params[:vocab].upcase.to_sym
87
+ end
88
+
89
+ def check_query_param
90
+ if params[:q].nil? || !params[:q].size.positive?
91
+ logger.warn "Required search param 'q' is missing or empty"
92
+ head :not_found
93
+ end
94
+ end
95
+
96
+ # converts wildcards into URL-encoded characters
97
+ def query
98
+ params[:q].gsub("*", "%2A")
99
+ end
100
+
101
+ def check_id_param
102
+ if params[:id].nil? || !params[:id].size.positive?
103
+ logger.warn "Required show param 'id' is missing or empty"
104
+ head :not_found
105
+ end
106
+ end
107
+
108
+ def id
109
+ params[:id]
110
+ end
111
+
112
+ def language
113
+ params[:lang]
114
+ end
115
+
116
+ def subauthority
117
+ params[:subauthority]
118
+ end
119
+
120
+ def replacement_params
121
+ params.reject { |k, _v| ['q', 'vocab', 'controller', 'action', 'subauthority', 'language', 'id'].include?(k) }
122
+ end
123
+
124
+ def subauth_warn_msg
125
+ subauthority.nil? ? "" : " sub-authority #{subauthority} in"
126
+ end
127
+ end
@@ -0,0 +1,61 @@
1
+ {
2
+ "term": {
3
+ "url": {
4
+ "@context": "http://www.w3.org/ns/hydra/context.jsonld",
5
+ "@type": "IriTemplate",
6
+ "template": "http://aims.fao.org/skosmos/rest/v1/data?uri=http://aims.fao.org/aos/agrovoc/{?term_id}",
7
+ "variableRepresentation": "BasicRepresentation",
8
+ "mapping": [
9
+ {
10
+ "@type": "IriTemplateMapping",
11
+ "variable": "term_id",
12
+ "property": "hydra:freetextQuery",
13
+ "required": true
14
+ }
15
+ ]
16
+ },
17
+ "qa_replacement_patterns": {
18
+ "term_id": "term_id"
19
+ },
20
+ "term_id": "ID",
21
+ "language": ["en"],
22
+ "results": {
23
+ "label_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel",
24
+ "altlabel_predicate": "http://www.w3.org/2004/02/skos/core#altLabel",
25
+ "broader_predicate": "http://www.w3.org/2004/02/skos/core#broader",
26
+ "narrower_predicate": "http://www.w3.org/2004/02/skos/core#narrower",
27
+ "sameas_predicate": "http://www.w3.org/2004/02/skos/core#exactMatch"
28
+ }
29
+ },
30
+ "search": {
31
+ "url": {
32
+ "@context": "http://www.w3.org/ns/hydra/context.jsonld",
33
+ "@type": "IriTemplate",
34
+ "template": "http://aims.fao.org/skosmos/rest/v1/search/?query=*{?query}*&lang={?lang}",
35
+ "variableRepresentation": "BasicRepresentation",
36
+ "mapping": [
37
+ {
38
+ "@type": "IriTemplateMapping",
39
+ "variable": "query",
40
+ "property": "hydra:freetextQuery",
41
+ "required": true
42
+ },
43
+ {
44
+ "@type": "IriTemplateMapping",
45
+ "variable": "lang",
46
+ "property": "hydra:freetextQuery",
47
+ "required": false,
48
+ "default": "en"
49
+ }
50
+ ]
51
+ },
52
+ "qa_replacement_patterns": {
53
+ "query": "query"
54
+ },
55
+ "results": {
56
+ "id_predicate": "http://purl.org/dc/terms/identifier",
57
+ "label_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel",
58
+ "sort_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel"
59
+ }
60
+ }
61
+ }