Class: Flame::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Static
Defined in:
lib/flame/dispatcher.rb,
lib/flame/static.rb,
lib/flame/cookies.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:



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

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

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



11
12
13
# File 'lib/flame/dispatcher.rb', line 11

def request
  @request
end

#responseObject (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



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

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

#configObject

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

#cookiesObject

Cookies object as Hash



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

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

#default_bodyObject

Generate default body of error page



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

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



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

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



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/flame/dispatcher.rb', line 117

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

#paramsObject

Parameters of the request



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

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:



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

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
  params = Rack::Utils.build_nested_query args.delete(:params)
  path = route.assign_arguments(args)
  path = '/' if path.empty?
  path << ("?#{params}" if params).to_s
end

#run!Object

Start of execution the request



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

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

#sessionObject

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



42
43
44
45
46
# File 'lib/flame/dispatcher.rb', line 42

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