Class: Evil::Client::Schema::Operation

Inherits:
Evil::Client::Schema show all
Defined in:
lib/evil/client/schema/operation.rb

Overview

Mutable container of operation definitions with DSL to configure both settings and parts of request/response.

Direct Known Subclasses

Scope

Instance Attribute Summary

Attributes inherited from Evil::Client::Schema

#client, #name, #parent

Instance Method Summary collapse

Methods inherited from Evil::Client::Schema

#let, #option, #settings, #to_s, #validate

Instance Method Details

#body(value = nil, &block) ⇒ self

Adds body definition to the schema

It is expected the body to correspond to [#format].

When a format is :json, the body should be convertable to json When a format is :text, the body should be stringified When a format is :form, the body should be a hash When a format is :multipart, the body can be object or array of objects

Unlike queries, previous body definitions aren’t inherited. The body defined for root scope can be fully reloaded at subscope/operation level without any merging.

Parameters:

  • value (Object) (defaults to: nil)
  • block (Proc)

Returns:

  • (self)


133
134
135
# File 'lib/evil/client/schema/operation.rb', line 133

def body(value = nil, &block)
  __define__(:body, value, block)
end

#definitionsHash<Symbol, [Proc, Hash<Integer, Proc>]>

Definitions for the current operation

Returns:

  • (Hash<Symbol, [Proc, Hash<Integer, Proc>]>)


19
20
21
# File 'lib/evil/client/schema/operation.rb', line 19

def definitions
  @definitions ||= { responses: {} }
end

#format(value = nil, &block) ⇒ self

Adds format for request body

Parameters:

  • value (:json, :form, :text, :multipart, nil) (defaults to: nil)

    (:json)

  • block (Proc)

Returns:

  • (self)


54
55
56
# File 'lib/evil/client/schema/operation.rb', line 54

def format(value = nil, &block)
  __define__(:format, value, block)
end

#headers(value = nil, &block) ⇒ self

Adds request headers definition to the schema

Headers should be hash of header-value pairs. Values should be either stringified values or array of stringified values.

Nested definition will be merged to the root one(s), so that you can add headers step-by-step from root of the client to its scopes and operations.

To reset previous settings you can either set all headers to nil, or assign nil to custom headers. All headers with empty values will be ignored.

Parameters:

  • value (Hash<#to_s, [#to_s, Array<#to_s>]>, nil) (defaults to: nil)
  • block (Proc)

Returns:

  • (self)


97
98
99
# File 'lib/evil/client/schema/operation.rb', line 97

def headers(value = nil, &block)
  __define__(:headers, value, block)
end

#http_method(value = nil, &block) ⇒ self

Adds http method definition to the schema

Parameters:

  • value (#to_s, nil) (defaults to: nil)

    Acceptable http_method

  • block (Proc)

Returns:

  • (self)

See Also:



44
45
46
# File 'lib/evil/client/schema/operation.rb', line 44

def http_method(value = nil, &block)
  __define__(:http_method, value, block)
end

#leaf?true

Tells that this is a schema for end route (operation)

Returns:

  • (true)


11
12
13
# File 'lib/evil/client/schema/operation.rb', line 11

def leaf?
  true
end

#middleware(value = nil, &block) ⇒ self

Adds list of middleware to the schema

New middleware are added to previously defined (by root). This means the operation-specific middleware will handle the request after a root-specific one, and will handle the response before a roog-specific middleware.

Values should be either a Rack middleware class, or array of Rack middleware classes.

Parameters:

  • value (Rack::Middleware, <Array<Rack::Middleware>>) (defaults to: nil)
  • block (Proc)

Returns:

  • (self)


151
152
153
# File 'lib/evil/client/schema/operation.rb', line 151

def middleware(value = nil, &block)
  __define__(:middleware, value, block)
end

#path(value = nil, &block) ⇒ self

Adds path definition to the schema

Root path should be a valid URL for HTTP(S) protocol

Parameters:

  • value (#to_s, nil) (defaults to: nil)
  • block (Proc)

Returns:

  • (self)


31
32
33
# File 'lib/evil/client/schema/operation.rb', line 31

def path(value = nil, &block)
  __define__(:path, value, block)
end

#query(value = nil, &block) ⇒ self

Adds query definition to the schema

Query should be a nested hash. Wnen subscope or operation reloads previously defined query, new definition are merged deeply to older one. You can populate a query step-by-step from client root to an operation.

Parameters:

  • value (Hash, nil) (defaults to: nil)
  • block (Proc)

Returns:

  • (self)


112
113
114
# File 'lib/evil/client/schema/operation.rb', line 112

def query(value = nil, &block)
  __define__(:query, value, block)
end

#response(*codes, &block) ⇒ self Also known as: responses

Adds response handler definition to the schema

Parameters:

  • codes (Integer, Array<Integer>)

    List of response codes

  • block (Proc)

Returns:

  • (self)


161
162
163
164
165
166
# File 'lib/evil/client/schema/operation.rb', line 161

def response(*codes, &block)
  codes.flatten.map(&:to_i).each do |code|
    definitions[:responses][code] = block || proc { |*response| response }
  end
  self
end

#security(value = nil, &block) ⇒ self

Adds security definition to the schema

The definition should be nested hash with a root keys :headers, or :query.

Inside the block we provide several helpers for standard authentication schemas, namely basic_auth, token_auth, and key_auth. Those are preferred ways to define a security schema:

Examples:

security { { headers: { "idempotent-token" => "foobar" } } }
security { token_auth token, prefix: "Bearer" }

Parameters:

  • (Hash<[:headers, :query], Hash>, nil)
  • block (Proc)

Returns:

  • (self)


76
77
78
# File 'lib/evil/client/schema/operation.rb', line 76

def security(value = nil, &block)
  __define__(:security, value, block)
end