Skip to content

Commit

Permalink
Merge pull request #208 from samvera/param_parse_template_url
Browse files Browse the repository at this point in the history
Add versioning to linked data configs
  • Loading branch information
elrayle committed Mar 6, 2019
2 parents 237cdc4 + 576f86b commit 9652d9b
Show file tree
Hide file tree
Showing 24 changed files with 447 additions and 49 deletions.
20 changes: 9 additions & 11 deletions app/models/qa/iri_template/variable_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def initialize(variable_map)
@property = Qa::LinkedData::Config::Helper.fetch(variable_map, :property, 'hydra:freetextQuery')
end

# TODO: When implementing more complex query substitution, simple_value is used when template url specifies variable as {var_name}.
# Value to use in substitution, using default if one isn't passed in
# Value to use in substitution, using default if one isn't passed in. Use when template url specifies variable as {var_name}.
# @param [Object] value to use if it exists
# @return the value to use (e.g. 'fr')
def simple_value(sub_value = nil)
Expand All @@ -39,15 +38,14 @@ def simple_value(sub_value = nil)
sub_value
end

# TODO: When implementing more complex query substitution, parameter_value is used when template url specifies variable as {?var_name}.
# # Parameter and value to use in substitution, using default is one isn't passed in
# # @param [Object] value to use if it exists
# # @return the parameter and value to use (e.g. 'language=fr')
# def parameter_value(sub_value = nil)
# simple_value = simple_value(sub_value)
# return '' if simple_value.blank?
# param_value = "#{variable}=#{simple_value}"
# end
# Parameter and value to use in substitution, using default is one isn't passed in. Use when template url specifies variable as {?var_name}.
# @param [Object] value to use if it exists
# @return the parameter and value to use (e.g. 'language=fr')
def parameter_value(sub_value = nil)
simple_value = simple_value(sub_value)
return '' if simple_value.blank?
"#{variable}=#{simple_value}"
end

private

