lib/rackful/resource.rb
The best media type for the response body, given the current HTTP request.
@param accept [Hash]
@param require_match [Boolean]
@return [String] content-type
@raise [HTTP406NotAcceptable] if `require_match` is `true` and no match was found.
The best media type for the response body, given the current HTTP request.
The best media type for the response body given the current HTTP request .
def best_content_type accept, require_match = true
      if accept.empty?
        return self.all_serializers.values.sort_by(&:last).last[0]::CONTENT_TYPES[0]
      end
      matches = []
      accept.each_pair {
        |accept_media_type, accept_quality|
        self.all_serializers.each_pair {
          |content_type, v|
          quality = v[1]
          media_type = content_type.split(';').first.strip
          if File.fnmatch( accept_media_type, media_type )
            matches << [ content_type, accept_quality * quality ]
          end
        }
      }
      if matches.empty?
        if require_match
          raise( HTTP406NotAcceptable, self.all_serializers.keys() )
        else
          return self.all_serializers.values.sort_by(&:last).last[0]::CONTENT_TYPES[0]
        end
      end
      matches.sort_by(&:last).last[0]
    end
def best_content_type accept , require_match = true if accept . empty? return self . all_serializers . values . sort_by ( & :last ) . last [ 0 ] :: CONTENT_TYPES [ 0 ] end matches = [ ] accept . each_pair { | accept_media_type , accept_quality | self . all_serializers . each_pair { | content_type , v | quality = v [ 1 ] media_type = content_type . split ( ';' ) . first . strip if File . fnmatch ( accept_media_type , media_type ) matches << [ content_type , accept_quality * quality ] end } } if matches . empty? if require_match raise ( HTTP406NotAcceptable , self . all_serializers . keys ( ) ) else return self . all_serializers . values . sort_by ( & :last ) . last [ 0 ] :: CONTENT_TYPES [ 0 ] end end matches . sort_by ( & :last ) . last [ 0 ] end
The best media type for the response body, given the current HTTP request.
The best media type for the response body given the current HTTP request .
None
String
[HTTP406NotAcceptable] if `require_match` is `true` and no match was found.
[ HTTP406NotAcceptable ] if ` require_match ` is ` true ` and no match was found .
accept
<not_specific>
require_match
<not_specific>
<not_specific>
None