class Grape::Router::Route

Constants

FIXED_NAMED_CAPTURES
ROUTE_ATTRIBUTE_REGEXP
SOURCE_LOCATION_REGEXP

Attributes

app[RW]
attributes[RW]
index[RW]
options[RW]
pattern[RW]
translator[RW]

Public Class Methods

new(method, pattern, **options) click to toggle source
# 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

Public Instance Methods

apply(app) click to toggle source
# File lib/grape/router/route.rb, line 61
def apply(app)
  @app = app
  self
end
exec(env) click to toggle source
# File lib/grape/router/route.rb, line 57
def exec(env)
  @app.call(env)
end
match?(input) click to toggle source
# 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
method_missing(method_id, *arguments) click to toggle source
Calls superclass method
# 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
params(input = nil) click to toggle source
# 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, _) click to toggle source
# File lib/grape/router/route.rb, line 34
def respond_to_missing?(method_id, _)
  ROUTE_ATTRIBUTE_REGEXP.match?(method_id.to_s)
end
route_method() click to toggle source
# File lib/grape/router/route.rb, line 38
def route_method
  warn_route_methods(:method, caller(1).shift, :request_method)
  request_method
end
route_path() click to toggle source
# File lib/grape/router/route.rb, line 43
def route_path
  warn_route_methods(:path, caller(1).shift)
  pattern.path
end

Private Instance Methods

warn_route_methods(name, location, expected = nil) click to toggle source
# File lib/grape/router/route.rb, line 83
      def warn_route_methods(name, location, expected = nil)
        path, line = *location.scan(SOURCE_LOCATION_REGEXP).first
        path = File.realpath(path) if Pathname.new(path).relative?
        expected ||= name
        warn <<~WARNING
          #{path}:#{line}: The route_xxx methods such as route_#{name} have been deprecated, please use #{expected}.
        WARNING
      end