Class: Flame::Dispatcher
- Inherits:
-
Object
- Object
- Flame::Dispatcher
- Defined in:
- lib/flame/dispatcher.rb
Overview
Helpers for dispatch Flame::Application#call
Defined Under Namespace
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
- #config ⇒ Object
- #cookies ⇒ Object
- #halt(new_status, body = nil, new_headers = {}) ⇒ Object
-
#initialize(app, env) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #params ⇒ Object
- #path_to(ctrl, action = :index, args = {}) ⇒ Object
- #run! ⇒ Object
- #session ⇒ Object
- #status(value = nil) ⇒ Object
Constructor Details
#initialize(app, env) ⇒ Dispatcher
Returns a new instance of Dispatcher.
10 11 12 13 14 |
# File 'lib/flame/dispatcher.rb', line 10 def initialize(app, env) @app = app @request = Flame::Request.new(env) @response = Rack::Response.new end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
6 7 8 |
# File 'lib/flame/dispatcher.rb', line 6 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
6 7 8 |
# File 'lib/flame/dispatcher.rb', line 6 def response @response end |
Instance Method Details
#config ⇒ Object
45 46 47 |
# File 'lib/flame/dispatcher.rb', line 45 def config @app.config end |
#cookies ⇒ Object
41 42 43 |
# File 'lib/flame/dispatcher.rb', line 41 def @cookies ||= Cookies.new(request., response) end |
#halt(new_status, body = nil, new_headers = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/flame/dispatcher.rb', line 56 def halt(new_status, body = nil, new_headers = {}) new_status.is_a?(String) ? (body = new_status) : (status new_status) response.headers.merge!(new_headers) # p response.body if body.nil? && !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) body = Rack::Utils::HTTP_STATUS_CODES[status] end throw :halt, body end |
#params ⇒ Object
33 34 35 |
# File 'lib/flame/dispatcher.rb', line 33 def params @params ||= request.params.merge(request.params.keys_to_sym) end |
#path_to(ctrl, action = :index, args = {}) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/flame/dispatcher.rb', line 49 def path_to(ctrl, action = :index, args = {}) route = @app.class.router.find_route(controller: ctrl, action: action) fail RouteNotFoundError.new(ctrl, action) unless route path = route.assign_arguments(args) path.empty? ? '/' : path end |
#run! ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/flame/dispatcher.rb', line 16 def run! body = catch :halt do try_route || try_static || try_static(File.join(__dir__, '..', '..', 'public')) || halt(404) end # p body response.write body response.finish end |
#session ⇒ Object
37 38 39 |
# File 'lib/flame/dispatcher.rb', line 37 def session request.session end |
#status(value = nil) ⇒ Object
28 29 30 31 |
# File 'lib/flame/dispatcher.rb', line 28 def status(value = nil) response.status ||= 200 value ? response.status = value : response.status end |