Class: Sliver::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/sliver/path.rb

Constant Summary collapse

METHOD_KEY =
"REQUEST_METHOD".freeze
PATH_KEY =
"PATH_INFO".freeze
EMPTY_PATH =
"".freeze
ROOT_PATH =
"/".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, string) ⇒ Path

Returns a new instance of Path.



11
12
13
14
# File 'lib/sliver/path.rb', line 11

def initialize(http_method, string)
  @http_method = http_method.to_s.upcase
  @string      = normalised_path string
end

Instance Attribute Details

#http_methodObject (readonly)

Returns the value of attribute http_method.



9
10
11
# File 'lib/sliver/path.rb', line 9

def http_method
  @http_method
end

#stringObject (readonly)

Returns the value of attribute string.



9
10
11
# File 'lib/sliver/path.rb', line 9

def string
  @string
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/sliver/path.rb', line 16

def eql?(other)
  http_method == other.http_method && string == other.string
end

#hashObject



20
21
22
# File 'lib/sliver/path.rb', line 20

def hash
  @hash ||= "#{http_method} #{string}".hash
end

#matches?(environment) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/sliver/path.rb', line 24

def matches?(environment)
  method = environment[METHOD_KEY]
  path   = normalised_path environment[PATH_KEY]

  http_method == method && path[string_to_regexp]
end

#to_params(environment) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sliver/path.rb', line 31

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