From 6c50e05b7c3f7914112614eca176e4bd316177c2 Mon Sep 17 00:00:00 2001 From: Tom Johnson Date: Sun, 12 Feb 2017 15:28:38 -0800 Subject: [PATCH] Add lookup_endpoint support for HydraTemplate This is a shameless green version of lookup endpoint support. This goes a long way toward a naive fix for #48. --- lib/linked_data_fragments/hydra_template.rb | 25 +++++++++- .../hydra_template_spec.rb | 49 +++++++++++++++++-- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/lib/linked_data_fragments/hydra_template.rb b/lib/linked_data_fragments/hydra_template.rb index 1437bc8..6ab90f0 100644 --- a/lib/linked_data_fragments/hydra_template.rb +++ b/lib/linked_data_fragments/hydra_template.rb @@ -1,6 +1,9 @@ module LinkedDataFragments class HydraTemplate - CONTROL_REGEX = /{\??(.*?)}/.freeze + CONTROL_REGEX = /{\??(.*?)}/.freeze + PATH_REGEX = /{([^\?]*?)}/.freeze + QUERY_REGEX = /{\?(.*?)}/.freeze + TEMPLATE_REGEX = %r((http://[^{}]*\/)(#{CONTROL_REGEX}\/?)+).freeze ## # @!attribute [r] template @@ -22,6 +25,26 @@ def controls .map(&:strip) # Strip whitespace end + ## + # Gives a list of valid `void:uriLookupEndpoint`s derived from the template. + # + # @todo Don't hard code control mappings + # + # @return [Array] a list of lookup endpoints based on the template + # @see https://www.w3.org/TR/void/#lookup + def lookup_endpoints + if controls.include?('subject') + case template + when QUERY_REGEX + [template.match(TEMPLATE_REGEX)[1] + '?subject='] + when PATH_REGEX + controls.first == 'subject' ? [template.match(TEMPLATE_REGEX)[1]] : [] + end + else + [] + end + end + ## # @return [String] the template def to_s diff --git a/spec/linked_data_fragments/hydra_template_spec.rb b/spec/linked_data_fragments/hydra_template_spec.rb index 45e08e9..5b512f8 100644 --- a/spec/linked_data_fragments/hydra_template_spec.rb +++ b/spec/linked_data_fragments/hydra_template_spec.rb @@ -6,14 +6,15 @@ describe '#controls' do it 'should return the mapped controls' do - expect(subject.controls).to eq ['subject'] + expect(subject.controls).to contain_exactly('subject') end context 'when given multiple controls' do let(:template) { 'http://localhost:4000/{?subject,predicate, object}' } it 'should return all of them' do - expect(subject.controls).to eq ['subject', 'predicate', 'object'] + expect(subject.controls) + .to contain_exactly('subject', 'predicate', 'object') end end @@ -29,11 +30,53 @@ let(:template) { 'http://localhost:4000/{subject}/{predicate}' } it 'should return them' do - expect(subject.controls).to eq ['subject', 'predicate'] + expect(subject.controls).to contain_exactly('subject', 'predicate') end end end + describe '#lookup_endpoints' do + context 'with subject as an available control' do + it 'gives the template uri' do + expect(subject.lookup_endpoints) + .to contain_exactly 'http://localhost:4000/?subject=' + end + + it 'varies to the URI geven' do + template = 'http://example.com/{?subject}' + subject = described_class.new(template) + + expect(subject.lookup_endpoints) + .to contain_exactly 'http://example.com/?subject=' + end + end + + context 'with slashes to separate controls' do + let(:template) { 'http://localhost:4000/blah/{subject}/{predicate}' } + + it 'gives the template uri' do + expect(subject.lookup_endpoints) + .to contain_exactly 'http://localhost:4000/blah/' + end + end + + context 'with non-appendable subject control' do + let(:template) { 'http://localhost:4000/blah/{predicate}/{subject}' } + + it 'is empty' do + expect(subject.lookup_endpoints).to be_empty + end + end + + context 'with no subject controls' do + let(:template) { 'http://localhost:4000/{?predicate,object}' } + + it 'is empty' do + expect(subject.lookup_endpoints).to be_empty + end + end + end + describe '#to_s' do it 'should be the template' do expect(subject.to_s).to eql template