class Serf::Serfer

Class to drive the Interactor execution.

Attributes

interactor[R]
parcel_factory[R]

Public Class Methods

new(interactor, *args) click to toggle source
# File lib/serf/serfer.rb, line 14
def initialize(interactor, *args)
  opts = Optser.extract_options! args

  # How to and when to handle requests
  @interactor = interactor

  # Tunable knobs
  @parcel_factory = opts.get(:parcel_factory) { Serf::ParcelFactory.new }
end

Public Instance Methods

call(parcel) click to toggle source

Rack-like call to run the Interactor’s use-case.

# File lib/serf/serfer.rb, line 27
def call(parcel)
  # 1. Execute interactor
  response_kind, response_message, response_headers = interactor.call parcel

  # 2. Extract a possible version embedded in the response_kind.
  #   This is sugar syntax for kind and version.
  if response_kind
    kind_part, version_part = response_kind.split '#', 2
    response_kind = kind_part if version_part
    if version_part
      response_headers ||= {}
      response_headers[:version] = version_part
    end
  end

  # 3. Return a new response parcel with:
  #   a. uuids set from parent parcel
  #   b. kind set to response kind
  #   c. the message set to response_message
  #   d. add extra headers to the parcel
  return parcel_factory.create(
    parent: parcel,
    kind: response_kind,
    message: response_message,
    headers: response_headers)
end