Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ group :development, :test do
gem 'coveralls', require: false
gem 'simplecov', require: false
end

gem 'lru_redux'
19 changes: 9 additions & 10 deletions bin/ldpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'optparse'
require 'open-uri'
require 'ldpath'
require 'lru_redux'

begin
require 'rest-client'
Expand All @@ -13,11 +14,9 @@ options = {}
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: ldpath [options] URI"

opts.on("--program=STRING_URI_OR_FILE", "LDPath program to run (reads from STDIN by default)") do |program|
opts.on("--program=STRING_URI_OR_FILE", "LDPath program to run") do |program|
options[:program] = if File.exist?(program) || program =~ /^http/
open(program).read
elsif program.strip == "-"
$stdin.read
else
program
end
Expand All @@ -26,13 +25,13 @@ end

opt_parser.parse!

uri = ARGV.shift
uris = ARGV
uris = ARGF if uris.empty? || uris.first == '-'

if uri.nil?
$stderr.puts opt_parser
raise OptionParser::MissingArgument, "URI" unless uri
end

options[:program] ||= $stdin.read
cache = LruRedux::TTL::Cache.new(1000, 120)
program = Ldpath::Program.parse(options[:program])

puts Ldpath::Program.parse(options[:program]).evaluate(RDF::URI.new(uri)).to_json
uris.each do |uri|
puts program.evaluate(RDF::URI.new(uri.strip), cache: cache).to_json
end
28 changes: 18 additions & 10 deletions lib/ldpath/loaders/linked_data_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@ def initialize(endpoint)
end

def load(uri)
Ldpath.logger.debug "Loading LDF data for #{uri.inspect}"
i = 0
begin
Ldpath.logger.debug "Loading LDF data for #{uri.inspect}"

graph = RDF::Graph.new
request_uri = RDF::URI("#{@endpoint}?subject=#{CGI::escape(uri)}")
graph = RDF::Graph.new
request_uri = RDF::URI("#{@endpoint}?subject=#{CGI::escape(uri)}")

while request_uri
Ldpath.logger.debug " -- querying #{request_uri}"
request_graph = RDF::Graph.load(request_uri)
graph.insert_statements(request_graph)
request_uri = request_graph.first_object([request_uri, NEXT_PAGE, nil])
end
while request_uri
Ldpath.logger.debug " -- querying #{request_uri}"
request_graph = RDF::Graph.load(request_uri)
graph.insert_statements(request_graph)
request_uri = request_graph.first_object([request_uri, NEXT_PAGE, nil])
end

graph
graph
rescue => e
i += 1
retry if i < 3
Ldpath.logger.warn e
RDF::Graph.new
end
end
end
4 changes: 2 additions & 2 deletions lib/ldpath/program.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def initialize(mappings, default_loader: Ldpath::Loaders::Direct.new, prefixes:

end

def evaluate(uri, context: nil, limit_to_context: false)
result = Ldpath::Result.new(self, uri, context: context, limit_to_context: limit_to_context)
def evaluate(uri, context: nil, limit_to_context: false, cache: RDF::Util::Cache.new)
result = Ldpath::Result.new(self, uri, context: context, limit_to_context: limit_to_context, cache: cache)
unless filters.empty?
return {} unless filters.all? { |f| f.evaluate(result, uri, result.context) }
end
Expand Down
13 changes: 13 additions & 0 deletions spec/ldpath_program_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,19 @@
result = subject.evaluate RDF::URI.new("http://www.bbc.co.uk/programmes/b0081dq5")
expect(result["title"]).to match_array "Huw Stephens"
end

context 'error handling' do
before do
stub_request(:get, 'http://example.com/ldf?subject=http://www.bbc.co.uk/programmes/b0081dq5')
.to_return(status: 500, body: nil)
end

it "logs an error but continues processing" do
expect(Ldpath.logger).to receive(:warn)
result = subject.evaluate RDF::URI.new("http://www.bbc.co.uk/programmes/b0081dq5")
expect(result["title"]).to match_array []
end
end
end
end

Expand Down