Class: Grape::Router::Route
- Extended by:
- Forwardable
- Defined in:
- lib/grape/router/route.rb
Constant Summary collapse
- FORWARD_MATCH_METHOD =
->(input, pattern) { input.start_with?(pattern.origin) }
- NON_FORWARD_MATCH_METHOD =
->(input, pattern) { pattern.match?(input) }
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#request_method ⇒ Object
readonly
Returns the value of attribute request_method.
Attributes inherited from BaseRoute
#namespace, #options, #pattern, #prefix, #regexp_capture_group, #regexp_capture_index, #settings
Instance Method Summary collapse
- #apply(app) ⇒ Object
-
#initialize(endpoint, method, pattern, options, forward_match:, params: {}, **route_attributes) ⇒ Route
constructor
A new instance of Route.
- #match?(input) ⇒ Boolean
-
#params ⇒ Object
The route's declared params keyed by name — path captures plus any declared body/query params, as their definitions.
-
#params_for(input) ⇒ Object
Extract param values from a matched request path.
- #to_head ⇒ Object
Methods inherited from BaseRoute
#default, #failure, #pattern_regexp, #resolve_capture_group!, #success, #to_regexp
Constructor Details
#initialize(endpoint, method, pattern, options, forward_match:, params: {}, **route_attributes) ⇒ Route
Returns a new instance of Route.
15 16 17 18 19 20 21 |
# File 'lib/grape/router/route.rb', line 15 def initialize(endpoint, method, pattern, , forward_match:, params: {}, **route_attributes) super(pattern, , **route_attributes) @app = endpoint @request_method = upcase_method(method) @match_function = forward_match ? FORWARD_MATCH_METHOD : NON_FORWARD_MATCH_METHOD @declared_params = params end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
11 12 13 |
# File 'lib/grape/router/route.rb', line 11 def app @app end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
11 12 13 |
# File 'lib/grape/router/route.rb', line 11 def index @index end |
#request_method ⇒ Object (readonly)
Returns the value of attribute request_method.
11 12 13 |
# File 'lib/grape/router/route.rb', line 11 def request_method @request_method end |
Instance Method Details
#apply(app) ⇒ Object
29 30 31 32 |
# File 'lib/grape/router/route.rb', line 29 def apply(app) @app = app self end |
#match?(input) ⇒ Boolean
34 35 36 37 38 |
# File 'lib/grape/router/route.rb', line 34 def match?(input) return false if input.blank? @match_function.call(input, pattern) end |
#params ⇒ Object
The route's declared params keyed by name — path captures plus any declared body/query params, as their definitions. Used for documentation (e.g. grape-swagger), not for extracting request values.
43 44 45 |
# File 'lib/grape/router/route.rb', line 43 def params @params ||= pattern.captures_default.merge(@declared_params) end |
#params_for(input) ⇒ Object
Extract param values from a matched request path. Used by the router.
A pattern with no named captures has nothing to extract, so it skips the match entirely and answers nil — the same way GreedyRoute#params_for does, and what the router already coerces into the Hash it builds routing args in.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/grape/router/route.rb', line 53 def params_for(input) return unless pattern.captures? parsed = pattern.params(input) return unless parsed params = {} parsed.each { |name, value| params[name.to_sym] = tag_utf8!(value) unless value.nil? } params end |
#to_head ⇒ Object
23 24 25 26 27 |
# File 'lib/grape/router/route.rb', line 23 def to_head head = dup head.convert_to_head_request! head end |