Module: Grape::DSL::Routing

Included in:
API::Instance
Defined in:
lib/grape/dsl/routing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



6
7
8
# File 'lib/grape/dsl/routing.rb', line 6

def endpoints
  @endpoints
end

Instance Method Details

#build_with(build_with) ⇒ Object



83
84
85
# File 'lib/grape/dsl/routing.rb', line 83

def build_with(build_with)
  inheritable_setting.namespace_inheritable[:build_params_with] = build_with
end

#cascade(value = nil) ⇒ Object



18
19
20
21
22
# File 'lib/grape/dsl/routing.rb', line 18

def cascade(value = nil)
  return inheritable_setting.namespace_inheritable.key?(:cascade) ? !inheritable_setting.namespace_inheritable[:cascade].nil? : true if value.nil?

  inheritable_setting.namespace_inheritable[:cascade] = value
end

#do_not_document!Object



101
102
103
# File 'lib/grape/dsl/routing.rb', line 101

def do_not_document!
  inheritable_setting.namespace_inheritable[:do_not_document] = true
end

#do_not_route_head!Object

Do not route HEAD requests to GET requests automatically.



88
89
90
# File 'lib/grape/dsl/routing.rb', line 88

def do_not_route_head!
  inheritable_setting.namespace_inheritable[:do_not_route_head] = true
end

#do_not_route_options!Object

Do not automatically route OPTIONS.



93
94
95
# File 'lib/grape/dsl/routing.rb', line 93

def do_not_route_options!
  inheritable_setting.namespace_inheritable[:do_not_route_options] = true
end

#given(conditional_option) ⇒ Object



8
9
10
11
12
# File 'lib/grape/dsl/routing.rb', line 8

def given(conditional_option, &)
  return unless conditional_option

  mounted(&)
end

#lint!Object



97
98
99
# File 'lib/grape/dsl/routing.rb', line 97

def lint!
  inheritable_setting.namespace_inheritable[:lint] = true
end

#mount(mounts, *opts) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/grape/dsl/routing.rb', line 105

def mount(mounts, *opts)
  mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
  mounts.each_pair do |app, path|
    if app.respond_to?(:mount_instance)
      opts_with = opts.any? ? opts.first[:with] : {}
      mount({ app.mount_instance(configuration: opts_with) => path }, *opts)
      next
    end
    in_setting = inheritable_setting

    if app.respond_to?(:inheritable_setting, true)
      mount_path = Grape::Router.normalize_path(path)
      app.top_level_setting.namespace_stackable[:mount_path] = mount_path

      app.inherit_settings(inheritable_setting)

      in_setting = app.top_level_setting

      app.change!
      change!
    end

    # When trying to mount multiple times the same endpoint, remove the previous ones
    # from the list of endpoints if refresh_already_mounted parameter is true
    refresh_already_mounted = opts.any? ? opts.first[:refresh_already_mounted] : false
    if refresh_already_mounted && !endpoints.empty?
      endpoints.delete_if do |endpoint|
        endpoint.options[:app].to_s == app.to_s
      end
    end

    endpoints << Grape::Endpoint.new(
      in_setting,
      method: :any,
      path: path,
      app: app,
      route_options: { anchor: false },
      forward_match: !app.respond_to?(:inheritable_setting),
      for: self
    )
  end
end

#mounted(&block) ⇒ Object



14
15
16
# File 'lib/grape/dsl/routing.rb', line 14

def mounted(&block)
  evaluate_as_instance_with_configuration(block, lazy: true)
end

#namespace(space = nil, requirements: nil, **options, &block) ⇒ Object Also known as: group, resource, resources, segment

Declare a “namespace”, which prefixes all subordinate routes with its name. Any endpoints within a namespace, group, resource or segment, etc., will share their parent context as well as any configuration done in the namespace context.

Examples:


namespace :foo do
  get 'bar' do
    # defines the endpoint: GET /foo/bar
  end
end


201
202
203
204
205
206
207
208
209
# File 'lib/grape/dsl/routing.rb', line 201

