Class: Utopia::Controller::Respond::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/controller/respond.rb

Constant Summary collapse

HTTP_ACCEPT =
'HTTP_ACCEPT'.freeze
NOT_ACCEPTABLE_RESPONSE =
[406, {}, []].freeze

Instance Method Summary collapse

Constructor Details

#initializeResponder

Returns a new instance of Responder.



116
117
118
# File 'lib/utopia/controller/respond.rb', line 116

def initialize
	@converters = HTTP::Accept::MediaTypes::Map.new
end

Instance Method Details

#call(context, request, path, response) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/utopia/controller/respond.rb', line 140

def call(context, request, path, response)
	# Parse the list of browser preferred content types and return ordered by priority:
	media_types = HTTP::Accept::MediaTypes.browser_preferred_media_types(request.env)
	
	converter, media_range = @converters.for(media_types)
	
	if converter
		converter.call(context, response, media_range)
	else
		NOT_ACCEPTABLE_RESPONSE
	end
end

#freezeObject



120
121
122
123
124
# File 'lib/utopia/controller/respond.rb', line 120

def freeze
	@converters.freeze
	
	super
end

#with(content_type, &block) ⇒ Object

Add a converter for the specified content type. Call the block with the response content if the request accepts the specified content_type.



127
128
129
# File 'lib/utopia/controller/respond.rb', line 127

def with(content_type, &block)
	@converters << Converter::Callback.new(content_type, block)
end

#with_jsonObject

Add a converter for JSON when requests accept 'application/json'



136
137
138
# File 'lib/utopia/controller/respond.rb', line 136

def with_json
	@converters << Converter::ToJSON
end

#with_passthroughObject



131
132
133
# File 'lib/utopia/controller/respond.rb', line 131

def with_passthrough
	@converters << Passthrough
end