Class: Flame::Dispatcher

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

Overview

Helpers for dispatch Flame::Application#call

Defined Under Namespace

Classes: Cookies

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Dispatcher

Returns a new instance of Dispatcher.



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

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.



4
5
6
# File 'lib/flame/dispatcher.rb', line 4

def request
  @request
end

#response ⇒ Object (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/flame/dispatcher.rb', line 4

def response
  @response
end

Instance Method Details

#config ⇒ Object



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

def config
	@app.config
end

#cookies ⇒ Object



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

def cookies
	@cookies ||= Cookies.new(request.cookies, response)
end

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



52
53
54
55
56
57
58
59
60
61
# File 'lib/flame/dispatcher.rb', line 52

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



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

def params
	request.params
end

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



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

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



12
13
14
15
16
17
18
19
20
21
22
# 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
	# p body
	response.write body
	response.finish
end

#session ⇒ Object



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

def session
	request.session
end

#status(value = nil) ⇒ Object



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

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