Class: Restspec::Endpoints::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/restspec/endpoints/dsl.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#namespace(name, base_path: nil, &block) ⇒ Object

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

Examples:

with only a name

namespace :books do
end

with a base_path

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

Parameters:

  • name (String)

    the name of the namespace.

  • base_path (String, nil) (defaults to: nil)

    the base_path property of the namespace.

  • block (:call)

    a block to yield to a newly created NamespaceDSL.



24
25
26
27
28
29
# 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) ⇒ Object

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.

Examples:

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

Parameters:

  • name (String)

    the name of the namespace.

  • base_path (String, nil) (defaults to: nil)

    the base_path property of the namespace.

  • block (:call)

    a block to yield to a newly created NamespaceDSL.



49
50
51
52
53
54
55
56
# 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