Class: BN::Middleware::HTTPResponse

Inherits:
Base
  • Object
show all
Defined in:
lib/bn/middleware/http_response.rb

Overview

Transforms the body of an HTTP request from JSON to Ruby.

Instance Method Summary collapse

Methods inherited from Base

execute, #initialize

Constructor Details

This class inherits a constructor from BN::Middleware::Base

Instance Method Details

#execute(response) ⇒ Hash

Execute the middleware.

Parameters:

  • response (#body)

Returns:

  • (Hash)

Raises:



14
15
16
17
18
19
20
21
# File 'lib/bn/middleware/http_response.rb', line 14

def execute(response)
  raise Error::Middleware::InvalidHTTPResponse, response: response unless response.respond_to?(:body)
  raise Error::Middleware::InvalidHTTPResponse, response: response unless response.respond_to?(:code) && (200...300).include?(response.code)

  body = response.body.to_s

  ::JSON.parse(body)
end