Skip to content

Commit

Permalink
Add lookup_endpoint support for HydraTemplate
Browse files Browse the repository at this point in the history
This is a shameless green version of lookup endpoint support. This goes
a long way toward a naive fix for #48.
  • Loading branch information
Tom Johnson committed Feb 19, 2017
1 parent 235a6bf commit 6c50e05
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
25 changes: 24 additions & 1 deletion lib/linked_data_fragments/hydra_template.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<String>] 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
Expand Down
49 changes: 46 additions & 3 deletions spec/linked_data_fragments/hydra_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 6c50e05

Please sign in to comment.