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

#index, #options, #pattern

Instance Method Summary collapse

Methods inherited from BaseRoute

#pattern_regexp, #regexp_capture_index, #to_regexp

Constructor Details

#initialize(method, origin, path, options) ⇒ Route

Returns a new instance of Route.



15
16
17
18
19
20
# File 'lib/grape/router/route.rb', line 15

def initialize(method, origin, path, options)
  @request_method = upcase_method(method)
  @pattern = Grape::Router::Pattern.new(origin, path, options)
  @match_function = options[:forward_match] ? FORWARD_MATCH_METHOD : NON_FORWARD_MATCH_METHOD
  super(options)
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

#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



30
31
32
33
# File 'lib/grape/router/route.rb', line 30

def apply(app)
  @app = app
  self
end

#convert_to_head_request!Object



22
23
24
# File 'lib/grape/router/route.rb', line 22

def convert_to_head_request!
  @request_method = Rack::HEAD
end

#exec(env) ⇒ Object



26
27
28
# File 'lib/grape/router/route.rb', line 26

def exec(env)
  @app.call(env)
end

#match?(input) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/grape/router/route.rb', line 35

def match?(input)
  return false if input.blank?

  @match_function.call(input, pattern)
end

#params(input = nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/grape/router/route.rb', line 41

def params(input = nil)
  return params_without_input if input.blank?

  parsed = pattern.params(input)
  return {} unless parsed

  parsed.compact.symbolize_keys
end