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.



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

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#configObject



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

def config
	@app.config
end

#cookiesObject



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

def cookies
	@cookies ||= Cookies.new(request.cookies, 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

#paramsObject



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

#sessionObject



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