Class: Flame::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/dispatcher.rb

Overview

Class initialize when Application.call(env) invoked For new request and response

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Dispatcher

Returns a new instance of Dispatcher.



7
8
9
10
# File 'lib/flame/dispatcher.rb', line 7

def initialize(app, env)
	@app = app
	@env = env
end

Instance Method Details

#config ⇒ Object

Helpers



24
25
26
# File 'lib/flame/dispatcher.rb', line 24

def config
	@app.config
end

#halt(new_status, body = '', new_headers = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/flame/dispatcher.rb', line 45

def halt(new_status, body = '', new_headers = {})
	status new_status
	response.headers.merge!(new_headers)
	# p response.body
	if body.empty? &&
	   !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status)
		body = Rack::Utils::HTTP_STATUS_CODES[status]
	end
	throw :halt, body
end

#params ⇒ Object



32
33
34
# File 'lib/flame/dispatcher.rb', line 32

def params
	request.params
end

#path_to(ctrl, action, args = {}) ⇒ Object



56
57
58
59
60
# File 'lib/flame/dispatcher.rb', line 56

def path_to(ctrl, action, args = {})
	route = @app.router.find_route(controller: ctrl, action: action)
	fail RouteNotFoundError.new(ctrl, action) unless route
	route.assign_arguments(args)
end

#request ⇒ Object



28
29
30
# File 'lib/flame/dispatcher.rb', line 28

def request
	@request ||= Rack::Request.new(@env)
end

#response ⇒ Object



36
37
38
# File 'lib/flame/dispatcher.rb', line 36

def response
	@response ||= Rack::Response.new
end

#run! ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/flame/dispatcher.rb', line 12

def run!
	body = catch :halt do
		try_route ||
		try_static ||
		try_static(File.join(__dir__, '..', '..', 'public')) ||
		halt(404)
	end
	response.write body
	response.finish
end

#status(value = nil) ⇒ Object



40
41
42
43
# File 'lib/flame/dispatcher.rb', line 40

def status(value = nil)
	response.status ||= 200
	value ? response.status = value : response.status
end