Strelka::App::

Negotiation

module
Included Modules
Strelka::Constants
Strelka::HTTPRequest::Negotiation
Strelka::HTTPResponse::Negotiation
Extended With
Strelka::Plugin

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

anchor
included( object )

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

# 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

anchor
fixup_response( response )

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

# 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
anchor
handle_request( request )

Start content-negotiation when the response has returned.

# 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
anchor
make_not_acceptable_body( response )

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