Class: Rackr::Router

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

Defined Under Namespace

Modules: Errors Classes: BuildRequest, Route

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Router.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rackr/router.rb', line 12

def initialize(config = {}, before: [], after: [])
  @instance_routes = {}
  %w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method|
    @instance_routes[method] = { __instances: [] }
  end
  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

  @config = config
  @branches = []
  @befores = ensure_array(before)
  @branches_befores = {}
  @afters = ensure_array(after)
  @branches_afters = {}
  @error = proc { |_req, e| raise e }
  @not_found = proc { [404, {}, ['Not found']] }
  @splitted_request_path = []
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/rackr/router.rb', line 10

def config
  @config
end

#not_found=(value) ⇒ Object (writeonly)

Sets the attribute not_found

Parameters:

  • value

    the value to set the attribute not_found to.



9
10
11
# File 'lib/rackr/router.rb', line 9

def not_found=(value)
  @not_found = value
end

#routesObject (readonly)

Returns the value of attribute routes.



10
11
12
# File 'lib/rackr/router.rb', line 10

def routes
  @routes
end

Instance Method Details

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rackr/router.rb', line 76

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

  path_with_branches = "/#{@branches.join('/')}#{put_path_slash(path)}"
  add_named_route(method, path_with_branches, as)

  route_instance =
    Route.new(
      path_with_branches,
      endpoint,
      befores: @befores + ensure_array(route_befores),
      afters: @afters + ensure_array(route_afters)
    )

  return push_to_branch(method.to_s.upcase, route_instance) if @branches.size >= 1

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

#add_error(endpoint) ⇒ Object



107
108
109
110
111
# File 'lib/rackr/router.rb', line 107

def add_error(endpoint)
  Errors.check_endpoint(endpoint, 'error')

  @error = endpoint
end

#add_not_found(endpoint) ⇒ Object



101
102
103
104
105
# File 'lib/rackr/router.rb', line 101

def add_not_found(endpoint)
  Errors.check_endpoint(endpoint, 'not_found')

  @not_found = endpoint
end

#append_branch(name, branch_befores: [], branch_afters: []) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rackr/router.rb', line 113

def append_branch(name, branch_befores: [], branch_afters: [])
  Errors.check_branch_name(name)
  Errors.check_callbacks(branch_befores, name)
  Errors.check_callbacks(branch_afters, name)

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

  @branches.push(name)

  branch_befores = ensure_array(branch_befores)
  @befores.concat(branch_befores)
  @branches_befores[name] = branch_befores

  branch_afters = ensure_array(branch_afters)
  @afters.concat(branch_afters)
  @branches_afters[name] = branch_afters
end

#call(env) ⇒ Object



36
37
38
39
40
41
42
43
44
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
# File 'lib/rackr/router.rb', line 36

def call(env)
  @splitted_request_path = env['PATH_INFO'].split('/')

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

  route_instance = match_route(env)

  return render_not_found(request_builder.call) if route_instance.nil?

  rack_request = request_builder.call(route_instance)

  befores = route_instance.befores
  before_result = nil
  i = 0
  while i < befores.size
    before_result = call_endpoint(befores[i], rack_request)
    return before_result unless before_result.is_a?(Rack::Request)

    rack_request = before_result

    i += 1
  end

  endpoint_result = call_endpoint(route_instance.endpoint, before_result || rack_request)

  afters = route_instance.afters
  i = 0
  while i < afters.size
    call_endpoint(afters[i], endpoint_result)
    i += 1
  end

  endpoint_result
rescue Rackr::NotFound
  render_not_found(request_builder.call)
rescue Exception => e
  @error.call(request_builder.call, e)
end

#clear_last_branchObject



131
132
133
134
135
# File 'lib/rackr/router.rb', line 131

def clear_last_branch
  @befores -= @branches_befores[@branches.last]
  @afters -= @branches_afters[@branches.last]
  @branches = @branches.first(@branches.size - 1)
end