Skip to content

Plugins

Matt Muller edited this page Apr 10, 2024 · 7 revisions

Plugins are distributable classes that modify Config to add custom behavior.

Usage

Plugins are any class (or even a Proc) that responds to call(config). Plugins can be applied to the Client class or the instance of the Client.

Note: The NetworkInterceptor example is defined in Interceptors.

class NetworkPlugin
  def call(config)
    config.interceptors << NetworkInterceptor.new
  end
end
# => :call

# Add our plugin to a plugin list
plugins = Hearth::PluginList.new([NetworkPlugin.new])
# => #<Hearth::PluginList ... >

# Configure plugin onto the Client
client = HighScoreService::Client.new(
  endpoint: 'http://127.0.0.1:3000',
  plugins: plugins
)
# => #<HighScoreService::Client ... >

# Register a Client class plugin
HighScoreService::Client.plugins << SomeOtherPlugin.new
# => [ ... ]

client.list_high_scores
# log request headers: { ... }
# log response headers: { ... }
# log request time is: ...
# => #<Hearth::Output @data=... >

Plugin ordering

Plugins registered to the Client are called before plugins registered to the instance of that Client.

# These are applied (called) first, if any.
HighScoreService::Client.plugins
# => #<Hearth::PluginList ... >

# Then these are applied (called), if any.
client.config.plugins
# => #<Hearth::PluginList ... >

Plugins passed as options to operation calls are additive and do not override other plugins configured on the Client.

more_plugins = Hearth::PluginList.new([YetAnotherPlugin.new])
# => #<Hearth::PluginList ... >

client.list_high_scores({}, plugins: more_plugins)
# <YetAnotherPlugin is called just before invoking the operation>