Class: Grape::Router::Route

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/grape/router/route.rb

Constant Summary collapse

ROUTE_ATTRIBUTE_REGEXP =
/route_([_a-zA-Z]\w*)/.freeze
SOURCE_LOCATION_REGEXP =
/^(.*?):(\d+?)(?::in `.+?')?$/.freeze
FIXED_NAMED_CAPTURES =
%w[format version].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, pattern, **options) ⇒ Route

Returns a new instance of Route.


48
49
50
51
52
53
54
55
# File 'lib/grape/router/route.rb', line 48

def initialize(method, pattern, **options)
  method_s = method.to_s
  method_upcase = Grape::Http::Headers.find_supported_method(method_s) || method_s.upcase

  @options    = options.merge(method: method_upcase)
  @pattern    = Pattern.new(pattern, **options)
  @translator = AttributeTranslator.new(**options, request_method: method_upcase)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'lib/grape/router/route.rb', line 23

def method_missing(method_id, *arguments)
  match = ROUTE_ATTRIBUTE_REGEXP.match(method_id.to_s)
  if match
    method_name = match.captures.last.to_sym
    warn_route_methods(method_name, caller(1).shift)
    @options[method_name]
  else
    super
  end
end

Instance Attribute Details

#appObject

Returns the value of attribute app


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

def app
  @app
end

#indexObject

Returns the value of attribute index


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

def index
  @index
end

#optionsObject

Returns the value of attribute options


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

def options
  @options
end

#patternObject

Returns the value of attribute pattern


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

def pattern
  @pattern
end

#translatorObject Also known as: attributes

Returns the value of attribute translator


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

def translator
  @translator
end

Instance Method Details

#apply(app) ⇒ Object


61
62
63
64
# File 'lib/grape/router/route.rb', line 61

def apply(app)
  @app = app
  self
end

#exec(env) ⇒ Object


57
58
59
# File 'lib/grape/router/route.rb', line 57

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

#match?(input) ⇒ Boolean

Returns:

  • (Boolean)

66
67
68
# File 'lib/grape/router/route.rb', line 66

def match?(input)
  translator.respond_to?(:forward_match) && translator.forward_match ? input.start_with?(pattern.origin) : pattern.match?(input)
end

#params(input = nil) ⇒ Object


70
71
72
73
74
75
76
77
78
79
# File 'lib/grape/router/route.rb', line 70

def params(input = nil)
  if input.nil?
    pattern.named_captures.keys.each_with_object(translator.params) do |(key), defaults|
      defaults[key] ||= '' unless FIXED_NAMED_CAPTURES.include?(key) || defaults.key?(key)
    end
  else
    parsed = pattern.params(input)
    parsed ? parsed.delete_if { |_, value| value.nil? }.symbolize_keys : {}
  end
end

#respond_to_missing?(method_id, _) ⇒ Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/grape/router/route.rb', line 34

def respond_to_missing?(method_id, _)
  ROUTE_ATTRIBUTE_REGEXP.match?(method_id.to_s)
end

#route_methodObject


38
39
40
41
# File 'lib/grape/router/route.rb', line 38

def route_method
  warn_route_methods(:method, caller(1).shift, :request_method)
  request_method
end

#route_pathObject


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

def route_path
  warn_route_methods(:path, caller(1).shift)
  pattern.path
end