Class: HttpClientGenerator::Plugs::ValidateResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client_generator/plugs/validate_response.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema_helper:) ⇒ ValidateResponse

Returns a new instance of ValidateResponse.



8
9
10
# File 'lib/http_client_generator/plugs/validate_response.rb', line 8

def initialize(schema_helper:)
  @schema_helper = schema_helper
end

Instance Method Details

#call(req) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/http_client_generator/plugs/validate_response.rb', line 12

def call(req)
  schema_name = :"#{req.verb}_#{req.name}"

  return req unless @schema_helper.respond_to?(schema_name)

  req.response_body = @schema_helper.public_send(schema_name, req.response_body)
    .value_or { |e| req.raise_message("Unexpected #{e.inspect} in #{req.response_body}") }

  req
end