class Rack::Way::Router::Route

Attributes

endpoint[R]
splitted_path[R]

Public Class Methods

new(path, endpoint) click to toggle source
# File lib/rack-way/router/route.rb, line 7
def initialize(path, endpoint)
  @path = path
  @splitted_path = @path.split('/')
  @endpoint = endpoint
  @params = fetch_params
end

Public Instance Methods

has_params?() click to toggle source
# File lib/rack-way/router/route.rb, line 22
def has_params?
  @params != []
end
match?(env) click to toggle source
# File lib/rack-way/router/route.rb, line 14
def match?(env)
  if has_params?
    return match_with_params?(env)
  end

  env['REQUEST_PATH'] == @path
end

Private Instance Methods

fetch_params() click to toggle source
# File lib/rack-way/router/route.rb, line 28
def fetch_params
  @splitted_path.select { |value| value.start_with? ':' }
end
match_with_params?(env) click to toggle source
# File lib/rack-way/router/route.rb, line 32
def match_with_params?(env)
  splitted_request_path = env['REQUEST_PATH'].split('/')

  if @splitted_path.size != splitted_request_path.size
    return false
  end

  matched =
    @splitted_path
      .map
      .with_index do |segment, i|
        if segment.start_with?(':')
          true
        else
          splitted_request_path[i] == segment
        end
      end

  !matched.include?(false)
end