Class: Preneeds::Middleware::Response::CleanResponse

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/preneeds/middleware/response/clean_response.rb

Overview

Faraday response middleware that cleans up the response body from EOAS that can have some extra content before the relevant XML body.

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Faraday::Env

Ensures expected formatting of EOAS response.

Returns:

  • (Faraday::Env)


14
15
16
17
18
19
# File 'lib/preneeds/middleware/response/clean_response.rb', line 14

def on_complete(env)
  return unless env.url.to_s == Preneeds::Configuration.url

  relevant_xml = env.body&.gsub(/[\t\n]/, ' ')&.scan(%r{<S:Envelope[^<>]*>.*</S:Envelope[^<>]*>}i)&.first
  env.body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>#{relevant_xml}" if relevant_xml.present?
end