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 = {}) ⇒ Router

Returns a new instance of Router.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rackr/router.rb', line 10

def initialize(config = {})
  @routes = {}
  %w[GET POST DELETE PUT TRACE OPTIONS PATCH].each do |method|
    @routes[method] = { __instances: [] }
  end
  @route =
    Hash.new do |_hash, key|
      raise(Errors::UndefinedNamedRouteError, "Undefined named route: '#{key}'")
    end
  @config = config
  @branches = []
  @befores = []
  @branches_befores = {}
  @nameds_as = []
  @branches_named_as = {}
  @error = proc { |_req, e| raise e }
  @not_found = proc { [404, {}, ['Not found']] }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

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.



7
8
9
# File 'lib/rackr/router.rb', line 7

def not_found=(value)
  @not_found = value
end

#routeObject (readonly)

Returns the value of attribute route.



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

def route
  @route
end

Instance Method Details

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rackr/router.rb', line 54

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

  method = :get if method == :head

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

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

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

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

#add_error(endpoint) ⇒ Object



81
82
83
84
85
# File 'lib/rackr/router.rb', line 81

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

  @error = endpoint
end

#add_not_found(endpoint) ⇒ Object



75
76
77
78
79
# File 'lib/rackr/router.rb', line 75

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

  @not_found = endpoint
end

#append_branch(name, branch_befores = [], as = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rackr/router.rb', line 87

def append_branch(name, branch_befores = [], as = nil)
  Errors.check_branch_name(name)
  Errors.check_as(as, @branches.join('/'))
  Errors.check_callbacks(branch_befores, name)

  @branches.push(name)

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

  @nameds_as.push(as)
  @branches_named_as[name] = as
end

#call(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rackr/router.rb', line 29

def call(env)
  request_builder = BuildRequest.new(env)
  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)

    i += 1
  end

  call_endpoint(route_instance.endpoint, before_result || rack_request)
rescue Exception => e
  @error.call(request_builder.call, e)
end

#clear_last_branchObject



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

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