Skip to content

Commit 9048ba1

Browse files
committed
In Reader.open, only use format to find a concrete reader, not other options. This allows the content-type and actual file content to be used to select a reader, in preference to filename and extensions.
Fixes #353.
1 parent 4dd98a4 commit 9048ba1

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

lib/rdf/reader.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ class << self
180180
# @yieldreturn [void] ignored
181181
# @raise [RDF::FormatError] if no reader found for the specified format
182182
def self.open(filename, format: nil, **options, &block)
183-
# If we're the abstract reader, and we can figure out a concrete reader from format and options, use that.
184-
if self == RDF::Reader && reader = self.for(format || {file_name: filename}.merge(options))
183+
# If we're the abstract reader, and we can figure out a concrete reader from format, use that.
184+
if self == RDF::Reader && format && reader = self.for(format)
185185
return reader.open(filename, format: format, **options, &block)
186186
end
187187

@@ -383,7 +383,7 @@ def each_statement(&block)
383383
if block_given?
384384
begin
385385
loop { block.call(read_statement) }
386-
rescue EOFError => e
386+
rescue EOFError
387387
rewind rescue nil
388388
end
389389
end
@@ -417,7 +417,7 @@ def each_triple(&block)
417417
if block_given?
418418
begin
419419
loop { block.call(*read_triple) }
420-
rescue EOFError => e
420+
rescue EOFError
421421
rewind rescue nil
422422
end
423423
end

spec/reader_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,20 @@
5656
reader_mock.got_here
5757
end
5858
end
59+
60+
it "uses content type in preference to file extension" do
61+
uri = "http://example/foo.nq"
62+
accept = (RDF::Format.accept_types + %w(*/*;q=0.1)).join(", ")
63+
reader_mock = double("reader")
64+
expect(reader_mock).to receive(:got_here)
65+
WebMock.
66+
stub_request(:get, uri).
67+
to_return(body: "foo", status: 200, headers: { 'Content-Type' => 'text/turtle'})
68+
69+
described_class.open(uri) do |r|
70+
expect(r).to be_a(RDF::Turtle::Reader)
71+
reader_mock.got_here
72+
end
73+
end
5974
end
6075
end

0 commit comments

Comments
 (0)