Class: Rack::Router::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/router/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, logger: nil, &block) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
11
12
13
14
15
16
17
# File 'lib/rack/router/middleware.rb', line 8

def initialize(app, logger: nil, &block)
  @app = app
  @routes = RouteBuilder.new
  @current_path = nil
  @current_options = {}
  @not_found = nil
  @logger = logger || Logger.new(STDOUT)
  @callbacks = []
  instance_exec(&block)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/rack/router/middleware.rb', line 6

def logger
  @logger
end

Instance Method Details

#after_route {|env, route| ... } ⇒ Object

Yields:

  • after route match executed

Yield Parameters:

Raises:

  • (ArgumentError)


104
105
106
107
108
# File 'lib/rack/router/middleware.rb', line 104

def after_route(&block)
  raise ArgumentError, 'block must be given' unless block_given?

  @callbacks.push(block)
end

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/router/middleware.rb', line 19

def call(env)
  route = @routes.match(env)
  env[RACK_ROUTER_PATH] = route&.value
  env[RACK_ROUTER_PATH_HASH] = route&.path_hash env[Rack::PATH_INFO]
  run_after_route(env, route)

  if route.nil? && !@not_found.nil?
    logger.debug { "#{self.class}#call route was not found\n#{@routes.print_routes}" }
    return render_not_found(env)
  end

  @app.call(env)
end

#define_path(http_method, path, value, options = {}) ⇒ Object

Parameters:

  • http_method (String, Symbol)
  • path (String)
  • value (String, Proc<Hash>)
  • options (Hash) (defaults to: {})

    :constraint [Hash<Symbol=>Proc<String>>] :content_type [Array<String>,String]



89
90
91
92
93
# File 'lib/rack/router/middleware.rb', line 89

def define_path(http_method, path, value, options = {})
  options = @current_options.dup.merge(options)
  path = [@current_path, path].compact.join('/')
  @routes.add(http_method, path, value, options)
end

#match(path, value, options = {}) ⇒ Object



44
45
46
# File 'lib/rack/router/middleware.rb', line 44

def match(path, value, options = {})
  define_path(nil, path, value, options)
end

#namespace(path, &block) ⇒ Object

Parameters:

  • path (String)


64
65
66
# File 'lib/rack/router/middleware.rb', line 64

def namespace(path, &block)
  nested(path, {}, &block)
end

#nested(path, options = {}) ⇒ Object

Parameters:

  • path (String)
  • options (Hash<Symbol>, Hash) (defaults to: {})

    :constraint [Hash<Symbol=>Proc<String>>] :content_type [String, Array<String>]



52
53
54
55
56
57
58
59
60
61
# File 'lib/rack/router/middleware.rb', line 52

def nested(path, options = {})
  old_current_path = @current_path
  old_current_options = @current_options
  @current_path = [@current_path, path].compact.join('/')
  @current_options = @current_options.dup.merge(options)
  yield
ensure
  @current_path = old_current_path
  @current_options = old_current_options
end

#not_found(response) ⇒ Object



95
96
97
98
99
# File 'lib/rack/router/middleware.rb', line 95

def not_found(response)
  response = default_not_found if response == :default
  response = nil unless response
  @not_found = response
end

#with_accept(accept, &block) ⇒ Object

Parameters:

  • accept (String, Array<String>)


79
80
81
# File 'lib/rack/router/middleware.rb', line 79

def with_accept(accept, &block)
  nested(nil, accept: accept, &block)
end

#with_constraint(constraint, &block) ⇒ Object

Parameters:

  • constraint (Hash<Symbol,Proc<String>>)


69
70
71
# File 'lib/rack/router/middleware.rb', line 69

def with_constraint(constraint, &block)
  nested(nil, constraint: constraint, &block)
end

#with_content_type(content_type, &block) ⇒ Object

Parameters:

  • content_type (String, Array<String>)


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

def with_content_type(content_type, &block)
  nested(nil, content_type: content_type, &block)
end