Module: Evil::Client::DSL

Included in:
Evil::Client
Defined in:
lib/evil/client/dsl.rb,
lib/evil/client/dsl/files.rb,
lib/evil/client/dsl/scope.rb,
lib/evil/client/dsl/security.rb,
lib/evil/client/dsl/operation.rb,
lib/evil/client/dsl/operations.rb

Overview

Defines a DSL to customize class-level settings of the specific client

Defined Under Namespace

Classes: Files, Operation, Operations, Scope, Security

Constant Summary collapse

DEFAULT_MIDDLEWARE =

Stack of default middleware before custom midleware and a connection This stack cannot be modified

Middleware.new do
  run Middleware::MergeSecurity
  run Middleware::StringifyJson
  run Middleware::StringifyQuery
  run Middleware::NormalizeHeaders
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object

Adds [#operations] to a specific client’s instances



17
18
19
# File 'lib/evil/client/dsl.rb', line 17

def self.extended(klass)
  klass.include Dry::Initializer.define -> { param :operations }
end

Instance Method Details

#base_url(&block) ⇒ self

Helper to define base url of the server

Parameters:

  • value (#to_s)

Returns:

  • (self)


43
44
45
46
47
# File 'lib/evil/client/dsl.rb', line 43

def base_url(&block)
  return self unless block
  schema[:base_url] = block
  self
end

#connection(type = nil, &block) ⇒ self

Helper specify a connection to be used by a client

Parameters:

  • type (#to_sym) (defaults to: nil)

    (nil) The specific type of connection. Uses NetHTTP by default.

Returns:

  • (self)


55
56
57
58
59
# File 'lib/evil/client/dsl.rb', line 55

def connection(type = nil, &block)
  schema[:connection] = Connection[type]
  schema[:middleware] = Middleware.new(&block)
  self
end

#new(*args) ⇒ Hash<Symbol, Object>

Takes constructor arguments and builds a final schema for an instance (All the instantiation magics goes here)

Parameters:

  • *args (Object)

Returns:

  • (Hash<Symbol, Object>)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/evil/client/dsl.rb', line 92

def new(*args)
  settings   = schema[:settings].new(*args)
  base_url   = schema[:base_url].call(settings)
  middleware = schema[:middleware].finalize(settings)
  operations = schema[:operations].finalize(settings)
  client     = schema[:connection].new URI(base_url)
  connection = Middleware.prepend.(middleware.(Middleware.append.(client)))

  data = operations.each_with_object({}) do |(key, schema), hash|
    hash[key] = Evil::Client::Operation.new schema, connection
  end

  super(data)
end

#operation(name = nil, &block) ⇒ self

Helper to declare operation, either default or specific

Parameters:

  • name (#to_sym) (defaults to: nil)

    (nil)

  • block (Proc)

Returns:

  • (self)


67
68
69
70
# File 'lib/evil/client/dsl.rb', line 67

def operation(name = nil, &block)
  schema[:operations].register(name, &block)
  self
end

#scope(name = :[], &block) ⇒ self

Helper to define scopes of the client’s top-level DSL

Parameters:

  • name (#to_sym) (defaults to: :[])

    (:[])

  • block (Proc)

Returns:

  • (self)


78
79
80
81
82
83
84
# File 'lib/evil/client/dsl.rb', line 78

def scope(name = :[], &block)
  klass = Class.new(Scope, &block)
  define_method(name) do |*args, **options|
    klass.new(*args, __scope__: self, **options)
  end
  self
end

#settings(&block) ⇒ self

Helper to define params and options a for a client’s constructor

Examples:

class MyClient < Evil::Client
end

MyClient.new "https://foo.com", user: "bar", token: "baz"

Parameters:

  • block (Proc)

Returns:

  • (self)


32
33
34
35
36
# File 'lib/evil/client/dsl.rb', line 32

def settings(&block)
  return self unless block
  schema[:settings] = Class.new { include Dry::Initializer.define(&block) }
  self
end