Class: Flame::Dispatcher
- Inherits:
-
Object
- Object
- Flame::Dispatcher
- Includes:
- Static
- Defined in:
- lib/flame/dispatcher.rb,
lib/flame/static.rb,
lib/flame/cookies.rb
Overview
Helpers for dispatch Flame::Application#call
Defined Under Namespace
Modules: Static Classes: Cookies
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.
-
#config ⇒ Object
Application-config object as Hash.
-
#content_type(ext = nil) ⇒ Object
Access to Content-Type header of response.
-
#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_or_body = 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
#return_static, #static_cached?, #try_static
Constructor Details
#initialize(app, env) ⇒ Dispatcher
Initialize Dispatcher from Application#call
20 21 22 23 24 25 |
# File 'lib/flame/dispatcher.rb', line 20 def initialize(app, env) @app = app @env = env @request = Flame::Request.new(env) @response = Flame::Response.new end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
11 12 13 |
# File 'lib/flame/dispatcher.rb', line 11 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
11 12 13 |
# File 'lib/flame/dispatcher.rb', line 11 def response @response end |
Instance Method Details
#body(value = nil) ⇒ String
Acccess to the body of response
55 56 57 |
# File 'lib/flame/dispatcher.rb', line 55 def body(value = nil) value ? @body = value : @body ||= '' end |
#config ⇒ Object
Application-config object as Hash
75 76 77 |
# File 'lib/flame/dispatcher.rb', line 75 def config @app.config end |
#content_type(ext = nil) ⇒ Object
Access to Content-Type header of response
80 81 82 83 |
# File 'lib/flame/dispatcher.rb', line 80 def content_type(ext = nil) return response[Rack::CONTENT_TYPE] unless ext response[Rack::CONTENT_TYPE] = Rack::Mime.mime_type(ext) end |
#cookies ⇒ Object
Cookies object as Hash
70 71 72 |
# File 'lib/flame/dispatcher.rb', line 70 def @cookies ||= Cookies.new(request., response) end |
#default_body ⇒ Object
Generate default body of error page
136 137 138 139 |
# File 'lib/flame/dispatcher.rb', line 136 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)
126 127 128 129 130 131 132 133 |
# File 'lib/flame/dispatcher.rb', line 126 def dump_error(error) @error_message = [ "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - " \ "#{error.class} - #{error.}:", *error.backtrace ].join("\n\t") @env['rack.errors'].puts(@error_message) end |
#halt(new_status_or_body = nil, new_body = nil, new_headers = {}) ⇒ Object
Interrupt the execution of route, and set new optional data
(otherwise using existing)
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/flame/dispatcher.rb', line 112 def halt(new_status_or_body = nil, new_body = nil, new_headers = {}) case new_status_or_body when String then new_body = new_status_or_body when Integer then status new_status_or_body end # new_status.is_a?(String) ? () : (status new_status) body new_body if new_body default_body_of_nearest_route if body.empty? response.headers.merge!(new_headers) throw :halt end |
#params ⇒ Object
Parameters of the request
60 61 62 |
# File 'lib/flame/dispatcher.rb', line 60 def params @params ||= request.params.merge(request.params.keys_to_sym(deep: true)) end |
#path_to(ctrl, action = :index, args = {}) ⇒ String
Build a path to the given controller and action, with any expected params
93 94 95 96 97 98 |
# File 'lib/flame/dispatcher.rb', line 93 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 path = route.assign_arguments(args) path.empty? ? '/' : path end |
#run! ⇒ Object
Start of execution the request
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/flame/dispatcher.rb', line 28 def run! catch :halt do try_route || try_static || try_static(File.join(__dir__, '..', '..', 'public')) || halt(404) end response.write body response.finish end |
#session ⇒ Object
Session object as Hash
65 66 67 |
# File 'lib/flame/dispatcher.rb', line 65 def session request.session end |
#status(value = nil) ⇒ Integer
Acccess to the status of response
44 45 46 47 48 |
# File 'lib/flame/dispatcher.rb', line 44 def status(value = nil) response.status ||= 200 response.headers['X-Cascade'] = 'pass' if value == 404 value ? response.status = value : response.status end |