Module: Strelka::App::Negotiation

Extended by:
Plugin
Includes:
Constants
Defined in:
lib/strelka/app/negotiation.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary

Attributes included from Plugin

#pluggable, #successors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugin

extended, plugin_name, run_inside, run_outside

Class Method Details

.included(object) ⇒ Object

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



56
57
58
59
60
61
# File 'lib/strelka/app/negotiation.rb', line 56

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

Instance Method Details

#fixup_response(response) ⇒ Object

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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/strelka/app/negotiation.rb', line 76

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) ⇒ Object

Start content-negotiation when the response has returned.



65
66
67
68
69
70
71
72
# File 'lib/strelka/app/negotiation.rb', line 65

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) ⇒ Object

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



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/strelka/app/negotiation.rb', line 91

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