class RapidRunty::Router::RouteParser

Parses the route, extracts and return the various important parts of the url.

@param [String] the request url

Attributes

options[RW]
path[RW]

Public Class Methods

new(url) click to toggle source
# File lib/rapid_runty/router/route_parser.rb, line 10
def initialize(url)
  self.path = split_url(url[:url])
  self.options = url[:to]
rescue TypeError
  self.path = split_url(url)
end

Public Instance Methods

==(other) click to toggle source
# File lib/rapid_runty/router/route_parser.rb, line 35
def ==(other)
  is_match = size_match?(other) && path_match?(other)
  @match = other if is_match
  is_match
end
placeholders() click to toggle source

Return the present placeholders for the url

@return [Hash] of the placeholder key and its value.

# File lib/rapid_runty/router/route_parser.rb, line 25
def placeholders
  return {} unless @match

  placeholders = {}
  path.each_with_index do |part, i|
    placeholders[part.delete(':').to_s] = @match.path[i] if part[0] == ':'
  end
  placeholders
end
to_s() click to toggle source
# File lib/rapid_runty/router/route_parser.rb, line 17
def to_s
  '/' + path.join('/')
end

Private Instance Methods

path_match?(url) click to toggle source
# File lib/rapid_runty/router/route_parser.rb, line 47
def path_match?(url)
  path.each_with_index do |part, i|
    return false unless part[0] == ':' || url.path[i] == part
  end
  true
end
size_match?(url) click to toggle source
# File lib/rapid_runty/router/route_parser.rb, line 43
def size_match?(url)
  path.size == url.path.size
end
split_url(url) click to toggle source
# File lib/rapid_runty/router/route_parser.rb, line 54
def split_url(url)
  url = url.to_s
  path_parts = url.split('/').reject(&:empty?)
  path_parts = [''] if url == '/'
  path_parts
end