def response_media_type(options={})
      options = {:halt_if_unacceptable => false}.merge(options)
      env = options[:env] || (respond_to?(:env) ? self.env : raise(ArgumentError, "must pass env"))
      supported_media_types = options[:supported_media_types] || self.supported_media_types
      accept = env['HTTP_ACCEPT']
      if accept =~ /\S/
        begin
          best_media_type = Rack::Accept::Request.new(env).best_media_type(supported_media_types)
        rescue RuntimeError => e
          # TODO: this is a crappy way to recognize this exception 
          raise unless e.message =~ /Invalid header value/
        end
        if best_media_type
          best_media_type
        else
          if options[:halt_if_unacceptable]
            logger.error "received Accept header of #{accept.inspect}; halting with 406"
            message = I18n.t('app.errors.request.accept',
              :default => "The request indicated that no supported media type is acceptable. Supported media types are: %{supported_media_types}. The request specified Accept: %{accept}",
              :accept => accept,
              :supported_media_types => supported_media_types.join(', ')
            )
            halt_error(406, {'Accept' => [message]})
          else
            supported_media_types.first
          end
        end
      elsif supported_media_types && supported_media_types.any?
        supported_media_types.first
      else
        raise "No media types are defined. Please set supported_media_types."
      end
    end
def response_media_type(options={})
      options = {:halt_if_unacceptable => false}.merge(options)
      env = options[:env] || (respond_to?(:env) ? self.env : raise(ArgumentError, "must pass env"))
      supported_media_types = options[:supported_media_types] || self.supported_media_types
      accept = env['HTTP_ACCEPT']
      if accept =~ /\S/
        begin
          best_media_type = Rack::Accept::Request.new(env).best_media_type(supported_media_types)
        rescue RuntimeError => e
          # TODO: this is a crappy way to recognize this exception 
          raise unless e.message =~ /Invalid header value/
        end
        if best_media_type
          best_media_type
        else
          if options[:halt_if_unacceptable]
            logger.error "received Accept header of #{accept.inspect}; halting with 406"
            message = I18n.t('app.errors.request.accept',
              :default => "The request indicated that no supported media type is acceptable. Supported media types are: %{supported_media_types}. The request specified Accept: %{accept}",
              :accept => accept,
              :supported_media_types => supported_media_types.join(', ')
            )
            halt_error(406, {'Accept' => [message]})
          else
            supported_media_types.first
          end
        end
      elsif supported_media_types && supported_media_types.any?
        supported_media_types.first
      else
        raise "No media types are defined. Please set supported_media_types."
      end
    end
finds the best match (highest q) for those supported_media_types indicated as acceptable by the Accept header.

 If the Accept header is not present, assumes that any supported media type is acceptable, and returns the first
 one.

 if the :halt_if_unacceptable option is true and no supported media type is acceptable, this halts with 406.

 if the :halt_if_unacceptable option is false (or omitted) and no supported media type is acceptable, this
 returns the first supported media type.