Class: Restspec::Endpoints::NamespaceDSL

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

Overview

The Namespace DSL is what should be used inside a namespace or resource block. Its major responsability is to add endpoints to the dsl.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace) ⇒ NamespaceDSL

Returns a new instance of NamespaceDSL.



64
65
66
67
# File 'lib/restspec/endpoints/dsl.rb', line 64

def initialize(namespace)
  self.namespace = namespace
  self.endpoint_config_blocks = []
end

Instance Attribute Details

#endpoint_config_blocksObject

Returns the value of attribute endpoint_config_blocks.



62
63
64
# File 'lib/restspec/endpoints/dsl.rb', line 62

def endpoint_config_blocks
  @endpoint_config_blocks
end

#namespaceObject

Returns the value of attribute namespace.



62
63
64
# File 'lib/restspec/endpoints/dsl.rb', line 62

def namespace
  @namespace
end

Instance Method Details

#all(&endpoints_config) ⇒ Object

Defines a block that will be executed in every endpoint inside this namespace. It should only be one for namespace.

Examples:

resource :products, base_path: '/:country_id/products' do
  member do
    all do
      url_params(:country_id) { 'us' }
    end

    get :show # /us/products/:id
    put :update # /us/products/us/:id
  end
end

Parameters:

  • endpoints_config (block)

    block that will be called in the context of an EndpointDSL instance.



218
219
220
# File 'lib/restspec/endpoints/dsl.rb', line 218

def all(&endpoints_config)
  self.endpoint_config_blocks << endpoints_config
end

#collection(base_path: nil, &block) ⇒ Object

This defines an anonymous namespace with a base_path equals to the empty string that will affect all his internals endpoints with the base_path of the parent namespace. This should be used to group collection related endpoints.

Examples:

resource :books do
  collection do
    get :index # /books
    post :create # /books
  end
end

Parameters:

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

    override of the base_pat

  • block (block)

    block that will be called with a instance, typically for create endpoints inside.



178
179
180
181
182
183
184
# File 'lib/restspec/endpoints/dsl.rb', line 178

def collection(base_path: nil, &block)
  collection_namespace = namespace.add_anonymous_children_namespace
  collection_namespace.base_path = base_path
  collection_dsl = NamespaceDSL.new(collection_namespace)
  collection_dsl.endpoint_config_blocks += endpoint_config_blocks
  collection_dsl.instance_eval(&block)
end

#delete(name, path = '', &block) ⇒ Object

Defines an endpoint with a delete http method.

Examples:

namespace :books do
  delete :endpoint_name, '/path' # this endpoint will have the delete http method attached
end

Parameters:

  • name (String)

    the name of the endpoint.

  • path (String) (defaults to: '')

    the optional path of the endpoint.

  • block (block)

    the block to call with the EndpointDSL instance.



128
129
130
# File 'lib/restspec/endpoints/dsl.rb', line 128

def delete(name, path = '', &block)
  setup_endpoint_from_http_method(:delete, name, path, &block)
end

#endpoint(name, &block) ⇒ Object

Defines a new endpoint with a name and a block that has a context equals to a EndpointDSL instance and saves the endpoint into the namespace.

Examples:

namespace :books do
  endpoint :get do
  end
end

Parameters:

  • name (Symbol)

    the endpoint name.

  • block (block)

    the block to call within an EndpointDSL instance.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/restspec/endpoints/dsl.rb', line 81

def endpoint(name, &block)
  endpoint = Endpoint.new(name)
  endpoint_dsl = EndpointDSL.new(endpoint)
  namespace.add_endpoint(endpoint)

  endpoint_config_blocks.each do |config_block|
    endpoint_dsl.instance_eval(&config_block)
  end

  endpoint_dsl.instance_eval(&block)

  Restspec::EndpointStore.store(endpoint)
end

#get(name, path = '', &block) ⇒ Object

Defines an endpoint with a get http method.

Examples:

namespace :books do
  get :endpoint_name, '/path' # this endpoint will have the get http method attached
end

Parameters:

  • name (String)

    the name of the endpoint.

  • path (String) (defaults to: '')

    the optional path of the endpoint.

  • block (block)

    the block to call with the EndpointDSL instance.



108
109
110
# File 'lib/restspec/endpoints/dsl.rb', line 108

