class Base

The base implementation of a controller class.

Definitions

def self.base_path

A string which is the full path to the directory which contains the controller.

Implementation

def self.base_path
	self.const_get(:BASE_PATH)
end

def self.uri_path

A relative path to the controller directory relative to the controller root directory.

Implementation

def self.uri_path
	self.const_get(:URI_PATH)
end

def self.controller

The controller middleware itself.

Implementation

def self.controller
	self.const_get(:CONTROLLER)
end

def self.inspect

Generate a debug representation of this object.

Signature

returns String

The resulting string.

Implementation

def self.inspect
	"#{super}#{self.uri_path}"
end

def inspect

Generate a debug representation of this object.

Signature

returns String

The resulting string.

Implementation

def inspect
	details = self.instance_variables.map{|name| " #{name}=#{self.instance_variable_get(name)}"}
	
	"\#<#{self.class}#{details.join}>"
end

def self.to_s

Convert this object to a string.

Signature

returns String

The resulting string.

Implementation

def self.to_s
	self.inspect
end

def to_s

Convert this object to a string.

Signature

returns String

The resulting string.

Implementation

def to_s
	"\#<#{self.class}>"
end

def self.freeze

Freeze this object and its internal state.

Signature

returns self

This object.

Implementation

def self.freeze
	# This ensures that all class variables are frozen.
	self.instance_variables.each do |name|
		self.instance_variable_get(name).freeze
	end
	
	super
end

def self.direct?(path)

Check whether the path refers directly to this controller.

Signature

parameter path Utopia::Path | String

The path.

returns Boolean

Whether the path is directly contained by this controller's URI path.

Implementation

def self.direct?(path)
	path.dirname == uri_path
end

def catch_response

Catch and return a response thrown while executing the block.

Signature

returns Protocol::HTTP::Response | Nil

The thrown response, or nil if the block completes.

Implementation

def catch_response
	catch(:response) do
		yield and nil
	end
end

def process!(request, relative_path)

Return nil if this controller didn't do anything. Request will keep on processing. Return a valid response if the controller can do so.

Implementation

def process!(request, relative_path)
	return nil
end

def copy_instance_variables(from)

Copy the instance variables from the previous controller to the next controller (usually only a few). This allows controllers to share effectively the same instance variables while still being separate classes/instances.

Implementation

def copy_instance_variables(from)
	from.instance_variables.each do |name|
		self.instance_variable_set(name, from.instance_variable_get(name))
	end
end

def call(request)

Call into the next application.

Implementation

def call(request)
	self.class.controller.delegate.call(request)
end

def parse_body(request, parser: Protocol::Content::Parser.default, &block)

Parse the request body according to its media type.

Signature

parameter request Utopia::Request

The request containing the body.

parameter parser Protocol::Content::Parser

The content parser.

yields {|name, value| ...}

Form entries, including streaming uploads.

returns Object | Nil

The parsed body, or nil when there is no body.

Implementation

def parse_body(request, parser: Protocol::Content::Parser.default, &block)
	body = request.body
	return unless body
	
	input = body.to_io
	error = nil
	
	begin
		return parser.parse(request.headers["content-type"], input, &block)
	rescue => error
		raise
	ensure
		input.close_read(error)
	end
end

def respond!(response)

Immediately respond with a complete protocol response.

Signature

parameter response Protocol::HTTP::Response

The response.

returns Object

This method does not return normally.

Implementation

def respond!(response)
	throw :response, Utopia::Response.wrap(response)
end

def respond?(response)

Respond with the response, but only if it's not nil.

Signature

parameter response Protocol::HTTP::Response | Nil

The optional response.

returns Nil

This method returns nil when no response is provided.

Implementation

def respond?(response)
	if response
		return respond!(response)
	end
	
	return nil
end

def ignore!

This will cause the controller middleware to pass on the request.

Implementation

def ignore!
	throw :response, nil
end

def redirect!(target, status = 302)

Request relative redirect. Respond with a redirect to the given target.

Implementation

def redirect!(target, status = 302)
	status = HTTP::Status.new(status, 300...400)
	
	if target.is_a?(Utopia::Path)
		location = target.to_url_path.encoded
	else
		location = target.to_s
	end
	
	respond! Utopia::Response[status.to_i, {HTTP::LOCATION => location}, [status.to_s]]
end

def goto!(target, status = 302)

Controller relative redirect.

Implementation

def goto!(target, status = 302)
	redirect! self.class.uri_path + target
end

def fail!(error = 400, message = nil)

Respond with an error which indiciates some kind of failure.

Implementation

def fail!(error = 400, message = nil)
	status = HTTP::Status.new(error, 400...600)
	
	message ||= status.to_s
	throw :response, Result.new(status.to_i, {}, message)
end

def succeed!(value = nil, status: 200, headers: {})

Succeed the request with a semantic value awaiting response negotiation.

Signature

parameter value Object

The semantic result value.

parameter status Integer | Symbol

The successful response status.

parameter headers Hash

Additional response headers.

returns Object

This method does not return normally.

Implementation

def succeed!(value = nil, status: 200, headers: {})
	status = HTTP::Status.new(status, 200...300)
	
	throw :response, Result.new(status.to_i, headers, value)
end