Class: Rack::HttpRouter::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/rack-http_router/router.rb,
lib/rack-http_router/router/route.rb,
lib/rack-http_router/router/build_request.rb

Defined Under Namespace

Classes: BuildRequest, Route, UndefinedNamedRoute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Router

Returns a new instance of Router.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rack-http_router/router.rb', line 12

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(UndefinedNamedRoute, "Undefined named route: '#{key}'")
    end
  @config = config
  @scopes = []
  @error = proc { |_req, e| raise e }
  @not_found = proc { [404, {}, ['Not found']] }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/rack-http_router/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/rack-http_router/router.rb', line 9

def not_found=(value)
  @not_found = value
end

#routeObject (readonly)

Returns the value of attribute route.



10
11
12
# File 'lib/rack-http_router/router.rb', line 10

def route
  @route
end

Instance Method Details

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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rack-http_router/router.rb', line 51

def add(method, path, endpoint, as = nil)
  method = :get if method == :head

  path_with_scopes = "/#{@scopes.join('/')}#{put_path_slash(path)}"
  @route[as] = path_with_scopes if as

  route_instance = Route.new(path_with_scopes, endpoint)

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

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

#add_error(endpoint) ⇒ Object



70
71
72
# File 'lib/rack-http_router/router.rb', line 70

def add_error(endpoint)
  @error = endpoint
end

#add_not_found(endpoint) ⇒ Object



66
67
68
# File 'lib/rack-http_router/router.rb', line 66

def add_not_found(endpoint)
  @not_found = endpoint
end

#append_scope(name) ⇒ Object



74
75
76
# File 'lib/rack-http_router/router.rb', line 74

def append_scope(name)
  @scopes.push(name)
end

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rack-http_router/router.rb', line 27

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?

  if route_instance.endpoint.respond_to?(:call)
    return route_instance.endpoint.call(
      request_builder.call(route_instance)
    )
  end

  if route_instance.endpoint.include?(Rack::HttpRouter::Action)
    return route_instance.endpoint.new(route: @route, config: @config)
      .call(request_builder.call(route_instance))
  end

  route_instance.endpoint.new.call(request_builder.call(route_instance))
rescue Exception => e
  @error.call(request_builder.call, e)
end

#clear_last_scopeObject



78
79
80
# File 'lib/rack-http_router/router.rb', line 78

def clear_last_scope
  @scopes = @scopes.first(@scopes.size - 1)
end