Class: Flame::Dispatcher

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

Overview

Helpers for dispatch Flame::Application#call

Defined Under Namespace

Modules: Static Classes: Cookies

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Static

#return_static, #static_cached?, #try_static

Constructor Details

#initialize(app, env) ⇒ Dispatcher

Initialize Dispatcher from Application#call

Parameters:



20
21
22
23
24
25
# File 'lib/flame/dispatcher.rb', line 20

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



55
56
57
# File 'lib/flame/dispatcher.rb', line 55

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

#config ⇒ Object

Application-config object as Hash



75
76
77
# File 'lib/flame/dispatcher.rb', line 75

def config
  @app.config
end

#content_type(ext = nil) ⇒ Object

Access to Content-Type header of response



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

def content_type(ext = nil)
  return response[Rack::CONTENT_TYPE] unless ext
  response[Rack::CONTENT_TYPE] = Rack::Mime.mime_type(ext)
end

#cookies ⇒ Object

Cookies object as Hash



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

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

#dump_error(error) ⇒ Object

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

Parameters:

  • error (Exception) —

    exception for class, message and backtrace



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

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.errors'].puts(@error_message)
end

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

Interrupt the execution of route, and set new optional data (otherwise using existing)

Examples:

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_or_body (Integer, String) (defaults to: nil) —

    set new HTTP status code or new body

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

    set new body

  • new_headers (String) (defaults to: {}) —

    merge new headers



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/flame/dispatcher.rb', line 112

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

#params ⇒ Object

Parameters of the request



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

def params
  @params ||= request.params.merge(request.params.keys_to_sym(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"

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:



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

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
  path = route.assign_arguments(args)
  path.empty? ? '/' : path
end

#run! ⇒ Object

Start of execution the request



28
29
30
31
32
33
34
35
36
37
# File 'lib/flame/dispatcher.rb', line 28

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

#session ⇒ Object

Session object as Hash



65
66
67
# File 'lib/flame/dispatcher.rb', line 65

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



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

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