def namespace(space = nil, requirements: nil, **options, &block)
  return Namespace.joined_space_path(inheritable_setting.namespace_stackable[:namespace]) unless space || block

  within_namespace do
    nest(block) do
      inheritable_setting.namespace_stackable[:namespace] = Grape::Namespace.new(space, requirements: requirements, **options) if space
    end
  end
end

#prefix(prefix = nil) ⇒ Object

Define a root URL prefix for your entire API.



67
68
69
70
71
# File 'lib/grape/dsl/routing.rb', line 67

def prefix(prefix = nil)
  return inheritable_setting.namespace_inheritable[:root_prefix] if prefix.nil?

  inheritable_setting.namespace_inheritable[:root_prefix] = prefix.to_s
end

#route(methods, paths = ['/'], route_options = {}) ⇒ 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.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/grape/dsl/routing.rb', line 160

def route(methods, paths = ['/'], route_options = {}, &)
  method = methods == :any ? '*' : methods
  endpoint_params = inheritable_setting.namespace_stackable_with_hash(:params) || {}
  endpoint_description = inheritable_setting.route[:description]
  all_route_options = { params: endpoint_params }
  all_route_options.deep_merge!(endpoint_description) if endpoint_description
  all_route_options.deep_merge!(route_options) if route_options&.any?

  new_endpoint = Grape::Endpoint.new(
    inheritable_setting,
    method: method,
    path: paths,
    for: self,
    route_options: all_route_options,
    &
  )
  endpoints << new_endpoint unless endpoints.any? { |e| e.equals?(new_endpoint) }

  inheritable_setting.route_end
  reset_validations!
end

#route_param(param, requirements: nil, type: nil, **options) ⇒ Object

This method allows you to quickly define a parameter route segment in your API.

Parameters:

  • param (Symbol)

    The name of the parameter you wish to declare.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • You (Regexp)

    may supply a regular expression that the declared parameter must meet.



226
227
228
229
230
231
232
233
234
# File 'lib/grape/dsl/routing.rb', line 226

def route_param(param, requirements: nil, type: nil, **options, &)
  requirements = { param.to_sym => requirements } if requirements.is_a?(Regexp)

  Grape::Validations::ParamsScope.new(api: self) do
    requires param, type: type
  end if type

  namespace(":#{param}", requirements: requirements, **options, &)
end

#routesObject

An array of API routes.



217
218
219
# File 'lib/grape/dsl/routing.rb', line 217

def routes
  @routes ||= endpoints.map(&:routes).flatten
end

#scope(_name = nil, &block) ⇒ Object

Create a scope without affecting the URL.

make the code more readable.

Parameters:

  • _name (Symbol) (defaults to: nil)

    Purely placebo, just allows to name the scope to



77
78
79
80
81
# File 'lib/grape/dsl/routing.rb', line 77

def scope(_name = nil, &block)
  within_namespace do
    nest(block)
  end
end

#version(*args, **options, &block) ⇒ Object

Specify an API version.

Examples:

API with legacy support.

class MyAPI < Grape::API
  version 'v2'

  get '/main' do
    {some: 'data'}
  end

  version 'v1' do
    get '/main' do
      {legacy: 'data'}
    end
  end
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/grape/dsl/routing.rb', line 41

def version(*args, **options, &block)
  if args.any?
    options = options.reverse_merge(using: :path)
    requested_versions = args.flatten.map(&:to_s)

    raise Grape::Exceptions::MissingVendorOption.new if options[:using] == :header && !options.key?(:vendor)

    @versions = versions | requested_versions

    if block
      within_namespace do
        inheritable_setting.namespace_inheritable[:version] = requested_versions
        inheritable_setting.namespace_inheritable[:version_options] = options

        instance_eval(&block)
      end
    else
      inheritable_setting.namespace_inheritable[:version] = requested_versions
      inheritable_setting.namespace_inheritable[:version_options] = options
    end
  end

  @versions.last if instance_variable_defined?(:@versions) && @versions
end

#versionsObject

Returns array of defined versions.

Returns:

  • array of defined versions



237
238
239
# File 'lib/grape/dsl/routing.rb', line 237

def versions
  @versions ||= []
end