lib/rack/ldp.rb in rdf-ldp-0.3.0 vs lib/rack/ldp.rb in rdf-ldp-0.4.0

- old
+ new

@@ -1,37 +1,38 @@ require 'rack' begin require 'linkeddata' -rescue LoadError => e +rescue LoadError require 'rdf/turtle' require 'json/ld' end require 'rack/linkeddata' require 'rdf/ldp' module Rack ## - # Provides Rack middleware for handling Linked Data Platform requirements + # Provides Rack middleware for handling Linked Data Platform requirements # when passed {RDF::LDP::Resource} and its subclasses as response objects. # # Response objects that are not an {RDF::LDP::Resource} are passed over # without alteration, allowing server implementers to mix LDP interaction # patterns with others on the same server. # # The suite can be mix-and-matched as needed. This allows easy swap in of # custom handlers for parts of the behavior. It is recommended that you use - # {Rack::LDP::ContentNegotiation}, {Rack::LDP::Errors}, and - # {Rack::LDP::Responses} as the outer three services. With these in place, - # you can handle requests as needed in your application, giving responses - # conforming to the core {RDF::LDP::Resource} interface. + # {Rack::LDP::ContentNegotiation}, {Rack::LDP::Errors}, {Rack::LDP::Responses} + # and {Rack::LDP::Reousets} as the outer middleware layers. With these in + # place, you can handle requests as needed in your application, giving + # responses conforming to the core {RDF::LDP::Resource} interface. # # @example # run Rack:;Builder.new do # use Rack::LDP::ContentNegotiation # use Rack::LDP::Errors # use Rack::LDP::Responses + # use Rack::LDP::Requests # # ... # end # # @see http://www.w3.org/TR/ldp/ the LDP specification module LDP @@ -108,13 +109,32 @@ end end ## # Specializes {Rack::LinkedData::ContentNegotiation}, making the default - # return type 'text/turtle' + # return type 'text/turtle'. + # + # @see Rack::LinkedData::ContentNegotiation}, making class ContentNegotiation < Rack::LinkedData::ContentNegotiation + DEFAULT_PREFIXES = + Hash[*RDF::Vocabulary.map { |v| [v.__prefix__, v.to_uri] }.flatten] + .freeze + def initialize(app, options = {}) options[:default] ||= 'text/turtle' + options[:prefixes] ||= DEFAULT_PREFIXES.dup + super + end + + ## + # The default LinkedData Conneg doesn't support wildcard operators. We + # patch in support for 'text/*' manually, giving Turtle. This should be + # considered helpful by LDP clients. + # + # @see Rack::LinkedData::ContentNegotiation#find_writer_for_content_type + def find_writer_for_content_type(content_type) + return [RDF::Writer.for(:ttl), 'text/turtle'] if + content_type == 'text/*' super end end end end