Class: Alchemrest::Response::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/alchemrest/response/pipeline.rb,
lib/alchemrest/response/pipeline/omit.rb,
lib/alchemrest/response/pipeline/final.rb,
lib/alchemrest/response/pipeline/sanitize.rb,
lib/alchemrest/response/pipeline/transform.rb,
lib/alchemrest/response/pipeline/was_successful.rb,
lib/alchemrest/response/pipeline/extract_payload.rb

Overview

The ‘Alchemrest::Result::Pipeline` is a class that orchestrates running a sequence of `Morpher::Transform` instances starting with an `Alchemrest::Response` instance and ending with an `Alchemrest::Result`. The pipeline is responsible for feeding the output of one transform into the input of the next when a transform succeeds, and wrapping errors in an `Alchemrest::Result::Error` when a transform fails. Transform steps must meet the following criteria

  1. Implement a call method

  2. Return a ‘Either::Right` prelude if the transform is successful

  3. Return a ‘Either::Left` prelude that wraps either a `Morpher::Transform::Error` or one of our `Alchemrest.rescuable_exceptions if the transform is unsuccessful

Defined Under Namespace

Classes: ExtractPayload, Final, Omit, Sanitize, Transform, WasSuccessful

Instance Method Summary collapse

Constructor Details

#initialize(*steps) ⇒ Pipeline

Returns a new instance of Pipeline.



18
19
20
# File 'lib/alchemrest/response/pipeline.rb', line 18

def initialize(*steps)
  super(steps)
end

Instance Method Details

#append(step) ⇒ Object



41
42
43
# File 'lib/alchemrest/response/pipeline.rb', line 41

def append(step)
  self.class.new(*steps.append(step))
end

#call(response) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/alchemrest/response/pipeline.rb', line 22

def call(response)
  final = steps.reduce(response) do |current, step|
    either = step.call(current)
    return build_error_result(either.from_left) if either.left?
    break either.from_right.value if either.from_right.instance_of?(Final)

    either.from_right
  end

  Result::Ok(final)
end

#insert_after(step_class, step) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
# File 'lib/alchemrest/response/pipeline.rb', line 34

def insert_after(step_class, step)
  index = steps.index { |s| s.instance_of?(step_class) }
  raise ArgumentError, "Step #{step_class} not found" if index.nil?

  self.class.new(*steps.insert(index + 1, step))
end

#replace_with(step_class, step) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
# File 'lib/alchemrest/response/pipeline.rb', line 45

def replace_with(step_class, step)
  index = steps.index { |s| s.instance_of?(step_class) }
  raise ArgumentError, "Step #{step_class} not found" if index.nil?

  new_steps = steps.dup
  new_steps[index] = step

  self.class.new(*new_steps)
end