Class: Grape::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/namespace.rb

Overview

A container for endpoints or other namespaces, which allows for both logical grouping of endpoints as well as sharing commonconfiguration. May also be referred to as group, segment, or resource.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space, **options) ⇒ Namespace

Returns a new instance of Namespace

Parameters:

  • space (String)

    the name of this namespace

  • options (Hash)

    options hash

Options Hash (**options):

  • :requirements (Hash)

    param-regex pairs, all of which must be met by a request's params for all endpoints in this namespace, or validation will fail and return a 422.



13
14
15
16
# File 'lib/grape/namespace.rb', line 13

def initialize(space, **options)
  @space = space.to_s
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



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

def options
  @options
end

#spaceObject (readonly)

Returns the value of attribute space



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

def space
  @space
end

Class Method Details

.joined_space(settings) ⇒ Object



25
26
27
# File 'lib/grape/namespace.rb', line 25

def self.joined_space(settings)
  (settings || []).map(&:space).join('/')
end

.joined_space_path(settings) ⇒ Object

Join the namespaces from a list of settings to create a path prefix.

Parameters:

  • settings (Array)

    list of Grape::Util::InheritableSettings.



31
32
33
# File 'lib/grape/namespace.rb', line 31

def self.joined_space_path(settings)
  Grape::Router.normalize_path(joined_space(settings))
end

Instance Method Details

#requirementsHash

Retrieves the requirements from the options hash, if given.

Returns:

  • (Hash)


20
21
22
# File 'lib/grape/namespace.rb', line 20

def requirements
  options[:requirements] || {}
end