Class: Utopia::Controller::Base
- Inherits:
-
Object
- Object
- Utopia::Controller::Base
- Defined in:
- lib/utopia/controller/base.rb
Overview
The base implementation of a controller class.
Constant Summary collapse
- URI_PATH =
nil- BASE_PATH =
nil- CONTROLLER =
nil
Class Method Summary collapse
-
.base_path ⇒ Object
A string which is the full path to the directory which contains the controller.
-
.controller ⇒ Object
The controller middleware itself.
-
.direct?(path) ⇒ Boolean
Check whether the path refers directly to this controller.
-
.freeze ⇒ Object
Freeze this object and its internal state.
-
.inspect ⇒ Object
Generate a debug representation of this object.
-
.to_s ⇒ Object
Convert this object to a string.
-
.uri_path ⇒ Object
A relative path to the controller directory relative to the controller root directory.
Instance Method Summary collapse
-
#call(request) ⇒ Object
Call into the next application.
-
#catch_response ⇒ Object
Catch and return a response thrown while executing the block.
-
#copy_instance_variables(from) ⇒ Object
Copy the instance variables from the previous controller to the next controller (usually only a few).
-
#fail!(error = 400, message = nil) ⇒ Object
Respond with an error which indiciates some kind of failure.
-
#goto!(target, status = 302) ⇒ Object
Controller relative redirect.
-
#ignore! ⇒ Object
This will cause the controller middleware to pass on the request.
-
#inspect ⇒ Object
Generate a debug representation of this object.
-
#parse_body(request, parser: Protocol::Content::Parser.default, &block) ⇒ Object
Parse the request body according to its media type.
-
#process!(request, relative_path) ⇒ Object
Return nil if this controller didn't do anything.
-
#redirect!(target, status = 302) ⇒ Object
Request relative redirect.
-
#respond!(response) ⇒ Object
Immediately respond with a complete protocol response.
-
#respond?(response) ⇒ Boolean
Respond with the response, but only if it's not nil.
-
#succeed!(value = nil, status: 200, headers: {}) ⇒ Object
Succeed the request with a semantic value awaiting response negotiation.
-
#to_s ⇒ Object
Convert this object to a string.
Class Method Details
.base_path ⇒ Object
A string which is the full path to the directory which contains the controller.
22 23 24 |
# File 'lib/utopia/controller/base.rb', line 22 def self.base_path self.const_get(:BASE_PATH) end |
.controller ⇒ Object
The controller middleware itself.
32 33 34 |
# File 'lib/utopia/controller/base.rb', line 32 def self.controller self.const_get(:CONTROLLER) end |
.direct?(path) ⇒ Boolean
Check whether the path refers directly to this controller.
76 77 78 |
# File 'lib/utopia/controller/base.rb', line 76 def self.direct?(path) path.dirname == uri_path end |
.freeze ⇒ Object
Freeze this object and its internal state.
64 65 66 67 68 69 70 71 |
# File 'lib/utopia/controller/base.rb', line 64 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 |
.inspect ⇒ Object
Generate a debug representation of this object.
38 39 40 |
# File 'lib/utopia/controller/base.rb', line 38 def self.inspect "#{super}#{self.uri_path}" end |
.to_s ⇒ Object
Convert this object to a string.
44 45 46 |
# File 'lib/utopia/controller/base.rb', line 44 def self.to_s self.inspect end |
.uri_path ⇒ Object
A relative path to the controller directory relative to the controller root directory.
27 28 29 |
# File 'lib/utopia/controller/base.rb', line 27 def self.uri_path self.const_get(:URI_PATH) end |
Instance Method Details
#call(request) ⇒ Object
Call into the next application.
102 103 104 |
# File 'lib/utopia/controller/base.rb', line 102 def call(request) self.class.controller.delegate.call(request) end |
#catch_response ⇒ Object
Catch and return a response thrown while executing the block.
83 84 85 86 87 |
# File 'lib/utopia/controller/base.rb', line 83 def catch_response catch(:response) do yield and nil end end |
#copy_instance_variables(from) ⇒ Object
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.
95 96 97 98 99 |
# File 'lib/utopia/controller/base.rb', line 95 def copy_instance_variables(from) from.instance_variables.each do |name| self.instance_variable_set(name, from.instance_variable_get(name)) end end |
#fail!(error = 400, message = nil) ⇒ Object
Respond with an error which indiciates some kind of failure.
169 170 171 172 173 174 |
# File 'lib/utopia/controller/base.rb', line 169 def fail!(error = 400, = nil) status = HTTP::Status.new(error, 400...600) ||= status.to_s throw :response, Result.new(status.to_i, {}, ) end |
#goto!(target, status = 302) ⇒ Object
Controller relative redirect.
164 165 166 |
# File 'lib/utopia/controller/base.rb', line 164 def goto!(target, status = 302) redirect! self.class.uri_path + target end |
#ignore! ⇒ Object
This will cause the controller middleware to pass on the request.
146 147 148 |
# File 'lib/utopia/controller/base.rb', line 146 def ignore! throw :response, nil end |
#inspect ⇒ Object
Generate a debug representation of this object.
56 57 58 59 60 |
# File 'lib/utopia/controller/base.rb', line 56 def inspect details = self.instance_variables.map{|name| " #{name}=#{self.instance_variable_get(name)}"} "\#<#{self.class}#{details.join}>" end |
#parse_body(request, parser: Protocol::Content::Parser.default, &block) ⇒ Object
Parse the request body according to its media type.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/utopia/controller/base.rb', line 111 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 |
#process!(request, relative_path) ⇒ Object
Return nil if this controller didn't do anything. Request will keep on processing. Return a valid response if the controller can do so.
90 91 92 |
# File 'lib/utopia/controller/base.rb', line 90 def process!(request, relative_path) return nil end |
#redirect!(target, status = 302) ⇒ Object
Request relative redirect. Respond with a redirect to the given target.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/utopia/controller/base.rb', line 151 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 |
#respond!(response) ⇒ Object
Immediately respond with a complete protocol response.
130 131 132 |
# File 'lib/utopia/controller/base.rb', line 130 def respond!(response) throw :response, Utopia::Response.wrap(response) end |
#respond?(response) ⇒ Boolean
Respond with the response, but only if it's not nil.
137 138 139 140 141 142 143 |
# File 'lib/utopia/controller/base.rb', line 137 def respond?(response) if response return respond!(response) end return nil end |
#succeed!(value = nil, status: 200, headers: {}) ⇒ Object
Succeed the request with a semantic value awaiting response negotiation.
181 182 183 184 185 |
# File 'lib/utopia/controller/base.rb', line 181 def succeed!(value = nil, status: 200, headers: {}) status = HTTP::Status.new(status, 200...300) throw :response, Result.new(status.to_i, headers, value) end |
#to_s ⇒ Object
Convert this object to a string.
50 51 52 |
# File 'lib/utopia/controller/base.rb', line 50 def to_s "\#<#{self.class}>" end |