Class: Flame::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Static
Defined in:
lib/flame/dispatcher.rb,
lib/flame/static.rb

Overview

Helpers for dispatch Flame::Application#call

Defined Under Namespace

Modules: Static

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Static

#return_static, #static_cached?, #try_static

Constructor Details

#initialize(app, env) ⇒ Dispatcher

Returns a new instance of Dispatcher.



17
18
19
20
21
22
# File 'lib/flame/dispatcher.rb', line 17

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) ⇒ Object



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

def body(value = nil)
	value ? @body = value : @body
end

#config ⇒ Object



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

def config
	@app.config
end

#cookies ⇒ Object



53
54
55
# File 'lib/flame/dispatcher.rb', line 53

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

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



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/flame/dispatcher.rb', line 68

def halt(new_status = nil, new_body = nil, new_headers = {})
	case new_status
	when String then new_body = new_status
	when Integer then status new_status
	end
	# new_status.is_a?(String) ? () : (status new_status)
	new_body = default_body if new_body.nil? && body.empty?
	body new_body if new_body
	response.headers.merge!(new_headers)
	throw :halt
end

#params ⇒ Object



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

def params
	@params ||= request.params.merge(request.params.keys_to_sym)
end

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



61
62
63
64
65
66
# File 'lib/flame/dispatcher.rb', line 61

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



24
25
26
27
28
29
30
31
32
33
# File 'lib/flame/dispatcher.rb', line 24

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

#session ⇒ Object



49
50
51
# File 'lib/flame/dispatcher.rb', line 49

def session
	request.session
end

#status(value = nil) ⇒ Object



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

def status(value = nil)
	response.status ||= 200
	response.headers['X-Cascade'] = 'pass' if value == 404
	value ? response.status = value : response.status
end