Base
Base class for adapters.
The engine of an API. Handles both introspection (generating types from representations) and runtime (processing requests through capabilities, serializing, and wrapping responses). The class declaration acts as a manifest.
Example
class MyAdapter < Apiwork::Adapter::Base
adapter_name :my
resource_serializer Serializer::Resource::Default
error_serializer Serializer::Error::Default
member_wrapper Wrapper::Member::Default
collection_wrapper Wrapper::Collection::Default
error_wrapper Wrapper::Error::Default
capability Capability::Filtering
capability Capability::Pagination
endClass Methods
.adapter_name
.adapter_name(value = nil)
The adapter name for this adapter.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
value | Symbol, String, nil | nil | The adapter name. |
Returns
Symbol, nil
Example
adapter_name :my.capability
.capability(klass)
Registers a capability for this adapter.
Capabilities are self-contained concerns (pagination, filtering, etc.) that handle both introspection and runtime behavior.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
klass | Class<Capability::Base> | The capability class. |
Returns
void
Example
capability Capability::Filtering
capability Capability::Pagination.collection_wrapper
.collection_wrapper(klass = nil)
Sets the wrapper class for collection responses.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
klass | Class<Wrapper::Collection::Base>, nil | nil | The wrapper class. |
Returns
Class<Wrapper::Collection::Base>, nil
Example
collection_wrapper Wrapper::Collection::Default.error_serializer
.error_serializer(klass = nil)
Sets the serializer class for errors.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
klass | Class<Serializer::Error::Base>, nil | nil | The serializer class. |
Returns
Class<Serializer::Error::Base>, nil
Example
error_serializer Serializer::Error::Default.error_wrapper
.error_wrapper(klass = nil)
Sets the wrapper class for error responses.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
klass | Class<Wrapper::Error::Base>, nil | nil | The wrapper class. |
Returns
Class<Wrapper::Error::Base>, nil
Example
error_wrapper Wrapper::Error::Default.member_wrapper
.member_wrapper(klass = nil)
Sets the wrapper class for single-record responses.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
klass | Class<Wrapper::Member::Base>, nil | nil | The wrapper class. |
Returns
Class<Wrapper::Member::Base>, nil
Example
member_wrapper Wrapper::Member::Default.option
.option(name, type:, default: nil, enum: nil, &block)
Defines a configuration option.
For nested options, use type: :hash with a block. Inside the block, call option to define child options.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
name | Symbol | The option name. | |
type | Symbol<:boolean, :hash, :integer, :string, :symbol> | The option type. | |
default | Object, nil | nil | The default value. |
enum | Array, nil | nil | The allowed values. |
Returns
void
See also
Example: Symbol option
option :locale, type: :symbol, default: :enExample: String option with enum
option :version, type: :string, default: '5', enum: %w[4 5]Example: Nested options
option :pagination, type: :hash do
option :strategy, type: :symbol, default: :offset, enum: %i[offset cursor]
option :default_size, type: :integer, default: 20
option :max_size, type: :integer, default: 100
end.resource_serializer
.resource_serializer(klass = nil)
Sets the serializer class for records and collections.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
klass | Class<Serializer::Resource::Base>, nil | nil | The serializer class. |
Returns
Class<Serializer::Resource::Base>, nil
Example
resource_serializer Serializer::Resource::Default.skip_capability
.skip_capability(name)
Skips an inherited capability by name.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
name | Symbol | The capability name to skip. |
Returns
void
Example
skip_capability :pagination