Class: Utopia::Controller::Responder

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

Defined Under Namespace

Classes: Handler, Responds

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponder

Returns a new instance of Responder.



58
59
60
# File 'lib/utopia/controller/responder.rb', line 58

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

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



62
63
64
# File 'lib/utopia/controller/responder.rb', line 62

def handlers
  @handlers
end

Instance Method Details

#call(context, request, *arguments, **options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/utopia/controller/responder.rb', line 70

def call(context, request, *arguments, **options)
	# Parse the list of browser preferred content types and return ordered by priority:
	media_types = HTTP::Accept::MediaTypes.browser_preferred_media_types(request.env)
	
	handler, media_range = @handlers.for(media_types)
	
	if handler
		handler.call(context, request, media_range, *arguments, **options)
	end
end

#freezeObject



64
65
66
67
68
# File 'lib/utopia/controller/responder.rb', line 64

def freeze
	@handlers.freeze
	
	super
end

#handle(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.



82
83
84
# File 'lib/utopia/controller/responder.rb', line 82

def handle(content_type, &block)
	@handlers << Handler.new(content_type, block)
end

#respond_to(context, request) ⇒ Object



86
87
88
# File 'lib/utopia/controller/responder.rb', line 86

def respond_to(context, request)
	Responds.new(self, context, request)
end

#with(content_type, &block) ⇒ Object



98
99
100
# File 'lib/utopia/controller/responder.rb', line 98

def with(content_type, &block)
	handle(content_type, &block)
end

#with_jsonObject



90
91
92
# File 'lib/utopia/controller/responder.rb', line 90

def with_json
	@handlers << Handlers::JSON
end

#with_passthroughObject



94
95
96
# File 'lib/utopia/controller/responder.rb', line 94

def with_passthrough
	@handlers << Handlers::Passthrough
end