Class: Evil::Client::Operation::Response

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer::Mixin
Defined in:
lib/evil/client/operation/response.rb

Overview

Processes rack responses using an operation’s schema

Instance Method Summary collapse

Instance Method Details

#handle(response) ⇒ Object

Processes rack responses returned by [Dry::Cluent::Connection]

Parameters:

  • array (Array)

    Rack-compatible array of response data

Returns:

  • (Object)

Raises:

  • (Evil::Client::ResponseError)

    when needed by the schema

  • (Evil::Client::UnexpectedResponseError)

    when a response cannot be processed



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/evil/client/operation/response.rb', line 19

def handle(response)
  status, _, body = response
  body = body.any? ? body.join("\n") : nil

  handlers(status).each do |handler|
    data = handler[:coercer][body] rescue next
    raise ResponseError.new(schema, status, data) if handler[:raise]
    return data
  end

  raise UnexpectedResponseError.new(schema, status, body)
end