Class: Flame::Dispatcher
- Inherits:
-
Object
- Object
- Flame::Dispatcher
- Includes:
- Static
- Defined in:
- lib/flame/dispatcher.rb,
lib/flame/dispatcher/static.rb,
lib/flame/dispatcher/cookies.rb,
lib/flame/dispatcher/request.rb,
lib/flame/dispatcher/response.rb
Overview
Helpers for dispatch Flame::Application#call
Defined Under Namespace
Modules: Static Classes: Cookies, Request, Response
Constant Summary collapse
- GEM_STATIC_FILES =
File.join __dir__, '..', '..', 'public'
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
-
#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.
-
#cookies ⇒ Object
Cookies 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, env) ⇒ Dispatcher
constructor
Initialize Dispatcher from Application#call.
-
#params ⇒ Object
Parameters of the request.
-
#path_to(ctrl, action = :index, args = {}) ⇒ String
Build a path to the given controller and action, with any expected params.
-
#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, env) ⇒ Dispatcher
Initialize Dispatcher from Application#call
25 26 27 28 29 30 |
# File 'lib/flame/dispatcher.rb', line 25 def initialize(app, env) @app = app @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.
18 19 20 |
# File 'lib/flame/dispatcher.rb', line 18 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
18 19 20 |
# File 'lib/flame/dispatcher.rb', line 18 def response @response end |
Instance Method Details
#body(value = nil) ⇒ String
Acccess to the body of response
60 61 62 |
# File 'lib/flame/dispatcher.rb', line 60 def body(value = nil) value ? @body = value : @body ||= '' end |
#cached_tilts ⇒ Object
All cached tilts (views) for application by Flame::Render
146 147 148 |
# File 'lib/flame/dispatcher.rb', line 146 def cached_tilts @app.class.cached_tilts end |
#config ⇒ Object
Application-config object as Hash
82 83 84 |
# File 'lib/flame/dispatcher.rb', line 82 def config @app.config end |
#cookies ⇒ Object
Cookies object as Hash
77 78 79 |
# File 'lib/flame/dispatcher.rb', line 77 def @cookies ||= Cookies.new(request., response) end |
#default_body ⇒ Object
Generate default body of error page
140 141 142 143 |
# File 'lib/flame/dispatcher.rb', line 140 def default_body # response.headers[Rack::CONTENT_TYPE] = 'text/html' "<h1>#{Rack::Utils::HTTP_STATUS_CODES[status]}</h1>" end |
#dump_error(error) ⇒ Object
Add error’s backtrace to @env (terminal or file)
130 131 132 133 134 135 136 137 |
# File 'lib/flame/dispatcher.rb', line 130 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)
121 122 123 124 125 126 |
# File 'lib/flame/dispatcher.rb', line 121 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
67 68 69 |
# File 'lib/flame/dispatcher.rb', line 67 def params @params ||= request.params.symbolize_keys(deep: true) end |
#path_to(ctrl, action = :index, args = {}) ⇒ String
Build a path to the given controller and action, with any expected params
97 98 99 100 101 102 103 104 105 |
# File 'lib/flame/dispatcher.rb', line 97 def path_to(ctrl, action = :index, args = {}) route = @app.class.router.find_route(controller: ctrl, action: action) raise Errors::RouteNotFoundError.new(ctrl, action) unless route query = Rack::Utils.build_nested_query args.delete(:params) query = nil if query&.empty? path = route.path.assign_arguments(args) path = '/' if path.empty? URI::Generic.build(path: path, query: query).to_s end |
#run! ⇒ Object
Start of execution the request
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/flame/dispatcher.rb', line 33 def run! catch :halt do try_static || try_static(dir: GEM_STATIC_FILES) || try_route || halt(404) end response.write body unless request.http_method == :HEAD response.finish end |
#session ⇒ Object
Session object as Hash
72 73 74 |
# File 'lib/flame/dispatcher.rb', line 72 def session request.session end |
#status(value = nil) ⇒ Integer
Acccess to the status of response
49 50 51 52 53 |
# File 'lib/flame/dispatcher.rb', line 49 def status(value = nil) response.status ||= 200 response.headers['X-Cascade'] = 'pass' if value == 404 value ? response.status = value : response.status end |