Class: Flame::Dispatcher

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Routes, Static, Memery
Defined in:
lib/flame/dispatcher.rb,
lib/flame/dispatcher/routes.rb,
lib/flame/dispatcher/static.rb,
lib/flame/dispatcher/request.rb,
lib/flame/dispatcher/response.rb

Overview

Helpers for dispatch Flame::Application#call

Defined Under Namespace

Modules: Routes, Static Classes: Request, Response

Constant Summary collapse

GEM_STATIC_FILES =
File.join(__dir__, '../../public').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Static

#find_static

Constructor Details

#initialize(app_class, env) ⇒ Dispatcher

Initialize Dispatcher from Application#call

Parameters:

  • app_class (Class)

    application class

  • env

    Rack-environment object



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

def initialize(app_class, env)
	@app_class = app_class
	@env = env
	@request = Flame::Dispatcher::Request.new(env)
	@response = Flame::Dispatcher::Response.new
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#available_endpointObject

Available routes endpoint



98
99
100
# File 'lib/flame/dispatcher.rb', line 98

memoize def available_endpoint
	router.navigate(*request.path.parts)
end

#body(value = nil) ⇒ String

Acccess to the body of response

Examples:

Set body value

body 'Hello World!'

Parameters:

  • value (String, nil) (defaults to: nil)

    string value for new body

Returns:

  • (String)

    current body



71
72
73
# File 'lib/flame/dispatcher.rb', line 71

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

#cached_tiltsObject

All cached tilts (views) for application by Flame::Render



141
142
143
# File 'lib/flame/dispatcher.rb', line 141

def cached_tilts
	@app_class.cached_tilts
end

#configObject

Application-config object as Hash



93
94
95
# File 'lib/flame/dispatcher.rb', line 93

def config
	@app_class.config
end

#default_bodyObject

Generate default body of error page



135
136
137
138
# File 'lib/flame/dispatcher.rb', line 135

def default_body
	# response.headers[Rack::CONTENT_TYPE] = 'text/html'
	Rack::Utils::HTTP_STATUS_CODES[status]
end

#dump_error(error) ⇒ Object

Add error’s backtrace to @env (terminal or file)

Parameters:

  • error (Exception)

    exception for class, message and backtrace



125
126
127
128
129
130
131
132
# File 'lib/flame/dispatcher.rb', line 125

def dump_error(error)
	error_message = [
		"#{Time.now.strftime('%Y-%m-%d %H:%M:%S')} - " \
			"#{error.class} - #{error.message}:",
		*error.backtrace
	].join("\n\t")
	@env[Rack::RACK_ERRORS].puts(error_message)
end

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

Interrupt the execution of route, and set new optional data

(otherwise using existing)

Examples:

Halt, no change status or body

halt

Halt with 500, no change body

halt 500

Halt with 404, render template

halt 404, render('errors/404')

Halt with 200, set new headers

halt 200, 'Cats!', 'Content-Type' # => 'animal/cat'

Parameters:

  • new_status (Integer, nil) (defaults to: nil)

    set new HTTP status code

  • new_body (String, nil) (defaults to: nil)

    set new body

  • new_headers (Hash, nil) (defaults to: {})

    merge new headers



116
117
118
119
120
121
# File 'lib/flame/dispatcher.rb', line 116

def halt(new_status = nil, new_body = nil, new_headers = {})
	status new_status if new_status
	body new_body || (default_body_of_nearest_route if body.empty?)
	response.headers.merge!(new_headers)
	throw :halt
end

#paramsObject

Parameters of the request



78
79
80
81
82
83
84
# File 'lib/flame/dispatcher.rb', line 78

def params
	request.params.symbolize_keys(deep: true)
rescue ArgumentError => e
	raise unless e.message.include?('invalid %-encoding')

	{}
end

#run!Object

Start of execution the request



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

def run!
	catch :halt do
		validate_request

		try_options ||
			try_static ||
			try_static(dir: GEM_STATIC_FILES) ||
			try_route ||
			halt(404)
	end
	response.write body unless request.head?
	response.finish
end

#sessionObject

Session object as Hash



88
89
90
# File 'lib/flame/dispatcher.rb', line 88

def session
	request.session
end

#status(value = nil) ⇒ Integer

Acccess to the status of response

Examples:

Set status value

status 200

Parameters:

  • value (Ineger, nil) (defaults to: nil)

    integer value for new status

Returns:

  • (Integer)

    current status



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

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