@@ -207,6 +207,19 @@ def self.reader_types
207207 reader_symbols . flat_map { |s | RDF ::Format . for ( s ) . content_type } . uniq
208208 end
209209
210+ ##
211+ # Returns the set of content types with quality for available RDF::Reader subclasses.
212+ #
213+ # @example
214+ #
215+ # accept_types = RDF::Format.accept_types
216+ # # => %w(text/html;q=0.5 text/turtle ...)
217+ #
218+ # @return [Array<String>]
219+ def self . accept_types
220+ reader_symbols . flat_map { |s | RDF ::Format . for ( s ) . accept_type } . uniq
221+ end
222+
210223 ##
211224 # Returns the set of format symbols for available RDF::Writer subclasses.
212225 #
@@ -400,6 +413,11 @@ class << self
400413 # extensions, that should be mapped to the given MIME type and handled
401414 # by this class.
402415 #
416+ # Optionally, both `type`, `alias`, and `aliases`, may be parameterized
417+ # for expressing quality.
418+ #
419+ # content_type "text/html;q=0.4"
420+ #
403421 # @param [String] type
404422 # @param [Hash{Symbol => Object}] options
405423 # @option options [String] :alias (nil)
@@ -420,10 +438,14 @@ def self.content_type(type = nil, options = {})
420438 [ @@content_type [ self ] , @@content_types . map {
421439 |ct , cl | ( cl . include? ( self ) && ct != @@content_type [ self ] ) ? ct : nil } ] . flatten . compact
422440 else
441+ accept_type , type = type , type . split ( ';' ) . first
423442 @@content_type [ self ] = type
424443 @@content_types [ type ] ||= [ ]
425444 @@content_types [ type ] << self unless @@content_types [ type ] . include? ( self )
426445
446+ @@accept_types [ accept_type ] ||= [ ]
447+ @@accept_types [ accept_type ] << self unless @@accept_types [ accept_type ] . include? ( self )
448+
427449 if extensions = ( options [ :extension ] || options [ :extensions ] )
428450 extensions = Array ( extensions ) . map ( &:to_sym )
429451 extensions . each do |ext |
@@ -433,13 +455,26 @@ def self.content_type(type = nil, options = {})
433455 end
434456 if aliases = ( options [ :alias ] || options [ :aliases ] )
435457 aliases = Array ( aliases ) . each do |a |
436- @@content_types [ a ] ||= [ ]
437- @@content_types [ a ] << self unless @@content_types [ a ] . include? ( self )
458+ aa = a . split ( ';' ) . first
459+ @@accept_types [ a ] ||= [ ]
460+ @@accept_types [ a ] << self unless @@accept_types [ a ] . include? ( self )
461+
462+ @@content_types [ aa ] ||= [ ]
463+ @@content_types [ aa ] << self unless @@content_types [ aa ] . include? ( self )
438464 end
439465 end
440466 end
441467 end
442468
469+ ##
470+ # Returns an array of values appropriate for an Accept header.
471+ # Same as `self.content_type`, if no parameter is given when defined.
472+ #
473+ # @return [Array<String>]
474+ def self . accept_type
475+ @@accept_types . map { |t , formats | t if formats . include? ( self ) } . compact
476+ end
477+
443478 ##
444479 # Retrieves or defines file extensions for this RDF serialization format.
445480 #
@@ -488,6 +523,7 @@ def self.content_encoding(encoding = nil)
488523 @@content_type = { } # @private
489524 @@content_types = { } # @private
490525 @@content_encoding = { } # @private
526+ @@accept_types = { } # @private
491527 @@readers = { } # @private
492528 @@writers = { } # @private
493529 @@subclasses = [ ] # @private
0 commit comments