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*)/
SOURCE_LOCATION_REGEXP =
/^(.*?):(\d+?)(?::in `.+?')?$/
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.



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

def initialize(method, pattern, **options)
  @suffix     = options[:suffix]
  @options    = options.merge(method: method.to_s.upcase)
  @pattern    = Pattern.new(pattern, **options)
  @translator = AttributeTranslator.new(**options, request_method: method.to_s.upcase)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/grape/router/route.rb', line 20

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.



13
14
15
# File 'lib/grape/router/route.rb', line 13

def app
  @app
end

#indexObject

Returns the value of attribute index.



13
14
15
# File 'lib/grape/router/route.rb', line 13

def index
  @index
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/grape/router/route.rb', line 13

def options
  @options
end

#patternObject

Returns the value of attribute pattern.



13
14
15
# File 'lib/grape/router/route.rb', line 13

def pattern
  @pattern
end

#regexpObject

Returns the value of attribute regexp.



13
14
15
# File 'lib/grape/router/route.rb', line 13

def regexp
  @regexp
end

#translatorObject Also known as: attributes

Returns the value of attribute translator.



13
14
15
# File 'lib/grape/router/route.rb', line 13

def translator
  @translator
end

Instance Method Details

#apply(app) ⇒ Object



75
76
77
78
# File 'lib/grape/router/route.rb', line 75

def apply(app)
  @app = app
  self
end

#exec(env) ⇒ Object



71
72
73
# File 'lib/grape/router/route.rb', line 71

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

#match?(input) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/grape/router/route.rb', line 80

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

#params(input = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/grape/router/route.rb', line 84

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)


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

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

#route_methodObject



54
55
56
57
# File 'lib/grape/router/route.rb', line 54

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

#route_pathObject



59
60
61
62
# File 'lib/grape/router/route.rb', line 59

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