module Strelka::App::Negotiation

HTTP Content negotiation for Strelka applications.

The application can test the request for which types are accepted, set different response blocks for different acceptable content types, provides tranformations for entity bodies and set transformations for new content types.

class UserService < Strelka::App

    plugins :routing, :negotiation

    add_content_type :tnetstring, 'text/x-tnetstring', TNetstring.method( :dump )

end # class UserService

Public Class Methods

included( object ) click to toggle source

Extension callback – extend the HTTPRequest and HTTPResponse classes with Negotiation support when this plugin is loaded.

Calls superclass method
# File lib/strelka/app/negotiation.rb, line 55
def self::included( object )
        self.log.debug "Extending Request and Response with Negotiation mixins"
        Strelka::HTTPRequest.class_eval { include Strelka::HTTPRequest::Negotiation }
        Strelka::HTTPResponse.class_eval { include Strelka::HTTPResponse::Negotiation }
        super
end

Public Instance Methods

fixup_response( response ) click to toggle source

Check to be sure the response is acceptable after the request is handled.

Calls superclass method
# File lib/strelka/app/negotiation.rb, line 75
def fixup_response( response )
        response = super

        # Ensure the response is acceptable; if it isn't respond with the appropriate
        # status.
        unless response.acceptable?
                body = self.make_not_acceptable_body( response )
                finish_with( HTTP::NOT_ACCEPTABLE, body ) # throw
        end

        return response
end
handle_request( request ) click to toggle source

Start content-negotiation when the response has returned.

Calls superclass method
# File lib/strelka/app/negotiation.rb, line 64
def handle_request( request )
        self.log.debug "[:negotiation] Wrapping response with HTTP content negotiation."

        response = super
        response.negotiate

        return response
end
make_not_acceptable_body( response ) click to toggle source

Create an HTTP entity body describing the variants of the given response.

# File lib/strelka/app/negotiation.rb, line 90
def make_not_acceptable_body( response )
        # :TODO: Unless it was a HEAD request, the response SHOULD include
        # an entity containing a list of available entity characteristics and
        # location(s) from which the user or user agent can choose the one
        # most appropriate. The entity format is specified by the media type
        # given in the Content-Type header field. Depending upon the format
        # and the capabilities of the user agent, selection of the most
        # appropriate choice MAY be performed automatically. However, this
        # specification does not define any standard for such automatic
        # selection. [RFC2616]
        return "No way to respond given the requested acceptance criteria."
end