Class: Restspec::Endpoints::EndpointDSL

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

Overview

The Endpoint DSL is what should be used inside an endpoint block. Its major responsability is to define endpoint behavior.

Defined Under Namespace

Classes: ExampleOrValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint

Returns:

  • (Object)

    the current value of endpoint



243
244
245
# File 'lib/restspec/endpoints/dsl.rb', line 243

def endpoint
  @endpoint
end

Instance Method Details

#headersObject

A mutable hash containing the headers for the endpoint

Examples:

endpoint :monkeys do
  headers['Content-Type'] = 'application/json'
end


302
303
304
# File 'lib/restspec/endpoints/dsl.rb', line 302

def headers
  endpoint.headers
end

#method(method) ⇒ Object

Defines what the HTTP method will be.

Examples:

endpoint :show do
  method :get
end

Parameters:

  • method (Symbol)

    the http method name



252
253
254
# File 'lib/restspec/endpoints/dsl.rb', line 252

def method(method)
  endpoint.method = method
end

#no_schemaObject

Remove the schema of the current endpoint. Some endpoints doesn't require schemas for payload or for responses. This is the typical case for DELETE requests.

Examples:

resource :game do
  delete(:destroy) { no_schema }
end


292
293
294
# File 'lib/restspec/endpoints/dsl.rb', line 292

def no_schema
  endpoint.remove_schemas
end

#path(path) ⇒ Object

Defines what the path will be. It will be append to the namespace's base_path and to the Restspec config's base_url.

Examples:

endpoint :monkeys do
  path '/monkeys'
end

Parameters:

  • path (String)

    the path of the endpoint



266
267
268
# File 'lib/restspec/endpoints/dsl.rb', line 266

def path(path)
  endpoint.path = path
end

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

Defines what the schema will be.

Examples:

endpoint :monkeys do
  schema :monkey
end

Parameters:

  • name (Symbol)

    The schema's name.

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

    A set of options to pass to HasSchemas#add_schema.



279
280
281
# File 'lib/restspec/endpoints/dsl.rb', line 279

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

#url_param(param, &value_or_type_block) ⇒ Object

This set url parameters for the endpoint. It receives a key and a block returning the value. If the value is a type, an example will be generated.

Examples:

with just a value:

endpoint :monkeys, path: '/:id' do
  url_param(:id) { '1' }
end # /1

with a type:

endpoint :monkeys, path: '/:id' do
  url_param(:id) { string } # a random string
end # /{the random string}

Parameters:

  • param (Symbol)

    the parameter name.

  • value_or_type_block (block)

    the block returning the value.



321
322
323
324
325
# File 'lib/restspec/endpoints/dsl.rb', line 321

def url_param(param, &value_or_type_block)
  endpoint.add_url_param_block(param) do
    ExampleOrValue.new(endpoint, param, value_or_type_block).value
  end
end