Class: Flame::Dispatcher
- Inherits:
-
Object
- Object
- Flame::Dispatcher
- Extended by:
- Forwardable
- Defined in:
- lib/flame/dispatcher.rb,
lib/flame/dispatcher/routes.rb,
lib/flame/dispatcher/static.rb,
lib/flame/dispatcher/request.rb,
lib/flame/dispatcher/response.rb
Overview
Helpers for dispatch Flame::Application#call
Defined Under Namespace
Modules: Routes, Static Classes: Request, Response
Constant Summary collapse
- GEM_STATIC_FILES =
File.join(__dir__, '../../public').freeze
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#available_endpoint ⇒ Object
Available routes endpoint.
-
#body(value = nil) ⇒ String
Acccess to the body of response.
-
#cached_tilts ⇒ Object
All cached tilts (views) for application by Flame::Render.
-
#config ⇒ Object
Application-config object as Hash.
-
#default_body ⇒ Object
Generate default body of error page.
-
#dump_error(error) ⇒ Object
Add error’s backtrace to @env (terminal or file).
-
#halt(new_status = nil, new_body = nil, new_headers = {}) ⇒ Object
Interrupt the execution of route, and set new optional data (otherwise using existing).
-
#initialize(app_class, env) ⇒ Dispatcher
constructor
Initialize Dispatcher from Application#call.
-
#params ⇒ Object
Parameters of the request.
-
#run! ⇒ Object
Start of execution the request.
-
#session ⇒ Object
Session object as Hash.
-
#status(value = nil) ⇒ Integer
Acccess to the status of response.
Methods included from Static
Constructor Details
#initialize(app_class, env) ⇒ Dispatcher
Initialize Dispatcher from Application#call
33 34 35 36 37 38 |
# File 'lib/flame/dispatcher.rb', line 33 def initialize(app_class, env) @app_class = app_class @env = env @request = Flame::Dispatcher::Request.new(env) @response = Flame::Dispatcher::Response.new end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
26 27 28 |
# File 'lib/flame/dispatcher.rb', line 26 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
26 27 28 |
# File 'lib/flame/dispatcher.rb', line 26 def response @response end |
Instance Method Details
#available_endpoint ⇒ Object
Available routes endpoint
98 99 100 |
# File 'lib/flame/dispatcher.rb', line 98 memoize def available_endpoint router.navigate(*request.path.parts) end |
#body(value = nil) ⇒ String
Acccess to the body of response
71 72 73 |
# File 'lib/flame/dispatcher.rb', line 71 def body(value = nil) value ? @body = value : @body ||= '' end |
#cached_tilts ⇒ Object
All cached tilts (views) for application by Flame::Render
141 142 143 |
# File 'lib/flame/dispatcher.rb', line 141 def cached_tilts @app_class.cached_tilts end |
#config ⇒ Object
Application-config object as Hash
93 94 95 |
# File 'lib/flame/dispatcher.rb', line 93 def config @app_class.config end |
#default_body ⇒ Object
Generate default body of error page
135 136 137 138 |
# File 'lib/flame/dispatcher.rb', line 135 def default_body # response.headers[Rack::CONTENT_TYPE] = 'text/html' Rack::Utils::HTTP_STATUS_CODES[status] end |
#dump_error(error) ⇒ Object
Add error’s backtrace to @env (terminal or file)
125 126 127 128 129 130 131 132 |
# File 'lib/flame/dispatcher.rb', line 125 def dump_error(error) = [ "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - " \ "#{error.class} - #{error.}:", *error.backtrace ].join("\n\t") @env[Rack::RACK_ERRORS].puts() end |
#halt(new_status = nil, new_body = nil, new_headers = {}) ⇒ Object
Interrupt the execution of route, and set new optional data
(otherwise using existing)
116 117 118 119 120 121 |
# File 'lib/flame/dispatcher.rb', line 116 def halt(new_status = nil, new_body = nil, new_headers = {}) status new_status if new_status body new_body || (default_body_of_nearest_route if body.empty?) response.headers.merge!(new_headers) throw :halt end |
#params ⇒ Object
Parameters of the request
78 79 80 81 82 83 84 |
# File 'lib/flame/dispatcher.rb', line 78 def params request.params.symbolize_keys(deep: true) rescue ArgumentError => e raise unless e..include?('invalid %-encoding') {} end |
#run! ⇒ Object
Start of execution the request
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/flame/dispatcher.rb', line 41 def run! catch :halt do validate_request || try_static || try_static(dir: GEM_STATIC_FILES) || try_route || halt(404) end response.write body unless request.head? response.finish end |
#session ⇒ Object
Session object as Hash
88 89 90 |
# File 'lib/flame/dispatcher.rb', line 88 def session request.session end |
#status(value = nil) ⇒ Integer
Acccess to the status of response
60 61 62 63 64 |
# File 'lib/flame/dispatcher.rb', line 60 def status(value = nil) response.status ||= 200 response.headers['X-Cascade'] = 'pass' if value == 404 value ? response.status = value : response.status end |