class Rack::Router::RouteBuilder

Public Class Methods

new() click to toggle source
# File lib/rack/router/route_builder.rb, line 4
def initialize
  @routes = []
end

Public Instance Methods

add(http_method, path, value, options = {}) click to toggle source

@param http_method [Symbol,String] @param path [String] @param value [String, Proc<Hash>] @param options [Hash]

:constraint [Hash<Symbol,Proc<String>>]
:content_type [Array<String>,String]
# File lib/rack/router/route_builder.rb, line 14
def add(http_method, path, value, options = {})
  path = path.split('/').reject(&:empty?).join('/')
  route = Route.new(
      http_method: http_method.to_s.upcase,
      path: path,
      value: value,
      **options
  )
  @routes.push(route)
  route
end
match(env) click to toggle source
# File lib/rack/router/route_builder.rb, line 26
def match(env)
  http_method = env[Rack::REQUEST_METHOD]
  path = env[Rack::PATH_INFO].to_s.gsub(/\..*\z/, '')
  content_type, *_ = env['CONTENT_TYPE'].to_s.split(';')
  content_type = nil if !content_type.nil? && content_type.empty?
  accept, *_ = env['HTTP_ACCEPT'].to_s.split(';')
  accept = accept.to_s.split(',')

  match_request(http_method, path, content_type: content_type, accept: accept)
end
match_request(http_method, path, content_type: nil, accept: []) click to toggle source

@param http_method [String] upcase GET POST PUT PATCH DELETE HEAD @param path [String] @param content_type [String, NilClass] @param accept [Array]

# File lib/rack/router/route_builder.rb, line 41
def match_request(http_method, path, content_type: nil, accept: [])
  path_parts = path.split('/').reject(&:empty?)
  @routes.detect do |route|
    next false if route.http_method && route.http_method != http_method
    next false if route.content_type && route.content_type.include?(content_type)
    next false if route.accept && (route.accept & accept).empty? && !accept.include?('*/*')
    next false if route.path_parts.size != path_parts.size
    next false if route.path_parts.map.with_index { |part, idx| part != path_parts[idx] }.any?
    true
  end
end
print_routes() click to toggle source