class Restspec::Endpoints::DSL

The Endpoints DSL is what should be used inside the `endpoints.rb` file. This class is related to the top-level namespace of the DSL.

Public Instance Methods

namespace(name, base_path: nil, &block) click to toggle source

Generates a new namespace that is just an entity that groups a couple of endpoints for whatever reason.

@example with only a name

namespace :books do
end

@example with a `base_path`

namespace :books, base_path: '/publications' do
end

@param name [String] the name of the namespace. @param base_path [String, nil] the base_path property of the namespace. @param block [:call] a block to yield to a newly created {NamespaceDSL}.

# File lib/restspec/endpoints/dsl.rb, line 24
def namespace(name, base_path: nil, &block)
  namespace = Namespace.create(name.to_s)
  namespace.base_path = base_path
  namespace_dsl = NamespaceDSL.new(namespace)
  namespace_dsl.instance_eval(&block)
end
resource(name, base_path: nil, &block) click to toggle source

This is actually a kind of namespace factory, that creates a namespace that have the next conventions:

- The name is a pluralization of another name. (products, books, etc)
- The base_path is, when not defined, the name of the namespace prepended with a "/"
- Attaches a schema with the name of the namespace singularized to the namespace.
  (product, book, etc)

In this way, it can express REST resources that groups a couple of endpoints related to them.

@example

resource :books do
  namespace.schema_for(:response).name # book
  namespace.base_path # /books
end

@param (see namespace)

# File lib/restspec/endpoints/dsl.rb, line 49
def resource(name, base_path: nil, &block)
  resource_name = name.to_s.singularize.to_sym

  namespace name, base_path: (base_path || "/#{name}") do
    schema resource_name
    instance_eval(&block)
  end
end