Class: Serf::Serfer

Inherits:
Object
  • Object
show all
Defined in:
lib/serf/serfer.rb

Overview

Class to drive the Interactor execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interactor, *args) ⇒ Serfer

Returns a new instance of Serfer.



14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#interactorObject (readonly)

Returns the value of attribute interactor.



11
12
13
# File 'lib/serf/serfer.rb', line 11

def interactor
  @interactor
end

#parcel_factoryObject (readonly)

Returns the value of attribute parcel_factory.



12
13
14
# File 'lib/serf/serfer.rb', line 12

def parcel_factory
  @parcel_factory
end

Instance Method Details

#call(parcel) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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