Class: Flame::Dispatcher

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

Overview

Helpers for dispatch Flame::Application#call

Defined Under Namespace

Modules: Static Classes: Cookies, Request, Response

Constant Summary collapse

GEM_STATIC_FILES =
File.join __dir__, '..', '..', 'public'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Static

#find_static

Constructor Details

#initialize(app, env) ⇒ Dispatcher

Initialize Dispatcher from Application#call

Parameters:



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

def initialize(app, env)
	@app = app
	@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.



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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#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



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

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

#cached_tiltsObject

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



146
147
148
# File 'lib/flame/dispatcher.rb', line 146

def cached_tilts
	@app.class.cached_tilts
end

#configObject

Application-config object as Hash



82
83
84
# File 'lib/flame/dispatcher.rb', line 82

def config
	@app.config
end

#cookiesObject

Cookies object as Hash



77
78
79
# File 'lib/flame/dispatcher.rb', line 77

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

#default_bodyObject

Generate default body of error page



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

def default_body
	# response.headers[Rack::CONTENT_TYPE] = 'text/html'
	"<h1>#{Rack::Utils::HTTP_STATUS_CODES[status]}</h1>"
end

#dump_error(error) ⇒ Object

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

Parameters:

  • error (Exception)

    exception for class, message and backtrace



130
131
132
133
134
135
136
137
# File 'lib/flame/dispatcher.rb', line 130

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



121
122
123
124
125
126
# File 'lib/flame/dispatcher.rb', line 121

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



67
68
69
# File 'lib/flame/dispatcher.rb', line 67

def params
	@params ||= request.params.symbolize_keys(deep: true)
end

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

Build a path to the given controller and action, with any expected params

Examples:

Path for ‘show(id)` method of `ArticlesController` with `id: 2`

path_to ArticlesController, :show, id: 2 # => "/articles/show/2"

Path for ‘new` method of `ArticlesController` with params

path_to ArticlesController, :new, params: { author_id: 1 }
# => "/articles/new?author_id=1"

Parameters:

  • ctrl (Flame::Controller)

    class of controller

  • action (Symbol) (defaults to: :index)

    method of controller

  • args (Hash) (defaults to: {})

    parameters for method of controller

Returns:

  • (String)

    path for requested method, controller and parameters

Raises:



97
98
99
100
101
102
103
104
105
# File 'lib/flame/dispatcher.rb', line 97

def path_to(ctrl, action = :index, args = {})
	route = @app.class.router.find_route(controller: ctrl, action: action)
	raise Errors::RouteNotFoundError.new(ctrl, action) unless route
	query = Rack::Utils.build_nested_query args.delete(:params)
	query = nil if query&.empty?
	path = route.path.assign_arguments(args)
	path = '/' if path.empty?
	URI::Generic.build(path: path, query: query).to_s
end

#run!Object

Start of execution the request



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

def run!
	catch :halt do
		try_static ||
			try_static(dir: GEM_STATIC_FILES) ||
			try_route ||
			halt(404)
	end
	response.write body unless request.http_method == :HEAD
	response.finish
end

#sessionObject

Session object as Hash



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

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



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

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