Module: Evil::Client::DSL
- Included in:
- Evil::Client
- Defined in:
- lib/evil/client/dsl.rb,
lib/evil/client/dsl/base.rb,
lib/evil/client/dsl/path.rb,
lib/evil/client/dsl/files.rb,
lib/evil/client/dsl/query.rb,
lib/evil/client/dsl/scope.rb,
lib/evil/client/dsl/headers.rb,
lib/evil/client/dsl/response.rb,
lib/evil/client/dsl/security.rb,
lib/evil/client/dsl/verifier.rb,
lib/evil/client/dsl/operation.rb,
lib/evil/client/dsl/operations.rb,
lib/evil/client/dsl/http_method.rb
Overview
Defines a DSL to customize class-level settings of the specific client
Defined Under Namespace
Classes: Base, Files, Headers, HttpMethod, Operation, Operations, Path, Query, Response, Responses, Scope, Security, Verifier
Constant Summary collapse
- SchemaError =
Class.new(StandardError)
Class Method Summary collapse
-
.extended(klass) ⇒ Object
Adds [#operations] to a specific client’s instances.
Instance Method Summary collapse
-
#base_url(&block) ⇒ self
Helper to define base url of the server.
-
#connection(type = nil, &block) ⇒ self
Helper specify a connection to be used by a client.
-
#new(*args) ⇒ Hash<Symbol, Object>
Takes constructor arguments and builds a final schema for an instance (All the instantiation magics goes here).
-
#operation(name = nil, &block) ⇒ self
Helper to declare operation, either default or specific.
-
#scope(name = :[], &block) ⇒ self
Helper to define scopes of the client’s top-level DSL.
-
#settings(&block) ⇒ self
Helper to define params and options a for a client’s constructor.
Class Method Details
.extended(klass) ⇒ Object
Adds [#operations] to a specific client’s instances
19 20 21 22 23 24 25 |
# File 'lib/evil/client/dsl.rb', line 19 def self.extended(klass) klass.include Dry::Initializer.define -> do param :settings param :base_url param :operations end end |
Instance Method Details
#base_url(&block) ⇒ self
Helper to define base url of the server
49 50 51 52 53 |
# File 'lib/evil/client/dsl.rb', line 49 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
61 62 63 64 65 |
# File 'lib/evil/client/dsl.rb', line 61 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)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/evil/client/dsl.rb', line 98 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(settings, base_url, data) end |
#operation(name = nil, &block) ⇒ self
Helper to declare operation, either default or specific
73 74 75 76 |
# File 'lib/evil/client/dsl.rb', line 73 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
84 85 86 87 88 89 90 |
# File 'lib/evil/client/dsl.rb', line 84 def scope(name = :[], &block) klass = Class.new(Scope, &block) define_method(name) do |*args, **| klass.new(*args, __scope__: self, **) end self end |
#settings(&block) ⇒ self
Helper to define params and options a for a client’s constructor
38 39 40 41 42 |
# File 'lib/evil/client/dsl.rb', line 38 def settings(&block) return self unless block schema[:settings] = Class.new { include Dry::Initializer.define(&block) } self end |