class Restspec::Endpoints::NamespaceDSL
The Namespace
DSL
is what should be used inside a namespace or resource block. Its major responsability is to add endpoints to the dsl.
Attributes
Public Class Methods
# File lib/restspec/endpoints/dsl.rb, line 64 def initialize(namespace) self.namespace = namespace self.endpoint_config_blocks = [] end
Public Instance Methods
Defines a block that will be executed in every endpoint inside this namespace. It should only be one for namespace.
@example
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
@param endpoints_config [block] block that will be called in the context of
an {EndpointDSL} instance.
# File lib/restspec/endpoints/dsl.rb, line 218 def all(&endpoints_config) self.endpoint_config_blocks << endpoints_config end
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.
@example
resource :books do collection do get :index # /books post :create # /books end end
@param base_path [String, nil] override of the base_pat @param block [block] block that will be called with a {NamespaceDSL instance},
typically for create endpoints inside.
# 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
@macro http_method_endpoint
# File lib/restspec/endpoints/dsl.rb, line 128 def delete(name, path = '', &block) setup_endpoint_from_http_method(:delete, name, path, &block) end
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.
@example
namespace :books do endpoint :get do end end
@param name [Symbol] the endpoint name. @param block [block] the block to call within an {EndpointDSL} instance.
# 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
@macro http_method_endpoint
# File lib/restspec/endpoints/dsl.rb, line 108 def get(name, path = '', &block) setup_endpoint_from_http_method(:get, name, path, &block) end
@macro http_method_endpoint
# File lib/restspec/endpoints/dsl.rb, line 133 def head(name, path = '', &block) setup_endpoint_from_http_method(:head, name, path, &block) end
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:
@example
resource :books do member do get :show # /books/:id patch :update # /books/:id get :chapters, '/chapters' # /books/:id/chapters end end
@param base_path [String, nil] override of the base_pat @param identifier_name [String, :id] override of the key :id @param block [block] block that will be called with a {NamespaceDSL instance},
typically for create endpoints inside.
# 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
@macro http_method_endpoint
# File lib/restspec/endpoints/dsl.rb, line 123 def patch(name, path = '', &block) setup_endpoint_from_http_method(:patch, name, path, &block) end
@macro http_method_endpoint
# File lib/restspec/endpoints/dsl.rb, line 113 def post(name, path = '', &block) setup_endpoint_from_http_method(:post, name, path, &block) end
@macro http_method_endpoint
# File lib/restspec/endpoints/dsl.rb, line 118 def put(name, path = '', &block) setup_endpoint_from_http_method(:put, name, path, &block) end
It attaches a schema to the namespace. This schema name will be used by the endpoints inside the namespace too.
@example
namespace :products do schema :product end
@param name [Symbol] the schema name. @param options [Hash] A set of options to pass to {Endpoint#add_schema}.
# File lib/restspec/endpoints/dsl.rb, line 196 def schema(name, options = {}) namespace.add_schema(name, options) all { schema(name, options) } end
It calls {#all} with the {EndpointDSL#url_param} method. Please refer to the {EndpointDSL#url_param} method.
# 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
Private Instance Methods
# File lib/restspec/endpoints/dsl.rb, line 232 def setup_endpoint_from_http_method(http_method, endpoint_name, path, &block) endpoint(endpoint_name) do self.method http_method self.path path instance_eval(&block) if block.present? end end