Class: Utopia::Application

Inherits:
Protocol::HTTP::Middleware
  • Object
show all
Defined in:
lib/utopia/application.rb

Overview

The protocol-facing entrypoint for a Utopia application.

This object accepts Protocol::HTTP::Request instances, dispatches to the Utopia application stack, and normalizes the result back to a Protocol::HTTP::Response.

Constant Summary collapse

PATH =
"config/application.rb".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(default_app = Response::NotFound, &block) ⇒ Object

Build a Utopia application stack using the protocol HTTP middleware builder.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/utopia/application.rb', line 25

def self.build(default_app = Response::NotFound, &block)
	builder = Protocol::HTTP::Middleware::Builder.new(default_app)
	
	if block
		if block.arity.zero?
			builder.instance_exec(&block)
		else
			block.call(builder)
		end
	end
	
	return self.new(builder.to_app)
end

.defaultObject

Build the default Utopia application.



41
42
43
# File 'lib/utopia/application.rb', line 41

def self.default
	self.build
end

.load(path = PATH, **options) ⇒ Object

Load a Utopia application from a conventional configuration file.

If the file defines an Application constant, it will be returned directly. If the constant is a class, it will be instantiated. If the file does not exist, or does not define Application, the default application is returned.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/utopia/application.rb', line 55

def self.load(path = PATH, **options)
	if File.exist?(path)
		top = Module.new
		top.class_eval(File.read(path), path)
		
		if top.const_defined?(:Application, false)
			application = top.const_get(:Application)
			
			if application.is_a?(Class)
				return application.new(**options)
			else
				return application
			end
		end
	end
	
	return self.default
end

Instance Method Details

#call(request) ⇒ Object

Process a protocol HTTP request.



77
78
79
80
81
# File 'lib/utopia/application.rb', line 77

def call(request)
	request = Request.new(request)
	
	return Response.wrap(super(request))
end