Class: Rivulet::Steps::CompileResponse

Inherits:
Rivulet::Step show all
Defined in:
lib/rivulet/steps/compile_response.rb

Instance Method Summary collapse

Methods inherited from Rivulet::Step

container_class_path, inherited

Instance Method Details

#call(input) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rivulet/steps/compile_response.rb', line 4

def call(input)
  input => { resource:, response: }

  status = response.status
  format = response.format

  body, headers =
    case format
    when :json
      payload = Oj.dump(response.body, mode: :json)

      [
        Protocol::HTTP::Body::Buffered.wrap(payload),
        {
          'Content-Type'   => 'application/json',
          'Content-Length' => payload.bytesize.to_s
        }
      ]
    when :text
      payload = response.body.to_s

      [
        Protocol::HTTP::Body::Buffered.wrap(payload),
        {
          'Content-Type'   => 'text/plain; charset=utf-8',
          'Content-Length' => payload.bytesize.to_s
        }
      ]
    when :file
      result = build_file_body(response.body, resource)
      return result if result in Failure
      result
    when :stream
      [Protocol::HTTP::Body::Stream.new(response.body), {}]
    when :as_is
      [response.body, {}]
    else
      [[], {}]
    end

  headers.merge!(response.headers)

  input[:response] = [status, headers, body]

  Success(input)
end