Class: Rivulet::Steps::ValidateResponse

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

Constant Summary collapse

NO_BODY_STATUSES =
[204, 304].freeze
FORMAT_VALUES =
%i[json text file stream as_is].freeze

Instance Method Summary collapse

Methods inherited from Rivulet::Step

container_class_path, inherited

Instance Method Details

#call(input) ⇒ Object



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
# File 'lib/rivulet/steps/validate_response.rb', line 7

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

  unless response.is_a? Rivulet::Response
    return Failure[:wrong_response_type, "Invalid response type for #{route.path}"]
  end

  if NO_BODY_STATUSES.include?(response.status) && !response.body.nil?
    return Failure[:conflicting_response, "Status #{response.status} has body #{response.body}"]
  end

  unless FORMAT_VALUES.include?(response.format)
    return Failure[:wrong_response_format, "Unsupported response format #{response.format.inspect}"]
  end

  if response.format == :stream && !io_like?(response.body)
    return Failure[:wrong_response_type, 'Response body is not supported for stream format']
  end

  if response.format == :file
    body = response.body
    return Failure[:wrong_response_type, "File body requires :path key"] if body.is_a?(Hash) && !body.key?(:path)
    return Failure[:wrong_response_type, "Response body is not supported for file format"] unless body.is_a?(String) || body.is_a?(Hash)
  end

  Success(input)
end