Skip to content

Commit

Permalink
Merge 2ee5f95 into f214462
Browse files Browse the repository at this point in the history
  • Loading branch information
elrayle committed Feb 11, 2019
2 parents f214462 + 2ee5f95 commit 4c2372b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
17 changes: 17 additions & 0 deletions app/models/qa/linked_data/config/context_property_map.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Defines the external authority predicates used to extract additional context from the graph.
require 'ldpath'

module Qa
module LinkedData
module Config
Expand Down Expand Up @@ -48,6 +50,13 @@ def drillable?
@drillable
end

def values(graph, subject_uri)
output = ldpath_program.evaluate subject_uri, graph
output.present? ? output['property'].uniq : nil
rescue
'PARSE ERROR'
end

def group?
group_id.present?
end
Expand All @@ -60,6 +69,14 @@ def extract_label
return I18n.t(i18n_key, default: default) if i18n_key.present?
default
end

def ldpath_program
return @program if @program.present?
program_code = ""
prefixes.each { |key, url| program_code << "@prefix #{key} : <#{url}> \;\n" }
program_code << "property = #{ldpath} \;"
@program = Ldpath::Program.parse program_code
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/qa/linked_data/mapper/graph_mapper_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def self.map_values(graph:, predicate_map:, subject_uri:)
value_map = {}
predicate_map.each do |key, predicate|
values = predicate == :subject_uri ? [subject_uri] : graph_service.object_values(graph: graph, subject: subject_uri, predicate: predicate)
values = yield values if block_given?
value_map[key] = values
end
value_map = yield value_map if block_given?
value_map
end
end
Expand Down
1 change: 1 addition & 0 deletions qa.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency 'activerecord-import'
s.add_dependency 'deprecation'
s.add_dependency 'faraday'
s.add_dependency 'ldpath'
s.add_dependency 'nokogiri', '~> 1.6'
s.add_dependency 'rails', '~> 5.0'
s.add_dependency 'rdf'
Expand Down
15 changes: 15 additions & 0 deletions spec/models/linked_data/config/context_property_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,19 @@
end
end
end

describe '#values' do
let(:program) { instance_double(Ldpath::Program) }
let(:coordinates) { '42.4488° N, 76.4763° W' }
let(:subject_uri) { instance_double(RDF::URI) }
let(:graph) { instance_double(RDF::Graph) }

before do
allow(Ldpath::Program).to receive(:parse).with(anything).and_return(program)
allow(program).to receive(:evaluate).with(anything, anything).and_return('property' => [coordinates, coordinates, coordinates]) # check that uniq is applied
end
it 'returns the ldpath program for this property map' do
expect(subject.values(graph, subject_uri)).to match_array coordinates
end
end
end
29 changes: 29 additions & 0 deletions spec/services/linked_data/mapper/graph_mapper_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@
validate_entry(subject, :sort, ['3'], RDF::Literal)
end
end

context 'when block is passed in' do
let(:subject_uri) { RDF::URI.new('http://id.worldcat.org/fast/5140') }
let(:context) do
{ location: '42.4488° N, 76.4763° W' }
end
let(:subject) do
described_class.map_values(graph: graph, predicate_map: predicate_map, subject_uri: subject_uri) do |value_map|
value_map[:context] = context
value_map
end
end

it 'yields to passed in block' do
expect(subject.count).to eq 7
expect(subject).to be_kind_of Hash
expect(subject.keys).to match_array [:uri, :id, :label, :altlabel, :sameas, :sort, :context]

validate_entry(subject, :uri, [subject_uri.to_s], RDF::URI)
validate_entry(subject, :id, ['5140'], RDF::Literal)
validate_entry(subject, :label, ['Cornell, Joseph'], RDF::Literal)
validate_entry(subject, :altlabel, [], NilClass)
validate_entry(subject, :sameas, [], NilClass)
validate_entry(subject, :sort, ['3'], RDF::Literal)

expect(subject[:context]).to be_kind_of Hash
expect(subject[:context]).to include(context)
end
end
end

def validate_entry(results, key, values, entry_kind)
Expand Down

0 comments on commit 4c2372b

Please sign in to comment.