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
18 19 20 21 22 23 |
# File 'lib/flame/dispatcher.rb', line 18 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
53 54 55 |
# File 'lib/flame/dispatcher.rb', line 53 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.new(request., response) end |
#default_body ⇒ Object
Generate default body of error page
141 142 143 144 |
# File 'lib/flame/dispatcher.rb', line 141 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)
131 132 133 134 135 136 137 138 |
# File 'lib/flame/dispatcher.rb', line 131 def dump_error(error) = [ "#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - " \ "#{error.class} - #{error.message}:", *error.backtrace ].join("\n\t") @env['rack.errors'].puts() 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)
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/flame/dispatcher.rb', line 117 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.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
96 97 98 99 100 101 102 103 |
# File 'lib/flame/dispatcher.rb', line 96 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 params = Rack::Utils.build_nested_query args.delete(:params) path = route.assign_arguments(args) path = '/' if path.empty? path << ("?#{params}" if params).to_s end |
#run! ⇒ Object
Start of execution the request
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/flame/dispatcher.rb', line 26 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
42 43 44 45 46 |
# File 'lib/flame/dispatcher.rb', line 42 def status(value = nil) response.status ||= 200 response.headers['X-Cascade'] = 'pass' if value == 404 value ? response.status = value : response.status end |