Method: Grape::DSL::Routing::ClassMethods#route

Defined in:
lib/grape/dsl/routing.rb

#route(methods, paths = ['/'], route_options = {}, &block) ⇒ Object

Defines a route that will be recognized by the Grape API.

Examples:

Defining a basic route.

class MyAPI < Grape::API
  route(:any, '/hello') do
    {hello: 'world'}
  end
end

Parameters:

  • methods (HTTP Verb)

    One or more HTTP verbs that are accepted by this route. Set to ‘:any` if you want any verb to be accepted.

  • paths (String) (defaults to: ['/'])

    One or more strings representing the URL segment(s) for this route.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/grape/dsl/routing.rb', line 127

def route(methods, paths = ['/'], route_options = {}, &block)
  methods = '*' if methods == :any
  endpoint_options = {
    method: methods,
    path: paths,
    for: self,
    route_options: {
      params: namespace_stackable_with_hash(:params) || {}
    }.deep_merge(route_setting(:description) || {}).deep_merge(route_options || {})
  }

  new_endpoint = Grape::Endpoint.new(inheritable_setting, endpoint_options, &block)
  endpoints << new_endpoint unless endpoints.any? { |e| e.equals?(new_endpoint) }

  route_end
  reset_validations!
end