Class: Rutter::Route
- Inherits:
-
Object
- Object
- Rutter::Route
- Defined in:
- lib/rutter/route.rb
Overview
Represents a single route.
Constant Summary collapse
- VERBS =
Valid request verbs.
%w[GET POST PUT PATCH DELETE OPTIONS HEAD TRACE].freeze
Instance Attribute Summary collapse
-
#endpoint ⇒ Hash
readonly
Route endpoint.
-
#method ⇒ String
readonly
Request method to match.
-
#path ⇒ String
readonly
Path to match.
Instance Method Summary collapse
-
#expand(params = {}) ⇒ String
Expands the pattern with the given arguments.
-
#match?(path_info) ⇒ Boolean
Matches the given path to the route pattern.
-
#params(path) ⇒ nil, Hash<String => String>
Extract params from the given path.
Instance Attribute Details
#endpoint ⇒ Hash (readonly)
Route endpoint.
14 15 16 |
# File 'lib/rutter/route.rb', line 14 def endpoint @endpoint end |
#method ⇒ String (readonly)
Request method to match.
14 15 16 |
# File 'lib/rutter/route.rb', line 14 def method @method end |
#path ⇒ String (readonly)
Path to match.
14 15 16 |
# File 'lib/rutter/route.rb', line 14 def path @path end |
Instance Method Details
#expand(params = {}) ⇒ String
Expands the pattern with the given arguments.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rutter/route.rb', line 95 def (params = {}) string = if @dynamic new_path = path.gsub(SEGMENT_MATCH) do |match| params.delete(match[1..-1].to_sym) end new_path.gsub(/\(|\)/, "") else path.dup end string = normalize_path(string) return string if params.empty? "#{string}?#{Rack::Utils.build_nested_query(params)}" end |
#match?(path_info) ⇒ Boolean
Matches the given path to the route pattern.
59 60 61 |
# File 'lib/rutter/route.rb', line 59 def match?(path_info) @dynamic ? path_info.match?(@pattern) : path_info == @path end |
#params(path) ⇒ nil, Hash<String => String>
Extract params from the given path.
NOTE: If the path does not match, nil is returned.
71 72 73 74 75 |
# File 'lib/rutter/route.rb', line 71 def params(path) return unless (result = path.match(@pattern)) values = result.captures.map { |v| CGI.unescape(v) if v } Hash[result.names.zip(values)] end |