class Sliver::Path

Constants

EMPTY_PATH
METHOD_KEY
PATH_KEY
ROOT_PATH

Attributes

http_method[R]
string[R]

Public Class Methods

new(http_method, string) click to toggle source
# File lib/sliver/path.rb, line 9
def initialize(http_method, string)
  @http_method = http_method.to_s.upcase
  @string      = normalised_path string
end

Public Instance Methods

eql?(other) click to toggle source
# File lib/sliver/path.rb, line 14
def eql?(other)
  http_method == other.http_method && string == other.string
end
hash() click to toggle source
# File lib/sliver/path.rb, line 18
def hash
  @hash ||= "#{http_method} #{string}".hash
end
matches?(environment) click to toggle source
# File lib/sliver/path.rb, line 22
def matches?(environment)
  method = environment[METHOD_KEY]
  path   = normalised_path environment[PATH_KEY]

  http_method == method && path[string_to_regexp]
end
to_params(environment) click to toggle source
# File lib/sliver/path.rb, line 29
def to_params(environment)
  return {} unless matches?(environment)

  path   = normalised_path environment[PATH_KEY]
  values = path.scan(string_to_regexp).flatten

  string_keys.each_with_index.inject({}) do |hash, (key, index)|
    hash[key] = values[index]
    hash
  end
end

Private Instance Methods

normalised_path(string) click to toggle source
# File lib/sliver/path.rb, line 43
def normalised_path(string)
  string == EMPTY_PATH ? ROOT_PATH : string
end
string_keys() click to toggle source
# File lib/sliver/path.rb, line 47
def string_keys
  @string_keys ||= string.to_s.scan(/:([\w-]+)/i).flatten
end
string_to_regexp() click to toggle source
# File lib/sliver/path.rb, line 51
def string_to_regexp
  @string_to_regexp ||= Regexp.new(
    "\\A" + string.to_s.gsub(/:[\w-]+/, "([\\w\\-\\.\\\\+]+)") + "\\z"
  )
end