Sha256: 6373c306fc949508ad9764300477ba0f479799e91985bc96b51bf6fdf7696121

Contents?: true

Size: 1.18 KB

Versions: 11

Compression:

Stored size: 1.18 KB

Contents

module Grape
  # A container for endpoints or other namespaces, which allows for both
  # logical grouping of endpoints as well as sharing common configuration.
  # May also be referred to as group, segment, or resource.
  class Namespace
    attr_reader :space, :options

    # @param space [String] the name of this namespace
    # @param options [Hash] options hash
    # @option 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.
    def initialize(space, **options)
      @space = space.to_s
      @options = options
    end

    # Retrieves the requirements from the options hash, if given.
    # @return [Hash]
    def requirements
      options[:requirements] || {}
    end

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

    # Join the namespaces from a list of settings to create a path prefix.
    # @param settings [Array] list of Grape::Util::InheritableSettings.
    def self.joined_space_path(settings)
      Grape::Router.normalize_path(joined_space(settings))
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/grape-1.2.5/lib/grape/namespace.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/grape-1.2.5/lib/grape/namespace.rb
grape-1.2.5 lib/grape/namespace.rb
grape-1.2.4 lib/grape/namespace.rb
grape-1.2.3 lib/grape/namespace.rb
grape-1.2.2 lib/grape/namespace.rb
grape-1.2.1 lib/grape/namespace.rb
grape-1.2.0 lib/grape/namespace.rb
grape-1.1.0 lib/grape/namespace.rb
grape-1.0.3 lib/grape/namespace.rb
grape-1.0.2 lib/grape/namespace.rb