Permalink
Browse files

Fixed Format.reader_symbols/writer_symbols to not load every reader/w…

…riter.

Added Format.reader_types/writer_types to get content types associated with available readers/writers.
Update File.open_file to use Format.reader_types
Include */*;q=0.1 as catch-all in Accept header.
This addresses issue #107
  • Loading branch information...
1 parent ab8b345 commit e0ccf20aff6e50af5787a6c1edefb642a48ebf8d @gkellogg gkellogg committed Mar 20, 2013
Showing with 93 additions and 13 deletions.
  1. +34 −8 lib/rdf/format.rb
  2. +2 −1 lib/rdf/nquads.rb
  3. +3 −3 lib/rdf/util/file.rb
  4. +43 −0 spec/format_spec.rb
  5. +1 −0 spec/nquads_spec.rb
  6. +10 −1 spec/util_file_spec.rb
View
@@ -181,29 +181,55 @@ def self.file_extensions
end
##
- # Returns the set of format symbols for loaded RDF::Reader subclasses.
+ # Returns the set of format symbols for available RDF::Reader subclasses.
#
# @example
#
- # formats = RDF::Format.reader_symbols
- # format = RDF::Format.for(formats.first)
+ # symbols = RDF::Format.reader_symbols
+ # format = RDF::Format.for(symbols.first)
#
# @return [Array<Symbol>]
def self.reader_symbols
- RDF::Format.each.to_a.map(&:reader).compact.map(&:to_sym).uniq
+ @@readers.keys.compact.map(&:to_sym).uniq
end
##
- # Returns the set of format symbols for loaded RDF::Writer subclasses.
+ # Returns the set of content types for available RDF::Reader subclasses.
#
# @example
#
- # formats = RDF::Format.writer_symbols
- # format = RDF::Format.for(formats.first)
+ # content_types = RDF::Format.reader_types
+ # format = RDF::Format.for(:content_type => content_types.first)
+ #
+ # @return [Array<String>]
+ def self.reader_types
+ reader_symbols.map {|s| RDF::Format.for(s).content_type}.flatten.uniq
+ end
+
+ ##
+ # Returns the set of format symbols for available RDF::Writer subclasses.
+ #
+ # @example
+ #
+ # symbols = RDF::Format.writer_symbols
+ # format = RDF::Format.for(symbols.first)
#
# @return [Array<Symbol>]
def self.writer_symbols
- RDF::Format.each.to_a.map(&:writer).compact.map(&:to_sym).uniq
+ @@writers.keys.compact.map(&:to_sym).uniq
+ end
+
+ ##
+ # Returns the set of content types for available RDF::Writer subclasses.
+ #
+ # @example
+ #
+ # content_types = RDF::Format.writer_types
+ # format = RDF::Format.for(:content_type => content_types.first)
+ #
+ # @return [Array<String>]
+ def self.writer_types
+ writer_symbols.map {|s| RDF::Format.for(s).content_type}.flatten.uniq
end
##
View
@@ -14,12 +14,13 @@ module NQuads
# RDF::Format.for("etc/doap.nq")
# RDF::Format.for(:file_name => "etc/doap.nq")
# RDF::Format.for(:file_extension => "nq")
+ # RDF::Format.for(:content_type => "application/n-quads")
# RDF::Format.for(:content_type => "text/x-nquads")
#
# @see http://sw.deri.org/2008/07/n-quads/#mediatype
# @since 0.4.0
class Format < RDF::Format
- content_type 'text/x-nquads', :extension => :nq
+ content_type 'application/n-quads', :extension => :nq, :alias => ['text/x-nquads']
content_encoding 'utf-8'
reader { RDF::NQuads::Reader }
View
@@ -29,17 +29,17 @@ module File
# to override this implementation to provide more control over HTTP
# headers and redirect following.
# @option options [Array, String] :headers
- # HTTP Request headers, passed to Kernel.open.
+ # HTTP Request headers, passed to Kernel.open. (Ruby >= 1.9 only)
# @return [IO] File stream
# @yield [IO] File stream
# @note HTTP headers not passed to `Kernel.open` for Ruby versions < 1.9.
def self.open_file(filename_or_url, options = {}, &block)
filename_or_url = $1 if filename_or_url.to_s.match(/^file:(.*)$/)
- options[:headers] ||= {}
- options[:headers]['Accept'] ||= RDF::Reader.map {|r| r.format.content_type}.uniq.join(", ")
if RUBY_VERSION < "1.9"
Kernel.open(filename_or_url.to_s, &block)
else
+ options[:headers] ||= {}
+ options[:headers]['Accept'] ||= (RDF::Format.reader_types + %w(*/*;q=0.1)).join(", ")
Kernel.open(filename_or_url.to_s, options[:headers], &block)
end
end
View
@@ -49,6 +49,49 @@ def self.detect(sample)
end
end
+ describe ".reader_symbols" do
+ it "returns symbols of available readers" do
+ [:ntriples, :nquads, :fooformat, :barformat].each do |sym|
+ RDF::Format.reader_symbols.should include(sym)
+ end
+ end
+ end
+
+ describe ".reader_types" do
+ it "returns content-types of available readers" do
+ %w(
+ application/n-triples text/plain
+ application/n-quads text/x-nquads
+ application/test
+ ).each do |ct|
+ RDF::Format.reader_types.should include(ct)
+ end
+ end
+ end
+
+ describe ".writer_symbols" do
+ it "returns symbols of available writers" do
+ [:ntriples, :nquads].each do |sym|
+ RDF::Format.writer_symbols.should include(sym)
+ end
+ [:fooformat, :barformat].each do |sym|
+ RDF::Format.writer_symbols.should_not include(sym)
+ end
+ end
+ end
+
+ describe ".writer_types" do
+ it "returns content-types of available writers" do
+ %w(
+ application/n-triples text/plain
+ application/n-quads text/x-nquads
+ ).each do |ct|
+ RDF::Format.writer_types.should include(ct)
+ end
+ RDF::Format.writer_types.should_not include("application/test")
+ end
+ end
+
RDF::Format.each do |format|
context format.name do
subject {format}
View
@@ -19,6 +19,7 @@
'etc/doap.nq',
{:file_name => 'etc/doap.nq'},
{:file_extension => 'nq'},
+ {:content_type => 'application/n-quads'},
{:content_type => 'text/x-nquads'},
].each do |arg|
it "discovers with #{arg.inspect}" do
View
@@ -44,8 +44,17 @@
end
end
+ it "adds Accept header with low-priority */*" do
+ Kernel.should_receive(:open) do |file_name, headers|
+ headers.should be_a(Hash)
+ headers['Accept'].should include('*/*;q=0.1')
+ end.and_yield(StringIO.new)
+ RDF::Util::File.open_file(@uri) do |f|
+ @opened.opened
+ end
+ end
- it "used provided Accept header" do
+ it "used provided Accept header", :ruby => "1.9" do
content_types = RDF::Reader.map {|r| r.format.content_type}.uniq
Kernel.should_receive(:open) do |file_name, headers|
headers.should be_a(Hash)

0 comments on commit e0ccf20

Please sign in to comment.