Class: Rackr::Router

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rackr/router.rb,
lib/rackr/router/route.rb,
lib/rackr/router/errors.rb,
lib/rackr/router/build_request.rb,
lib/rackr/router/errors/dev_html.rb

Defined Under Namespace

Modules: Errors Classes: BuildRequest, Route

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}, before: [], after: []) ⇒ Router



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rackr/router.rb', line 17

def initialize(config = {}, before: [], after: [])
  @path_routes_instances = {}
  %w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method|
    @path_routes_instances[method] = { __instances: [] }
  end
  @not_found_instances = {}
  @error_instances = {}

  http_methods = HTTP_METHODS.map { |m| m.downcase.to_sym }
  @routes = Struct.new(*http_methods).new
  http_methods.each do |method|
    @routes.send("#{method}=", Hash.new do |_hash, key|
      raise(Errors::UndefinedNamedRouteError, "Undefined named route: '#{key}'")
    end)
  end
  @dev_mode = ENV['RACK_ENV'] == 'development'
  @config = config
  @scopes = []
  @befores = ensure_array(before)
  @scopes_befores = {}
  @afters = ensure_array(after)
  @scopes_afters = {}
  @default_error = Route.new(proc { |_req, e| raise e })
  @default_not_found = Route.new(proc { [404, {}, ['Not found']] })
  @splitted_request_path_info = []
  @current_request_path_info = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/rackr/router.rb', line 15

def config
  @config
end

#default_not_found=(value) ⇒ Object (writeonly)

Sets the attribute default_not_found



14
15
16
# File 'lib/rackr/router.rb', line 14

def default_not_found=(value)
  @default_not_found = value
end

#error_instancesObject (readonly)

Returns the value of attribute error_instances.



15
16
17
# File 'lib/rackr/router.rb', line 15

def error_instances
  @error_instances
end

#not_found_instancesObject (readonly)

Returns the value of attribute not_found_instances.



15
16
17
# File 'lib/rackr/router.rb', line 15

def not_found_instances
  @not_found_instances
end

#routesObject (readonly)

Returns the value of attribute routes.



15
16
17
# File 'lib/rackr/router.rb', line 15

def routes
  @routes
end

Instance Method Details

#add(method, path, endpoint, as: nil, route_befores: [], route_afters: []) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rackr/router.rb', line 87

def add(method, path, endpoint, as: nil, route_befores: [], route_afters: [])
  Errors.check_path(path)
  Errors.check_endpoint(endpoint, path)
  Errors.check_as(as, path)
  Errors.check_callbacks(route_befores, path)
  Errors.check_callbacks(route_afters, path)

  method = :get if method == :head

  wildcard = path == '*'
  path = path.is_a?(Symbol) ? path.inspect : path.sub(%r{\A/}, '')
  path_with_scopes = "/#{not_empty_scopes.join('/')}#{put_path_slash(path)}"
  add_named_route(method, path_with_scopes, as)

  route_instance =
    PathRoute.new(
      path_with_scopes,
      endpoint,
      befores: @befores + ensure_array(route_befores),
      afters: @afters + ensure_array(route_afters),
      wildcard: wildcard
    )

  return push_to_scope(method.to_s.upcase, route_instance) if @scopes.size >= 1

  @path_routes_instances[method.to_s.upcase][:__instances].push(route_instance)
end

#append_scope(name, scope_befores: [], scope_afters: []) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rackr/router.rb', line 159

def append_scope(name, scope_befores: [], scope_afters: [])
  Errors.check_scope_name(name)
  Errors.check_scope_slashes(name)
  Errors.check_callbacks(scope_befores, name)
  Errors.check_callbacks(scope_afters, name)

  name = ":#{name}" if name.is_a? Symbol

  @scopes.push(name)

  scope_befores = ensure_array(scope_befores)
  @befores.concat(scope_befores)
  @scopes_befores[name] = scope_befores

  scope_afters = ensure_array(scope_afters)
  @afters.concat(scope_afters)
  @scopes_afters[name] = scope_afters
end

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rackr/router.rb', line 45

def call(env)
  path_info = env['PATH_INFO']

  @splitted_request_path_info = path_info.split('/')
  @current_request_path_info =
    path_info == '/' ? path_info : path_info.chomp('/') # remove trailing "/"

  request_builder = BuildRequest.new(env, @splitted_request_path_info)
  env['REQUEST_METHOD'] = 'GET' if env['REQUEST_METHOD'] == 'HEAD'

  route_instance, found_scopes = match_path_route(env['REQUEST_METHOD'])
  rack_request = request_builder.call(route_instance)
  befores = route_instance.befores
  before_result = nil

  begin
    i = 0
    while i < befores.size
      before_result = Endpoint.call(befores[i], rack_request)
      return before_result unless before_result.is_a?(Rack::Request)

      rack_request = before_result

      i += 1
    end

    endpoint_result = Endpoint.call(route_instance.endpoint, before_result || rack_request)

    call_afters(route_instance, endpoint_result)
  rescue Rackr::NotFound
    return not_found_fallback(found_scopes, route_instance, before_result || rack_request)
  rescue Exception => e
    if !@dev_mode || ENV['RACKR_ERROR_DEV']
      return error_fallback(found_scopes, route_instance, before_result || rack_request, e)
    end

    return Endpoint.call(Errors::DevHtml, env.merge({ 'error' => e }))
  end

  endpoint_result
end

#call_afters(route_instance, endpoint_result) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/rackr/router.rb', line 150

def call_afters(route_instance, endpoint_result)
  afters = route_instance.afters
  i = 0
  while i < afters.size
    Endpoint.call(afters[i], endpoint_result)
    i += 1
  end
end

#clear_last_scopeObject



178
179
180
181
182
# File 'lib/rackr/router.rb', line 178

def clear_last_scope
  @befores -= @scopes_befores[@scopes.last]
  @afters -= @scopes_afters[@scopes.last]
  @scopes = @scopes.first(@scopes.size - 1)
end