Skip to content

Commit

Permalink
Adds a default block to RDFSource#fetch
Browse files Browse the repository at this point in the history
`#fetch` yields itself to the block if an error is raised while trying
to `#load`. This closes #53.

As part of the rewrite, a more informative error is thrown when trying
to fetch a bnode; closes #108.
  • Loading branch information
Tom Johnson committed Jul 14, 2015
1 parent 1201b3a commit a02e4d0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
25 changes: 22 additions & 3 deletions lib/active_triples/rdf_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,33 @@ def rdf_label
# and available from property accessors if if predicates are
# registered.
#
# @example
# osu = new('http://dbpedia.org/resource/Oregon_State_University')
# osu.fetch
# osu.rdf_label.first
# # => "Oregon State University"
#
# @example with default action block
# my_source = new('http://example.org/dead_url')
# my_source.fetch { |obj| obj.status = 'dead link' }y
#
# @return [ActiveTriples::Entity] self
def fetch
load(rdf_subject)
# @yield gives self to block if this is a node, or an error is raised during
# load
# @yieldparam [ActiveTriples::RDFSource] resource self
#
# @return [ActiveTriples::RDFSource] self
def fetch(&block)
begin
load(rdf_subject)
rescue => e
if block_given?
yield(self)
else
raise "#{self} is a blank node; Cannot fetch a resource without a URI" if
node?
raise e
end
end
self
end

Expand Down
52 changes: 50 additions & 2 deletions spec/active_triples/rdf_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,57 @@
end
end

describe '#id' do
end
describe '#fetch' do
it 'raises an error when it is a node' do
expect { subject.fetch }
.to raise_error "#{subject} is a blank node; Cannot fetch a resource " \
'without a URI'
end

context 'with a valid URI' do
subject { source_class.new(uri) }

context 'with a bad link' do
before { stub_request(:get, uri).to_return(:status => 404) }

it 'raises an error if no block is given' do
expect { subject.fetch }.to raise_error IOError
end

it 'yields self to block' do
expect { |block| subject.fetch(&block) }.to yield_with_args(subject)
end
end

context 'with a working link' do
before do
stub_request(:get, uri).to_return(:status => 200,
:body => graph.dump(:ttl))
end

let(:graph) { RDF::Graph.new << statement }
let(:statement) { RDF::Statement(subject, RDF::DC.title, 'moomin') }

it 'loads retrieved graph into its own' do
expect { subject.fetch }
.to change { subject.statements.to_a }
.from(a_collection_containing_exactly())
.to(a_collection_containing_exactly(statement))
end

it 'merges retrieved graph into its own' do
existing = RDF::Statement(subject, RDF::DC.creator, 'Tove Jansson')
subject << existing

expect { subject.fetch }
.to change { subject.statements.to_a }
.from(a_collection_containing_exactly(existing))
.to(a_collection_containing_exactly(statement, existing))
end
end
end
end

describe '#humanize' do
it 'gives the "" for a node' do
expect(subject.humanize).to eq ''
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
Bundler.setup

require 'rdf/spec'
require 'webmock/rspec'
require 'active_triples'

require 'pry' unless ENV["CI"]

Dir['./spec/support/**/*.rb'].each { |f| require f }

WebMock.disable_net_connect!

RSpec.configure do |config|
config.color = true
config.tty = true
Expand Down

0 comments on commit a02e4d0

Please sign in to comment.