Class: Restspec::Endpoints::NamespaceDSL
- Inherits:
-
Object
- Object
- Restspec::Endpoints::NamespaceDSL
- 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
-
#endpoint_config_blocks ⇒ Object
Returns the value of attribute endpoint_config_blocks.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
Instance Method Summary collapse
-
#all(&endpoints_config) ⇒ Object
Defines a block that will be executed in every endpoint inside this namespace.
-
#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.
-
#delete(name, path = '', &block) ⇒ Object
Defines an endpoint with a delete http method.
-
#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.
-
#get(name, path = '', &block) ⇒ Object
Defines an endpoint with a get http method.
-
#head(name, path = '', &block) ⇒ Object
Defines an endpoint with a head http method.
-
#initialize(namespace) ⇒ NamespaceDSL
constructor
A new instance of NamespaceDSL.
-
#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.
-
#patch(name, path = '', &block) ⇒ Object
Defines an endpoint with a patch http method.
-
#post(name, path = '', &block) ⇒ Object
Defines an endpoint with a post http method.
-
#put(name, path = '', &block) ⇒ Object
Defines an endpoint with a put http method.
-
#schema(name, options = {}) ⇒ Object
It attaches a schema to the namespace.
-
#url_param(param, &value_or_example_block) ⇒ Object
It calls #all with the EndpointDSL#url_param method.
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_blocks ⇒ Object
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 |
#namespace ⇒ Object
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.
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.
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.
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.
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.
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.
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:
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.
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.
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.
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.
196 197 198 199 |
# File 'lib/restspec/endpoints/dsl.rb', line 196 def schema(name, = {}) namespace.add_schema(name, ) all { schema(name, ) } 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 |