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



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

def endpoints
  @endpoints
end

#route_setObject (readonly)

Returns the value of attribute route_set



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

def route_set
  @route_set
end

#routesObject (readonly)

An array of API routes.



150
151
152
# File 'lib/grape/dsl/routing.rb', line 150

def routes
  @routes
end

Instance Method Details

#do_not_route_head!Object

Do not route HEAD requests to GET requests automatically



53
54
55
# File 'lib/grape/dsl/routing.rb', line 53

def do_not_route_head!
  namespace_inheritable(:do_not_route_head, true)
end

#do_not_route_options!Object

Do not automatically route OPTIONS



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

def do_not_route_options!
  namespace_inheritable(:do_not_route_options, true)
end

#mount(mounts) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/grape/dsl/routing.rb', line 62

def mount(mounts)
  mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
  mounts.each_pair do |app, path|
    in_setting = inheritable_setting

    if app.respond_to?(:inheritable_setting, true)
      mount_path = Rack::Mount::Utils.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.regenerate_endpoints(in_setting)

      app.change!
      change!
    end

    endpoints << Grape::Endpoint.new(
        in_setting,
        method: :any,
        path: path,
        app: app,
        for: self
    )
  end
end

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



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

def namespace(space = nil, options = {},  &block)
  if space || block_given?
    within_namespace do
      previous_namespace_description = @namespace_description
      @namespace_description = (@namespace_description || {}).deep_merge(namespace_setting(:description) || {})
      nest(block) do
        if space
          namespace_stackable(:namespace, Namespace.new(space, options))
        end
      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.



48
49
50
# File 'lib/grape/dsl/routing.rb', line 48

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

#reset_routes!Object



154
155
156
# File 'lib/grape/dsl/routing.rb', line 154

def 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.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/grape/dsl/routing.rb', line 103

def route(methods, paths = ['/'], route_options = {}, &block)
  endpoint_options = {
    method: methods,
    path: paths,
    for: self,
    route_options: ({
      params: Grape::DSL::Configuration.stacked_hash_to_hash(namespace_stackable(:params)) || {}
    }).deep_merge(route_setting(:description) || {}).deep_merge(route_options || {})
  }

  endpoints << Grape::Endpoint.new(inheritable_setting, endpoint_options, &block)

  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.



163
164
165
166
167
# File 'lib/grape/dsl/routing.rb', line 163

def route_param(param, options = {}, &block)
  options = options.dup
  options[:requirements] = { param.to_sym => options[:requirements] } if options[:requirements].is_a?(Regexp)
  namespace(":#{param}", options, &block)
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


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/grape/dsl/routing.rb', line 29

def version(*args, &block)
  if args.any?
    options = args.pop if args.last.is_a? Hash
    options ||= {}
    options = { using: :path }.merge(options)

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

    @versions = versions | args
    nest(block) do
      namespace_inheritable(:version, args)
      namespace_inheritable(:version_options, options)
    end
  end

  @versions.last unless @versions.nil?
end

#versionsObject



169
170
171
# File 'lib/grape/dsl/routing.rb', line 169

def versions
  @versions ||= []
end