def get(name, path = '', &block)
  setup_endpoint_from_http_method(:get, name, path, &block)
end

#head(name, path = '', &block) ⇒ Object

Defines an endpoint with a head http method.

Examples:

namespace :books do
  head :endpoint_name, '/path' # this endpoint will have the head http method attached
end

Parameters:

  • name (String)

    the name of the endpoint.

  • path (String) (defaults to: '')

    the optional path of the endpoint.

  • block (block)

    the block to call with the EndpointDSL instance.



133
134
135
# File 'lib/restspec/endpoints/dsl.rb', line 133

def head(name, path = '', &block)
  setup_endpoint_from_http_method(:head, name, path, &block)
end

#member(base_path: nil, identifier_name: 'id', &block) ⇒ Object

This defines an anonymous namespace with a base_path equals to '/:id' that will affect all his internals endpoints with the base_path of the parent namespace. Esentially:

Examples:

resource :books do
  member do
    get :show # /books/:id
    patch :update # /books/:id
    get :chapters, '/chapters' # /books/:id/chapters
  end
end

Parameters:

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

    override of the base_pat

  • identifier_name (String, :id) (defaults to: 'id')

    override of the key :id

  • block (block)

    block that will be called with a instance, typically for create endpoints inside.



154
155
156
157
158
159
160
# File 'lib/restspec/endpoints/dsl.rb', line 154

def member(base_path: nil, identifier_name: 'id', &block)
  member_namespace = namespace.add_anonymous_children_namespace
  member_namespace.base_path = base_path || "/:#{identifier_name}"
  member_dsl = NamespaceDSL.new(member_namespace)
  member_dsl.endpoint_config_blocks += endpoint_config_blocks
  member_dsl.instance_eval(&block)
end

#patch(name, path = '', &block) ⇒ Object

Defines an endpoint with a patch http method.

Examples:

namespace :books do
  patch :endpoint_name, '/path' # this endpoint will have the patch http method attached
end

Parameters:

  • name (String)

    the name of the endpoint.

  • path (String) (defaults to: '')

    the optional path of the endpoint.

  • block (block)

    the block to call with the EndpointDSL instance.



123
124
125
# File 'lib/restspec/endpoints/dsl.rb', line 123

def patch(name, path = '', &block)
  setup_endpoint_from_http_method(:patch, name, path, &block)
end

#post(name, path = '', &block) ⇒ Object

Defines an endpoint with a post http method.

Examples:

namespace :books do
  post :endpoint_name, '/path' # this endpoint will have the post http method attached
end

Parameters:

  • name (String)

    the name of the endpoint.

  • path (String) (defaults to: '')

    the optional path of the endpoint.

  • block (block)

    the block to call with the EndpointDSL instance.



113
114
115
# File 'lib/restspec/endpoints/dsl.rb', line 113

def post(name, path = '', &block)
  setup_endpoint_from_http_method(:post, name, path, &block)
end

#put(name, path = '', &block) ⇒ Object

Defines an endpoint with a put http method.

Examples:

namespace :books do
  put :endpoint_name, '/path' # this endpoint will have the put http method attached
end

Parameters:

  • name (String)

    the name of the endpoint.

  • path (String) (defaults to: '')

    the optional path of the endpoint.

  • block (block)

    the block to call with the EndpointDSL instance.



118
119
120
# File 'lib/restspec/endpoints/dsl.rb', line 118

def put(name, path = '', &block)
  setup_endpoint_from_http_method(:put, name, path, &block)
end

#schema(name, options = {}) ⇒ Object

It attaches a schema to the namespace. This schema name will be used by the endpoints inside the namespace too.

Examples:

namespace :products do
  schema :product
end

Parameters:

  • name (Symbol)

    the schema name.

  • options (Hash) (defaults to: {})

    A set of options to pass to HasSchemas#add_schema.



196
197
198
199
# File 'lib/restspec/endpoints/dsl.rb', line 196

def schema(name, options = {})
  namespace.add_schema(name, options)
  all { schema(name, options) }
end

#url_param(param, &value_or_example_block) ⇒ Object

It calls #all with the EndpointDSL#url_param method. Please refer to the EndpointDSL#url_param method.



224
225
226
227
228
# File 'lib/restspec/endpoints/dsl.rb', line 224

def url_param(param, &value_or_example_block)
  all do
    url_param(param, &value_or_example_block)
  end
end