Module: Grape::DSL::Routing::ClassMethods

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


12
13
14
# File 'lib/grape/dsl/routing.rb', line 12

def endpoints
  @endpoints
end

Instance Method Details

#do_not_route_head!Object

Do not route HEAD requests to GET requests automatically.


73
74
75
# File 'lib/grape/dsl/routing.rb', line 73

def do_not_route_head!
  namespace_inheritable(:do_not_route_head, true)
end

#do_not_route_options!Object

Do not automatically route OPTIONS.


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

def do_not_route_options!
  namespace_inheritable(:do_not_route_options, true)
end

#mount(mounts, **opts) ⇒ Object


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/grape/dsl/routing.rb', line 82

def mount(mounts, **opts)
  mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
  mounts.each_pair do |app, path|
    if app.respond_to?(:mount_instance)
      mount(app.mount_instance(configuration: opts[:with] || {}) => path)
      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

    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

#namespace(space = 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, or group, resource, 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

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

def namespace(space = nil, options = {}, &block)
  @namespace_description = nil unless instance_variable_defined?(:@namespace_description) && @namespace_description

  if space || block_given?
    within_namespace do
      previous_namespace_description = @namespace_description
      @namespace_description = (@namespace_description || {}).deep_merge(namespace_setting(:description) || {})
      nest(block) do
        namespace_stackable(:namespace, Namespace.new(space, **options)) if space
      end
      @namespace_description = previous_namespace_description
    end
  else
    Namespace.joined_space_path(namespace_stackable(:namespace))
  end
end

#prefix(prefix = nil) ⇒ Object

Define a root URL prefix for your entire API.


58
59
60
# File 'lib/grape/dsl/routing.rb', line 58

def prefix(prefix = nil)
  namespace_inheritable(:root_prefix, prefix)
end

#reset_endpoints!Object


198
199
200
# File 'lib/grape/dsl/routing.rb', line 198

def reset_endpoints!
  @endpoints = []
end

#reset_routes!Object

Remove all defined routes.


193
194
195
196
# File 'lib/grape/dsl/routing.rb', line 193

def reset_routes!
  endpoints.each(&:reset_routes!)
  @routes = nil
end

#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

#route_param(param, options = {}, &block) ⇒ Object

Thie 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) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • You (Regexp)

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


207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/grape/dsl/routing.rb', line 207

def route_param(param, options = {}, &block)
  options = options.dup

  options[:requirements] = {
    param.to_sym => options[:requirements]
  } if options[:requirements].is_a?(Regexp)

  Grape::Validations::ParamsScope.new(api: self) do
    requires param, type: options[:type]
  end if options.key?(:type)

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

#routesObject

An array of API routes.


188
189
190
# File 'lib/grape/dsl/routing.rb', line 188

def routes
  @routes ||= prepare_routes
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


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

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

#version(*args, &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

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/grape/dsl/routing.rb', line 31

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

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

    @versions = versions | requested_versions

    if block_given?
      within_namespace do
        namespace_inheritable(:version, requested_versions)
        namespace_inheritable(:version_options, options)

        instance_eval(&block)
      end
    else
      namespace_inheritable(:version, requested_versions)
      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


222
223
224
# File 'lib/grape/dsl/routing.rb', line 222

def versions
  @versions ||= []
end