Expand Down
7 changes: 2 additions & 5 deletions app/services/qa/iri_template_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class IriTemplateService
def self.build_url(url_config:, substitutions:)
# TODO: This is a very simple approach using direct substitution into the template string.
# Better would be to...
# * pattern {var_name} = simple value substitution in place of pattern produces 'value'
# * pattern {?var_name} = parameter substitution in place of pattern produces 'var_name=value'
# * patterns without a substitution are not included in the resulting URL
# * appropriately adds '?' or '&'
# * ensure proper escaping of values (e.g. value="A simple string" which is encoded as A%20simple%20string)
Expand All @@ -24,9 +22,8 @@ def self.build_url(url_config:, substitutions:)
url = url_config.template
url_config.mapping.each do |m|
key = m.variable
url = url.gsub("{?#{key}}", m.simple_value(substitutions[key])) # Incorrectly applies pattern {?var_name} to produce substitution 'value'
# url.gsub("{#{key}}", m.simple_value(substitutions[key])) # TODO: pattern {var_name} should produce substitution 'value'
# url.gsub("{?#{key}}", m.parameter_value(substitutions[key])) # TODO: pattern {?var_name} should produce substitution 'var_name=value'
url = url.gsub("{#{key}}", m.simple_value(substitutions[key]))
url = url.gsub("{?#{key}}", m.parameter_value(substitutions[key]))
end
url
end
Expand Down
7 changes: 5 additions & 2 deletions config/authorities/linked_data/loc.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"QA_CONFIG_VERSION": "2.0",
"term": {
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://id.loc.gov/authorities/{?subauth}/{?term_id}",
"template": "http://id.loc.gov/authorities/{subauth}/{term_id}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand Down Expand Up @@ -31,7 +32,9 @@
"id_predicate": "http://id.loc.gov/vocabulary/identifiers/lccn",
"label_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel",
"altlabel_predicate": "http://www.w3.org/2004/02/skos/core#altLabel",
"sameas_predicate": "http://www.w3.org/2004/02/skos/core#exactMatch"
"sameas_predicate": "http://www.w3.org/2004/02/skos/core#exactMatch",
"narrower_predicate": "http://www.loc.gov/mads/rdf/v1#hasNarrowerAuthority",
"broader_predicate": "http://www.loc.gov/mads/rdf/v1#hasBroaderAuthority"
},
"subauthorities": {
"subjects": "subjects",
Expand Down
5 changes: 3 additions & 2 deletions config/authorities/linked_data/oclc_fast.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"QA_CONFIG_VERSION": "2.0",
"term": {
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://id.worldcat.org/fast/{?term_id}",
"template": "http://id.worldcat.org/fast/{term_id}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand All @@ -29,7 +30,7 @@
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://experimental.worldcat.org/fast/search?query={?subauth}+all+%22{?query}%22&sortKeys=usage&maximumRecords={?maximumRecords}",
"template": "http://experimental.worldcat.org/fast/search?query={subauth}+all+%22{query}%22&sortKeys=usage&{?maximumRecords}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand Down
24 changes: 24 additions & 0 deletions lib/qa/authorities/linked_data/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ def prefixes
@prefixes ||= authority_config.fetch(:prefixes, {})
end

def config_version
@config_version ||= authority_config.fetch(:QA_CONFIG_VERSION, '1.0')
end

def config_version?(version)
config_version == version
end

# Return the full configuration for an authority
# @return [String] the authority configuration
def authority_config
@authority_config ||= Qa::LinkedData::AuthorityService.authority_config(@authority_name)
raise Qa::InvalidLinkedDataAuthority, "Unable to initialize linked data authority '#{@authority_name}'" if @authority_config.nil?
convert_1_0_to_2_0 if @authority_config.fetch(:QA_CONFIG_VERSION, '1.0') == '1.0'
@authority_config
end

Expand All @@ -63,6 +72,21 @@ def self.predicate_uri(config, key)
pred_uri = RDF::URI(pred) unless pred.nil? || pred.length <= 0
pred_uri
end

private

def convert_1_0_to_2_0
convert_1_0_url_to_2_0_url(:search)
convert_1_0_url_to_2_0_url(:term)
end

# @deprecated Update to linked data config version 2.0 instead
def convert_1_0_url_to_2_0_url(action_key)
url_template = @authority_config.fetch(action_key, {}).fetch(:url, {}).fetch(:template, "")
return if url_template.blank?
warn "[DEPRECATED] #Linked data configuration #{authority_name} has 1.0 version format which is deprecated; update to version 2.0 configuration."
@authority_config[action_key][:url][:template] = url_template.gsub("{?", "{")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"QA_CONFIG_VERSION": "2.0",
"term": {
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
Expand Down Expand Up @@ -48,7 +49,7 @@
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/search?query={?query}&{?encode_true}&{?encode_false}&{?encode_not_specified}",
"template": "http://localhost/test_default/search?{?query}&{?encode_true}&{?encode_false}&{?encode_not_specified}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand Down
5 changes: 3 additions & 2 deletions spec/fixtures/authorities/linked_data/lod_full_config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"QA_CONFIG_VERSION": "2.0",
"prefixes": {
"schema": "http://www.w3.org/2000/01/rdf-schema#",
"skos": "http://www.w3.org/2004/02/skos/core#"
Expand All @@ -7,7 +8,7 @@
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/term/{?subauth}/{?term_id}?param1={?param1}&param2={?param2}",
"template": "http://localhost/test_default/term/{subauth}/{term_id}?{?param1}&{?param2}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand Down Expand Up @@ -63,7 +64,7 @@
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/search?subauth={?subauth}&query={?query}&param1={?param1}&param2={?param2}",
"template": "http://localhost/test_default/search?{?subauth}&{?query}&{?param1}&{?param2}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand Down
164 changes: 164 additions & 0 deletions spec/fixtures/authorities/linked_data/lod_full_config_1_0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
{
"prefixes": {
"schema": "http://www.w3.org/2000/01/rdf-schema#",
"skos": "http://www.w3.org/2004/02/skos/core#"
},
"term": {
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/term/{?subauth}/{?term_id}?param1={?param1}&param2={?param2}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
"@type": "IriTemplateMapping",
"variable": "term_id",
"property": "hydra:freetextQuery",
"required": true
},
{
"@type": "IriTemplateMapping",
"variable": "subauth",
"property": "hydra:freetextQuery",
"required": false,
"default": "term_sub2_name"
},
{
"@type": "IriTemplateMapping",
"variable": "param1",
"property": "hydra:freetextQuery",
"required": false,
"default": "alpha"
},
{
"@type": "IriTemplateMapping",
"variable": "param2",
"property": "hydra:freetextQuery",
"required": false,
"default": "beta"
}
]
},
"qa_replacement_patterns": {
"term_id": "term_id",
"subauth": "subauth"
},
"term_id": "ID",
"language": [ "en" ],
"results": {
"id_predicate": "http://purl.org/dc/terms/identifier",
"label_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel",
"altlabel_predicate": "http://www.w3.org/2004/02/skos/core#altLabel",
"broader_predicate": "http://www.w3.org/2004/02/skos/core#broader",
"narrower_predicate": "http://www.w3.org/2004/02/skos/core#narrower",
"sameas_predicate": "http://www.w3.org/2004/02/skos/core#exactMatch"
},
"subauthorities": {
"term_sub1_key": "term_sub1_name",
"term_sub2_key": "term_sub2_name",
"term_sub3_key": "term_sub3_name"
}
},
"search": {
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/search?subauth={?subauth}&query={?query}&param1={?param1}&param2={?param2}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
"@type": "IriTemplateMapping",
"variable": "query",
"property": "hydra:freetextQuery",
"required": true
},
{
"@type": "IriTemplateMapping",
"variable": "subauth",
"property": "hydra:freetextQuery",
"required": false,
"default": "search_sub1_name"
},
{
"@type": "IriTemplateMapping",
"variable": "param1",
"property": "hydra:freetextQuery",
"required": false,
"default": "delta"
},
{
"@type": "IriTemplateMapping",
"variable": "param2",
"property": "hydra:freetextQuery",
"required": false,
"default": "echo"
}
]
},
"qa_replacement_patterns": {
"query": "query",
"subauth": "subauth"
},
"language": [ "en", "fr", "de" ],
"results": {
"id_predicate": "http://purl.org/dc/terms/identifier",
"label_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel",
"altlabel_predicate": "http://www.w3.org/2004/02/skos/core#altLabel",
"sort_predicate": "http://www.w3.org/2004/02/skos/core#prefLabel"
},
"context": {
"groups": {
"dates": {
"group_label_i18n": "qa.linked_data.authority.locnames_ld4l_cache.dates",
"group_label_default": "Dates"
},
"hierarchy": {
"group_label_i18n": "qa.linked_data.authority.locgenres_ld4l_cache.hierarchy",
"group_label_default": "Hierarchy"
}
},
"properties": [
{
"property_label_i18n": "qa.linked_data.authority.locgenres_ld4l_cache.authoritative_label",
"property_label_default": "Authoritative Label",
"ldpath": "madsrdf:authoritativeLabel",
"selectable": true,
"drillable": false
},
{
"group_id": "dates",
"property_label_i18n": "qa.linked_data.authority.locnames_ld4l_cache.birth_date",
"property_label_default": "Birth",
"ldpath": "madsrdf:identifiesRWO/madsrdf:birthDate/schema:label",
"selectable": false,
"drillable": false
},
{
"group_id": "hierarchy",
"property_label_i18n": "qa.linked_data.authority.locgenres_ld4l_cache.narrower",
"property_label_default": "Narrower",
"ldpath": "skos:narrower :: xsd:string",
"selectable": true,
"drillable": true,
"expansion_label_ldpath": "skos:prefLabel ::xsd:string",
"expansion_id_ldpath": "loc:lccn ::xsd:string"
},
{
"group_id": "hierarchy",
"property_label_i18n": "qa.linked_data.authority.locgenres_ld4l_cache.broader",
"property_label_default": "Broader",
"ldpath": "skos:broader :: xsd:string",
"selectable": true,
"drillable": true,
"expansion_label_ldpath": "skos:prefLabel ::xsd:string",
"expansion_id_ldpath": "loc:lccn ::xsd:string"
}
]
},
"subauthorities": {
"search_sub1_key": "search_sub1_name",
"search_sub2_key": "search_sub2_name",
"search_sub3_key": "search_sub3_name"
}
}
}
5 changes: 3 additions & 2 deletions spec/fixtures/authorities/linked_data/lod_lang_defaults.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"QA_CONFIG_VERSION": "2.0",
"term": {
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/term/{?term_id}",
"template": "http://localhost/test_default/term/{term_id}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand All @@ -29,7 +30,7 @@
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/search?query={?query}",
"template": "http://localhost/test_default/search?{?query}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"QA_CONFIG_VERSION": "2.0",
"term": {
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/term/{?term_id}",
"template": "http://localhost/test_default/term/{term_id}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand All @@ -29,7 +30,7 @@
"url": {
"@context": "http://www.w3.org/ns/hydra/context.jsonld",
"@type": "IriTemplate",
"template": "http://localhost/test_default/search?query={?query}",
"template": "http://localhost/test_default/search?{?query}",
"variableRepresentation": "BasicRepresentation",
"mapping": [
{
Expand Down
Loading

0 comments on commit 9652d9b

Please sign in to comment.