Class: Grape::Router::Route

Inherits:
BaseRoute show all
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

Attributes inherited from BaseRoute

#namespace, #options, #pattern, #prefix, #regexp_capture_group, #regexp_capture_index, #settings

Instance Method Summary collapse

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, options, forward_match:, params: {}, **route_attributes)
  super(pattern, options, **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

#appObject (readonly)

Returns the value of attribute app.



11
12
13
# File 'lib/grape/router/route.rb', line 11

def app
  @app
end

#indexObject (readonly)

Returns the value of attribute index.



11
12
13
# File 'lib/grape/router/route.rb', line 11

def index
  @index
end

#request_methodObject (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

Returns:

  • (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

#paramsObject

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_headObject



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