Sha256: c98c9e99f0b01d1ed9ff2a7c7ff9fdcf14c327154d279f5610ad0fa8f4b81eb8

Contents?: true

Size: 709 Bytes

Versions: 66

Compression:

Stored size: 709 Bytes

Contents

module JsonApiClient
  module Middleware
    class ParseJson < Faraday::Middleware

      def call(environment)
        @app.call(environment).on_complete do |env|
          if process_response_type?(response_type(env))
            env[:raw_body] = env[:body]
            env[:body] = parse(env[:body])
          end
        end
      end

      private

      def parse(body)
        ::JSON.parse(body) unless body.strip.empty?
      end

      def response_type(env)
        type = env[:response_headers]['Content-Type'].to_s
        type = type.split(';', 2).first if type.index(';')
        type
      end

      def process_response_type?(type)
        !!type.match(/\bjson$/)
      end
    end
  end
end

Version data entries

66 entries across 66 versions & 2 rubygems

Version Path
json_api_client-1.22.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.21.1 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.21.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.20.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.19.0 lib/json_api_client/middleware/parse_json.rb
carwow-json_api_client-1.19.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.18.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.17.1 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.17.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.16.1 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.16.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.15.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.14.1 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.14.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.13.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.12.2 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.12.1 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.12.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.11.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-1.10.0 lib/json_api_client/middleware/parse_json.rb