Class: NContent::SDK::FaradayMiddleware::ResponseParser

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/ncontent/sdk/faraday_middleware/response_parser.rb

Overview

Parses the responses to models:

Instance Method Summary collapse

Instance Method Details

#call(request_env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ncontent/sdk/faraday_middleware/response_parser.rb', line 9

def call(request_env)
  # do something with the request
  # request_env[:request_headers].merge!(...)

  @app.call(request_env).on_complete do |response_env|
    
    unless request_env.url.path =~ /\A\/oauth/i ||
           response_env.response_headers["Content-Type"] !~ /\Aapplication\/json/i ||
           response_env.body.strip.empty?

      benchmark = Benchmark.measure "api_response_parsing" do
        # replace body with parsed_data:
        response_env[:body] = ActiveSupport::JSON.decode(
          response_env[:body]
        )
      end

      response_env[:ncontent_api_response_parsing_tms] = benchmark

    end
  end
end