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-0.4.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-0.3.1 lib/json_api_client/middleware/parse_json.rb
json_api_client-0.3.0 lib/json_api_client/middleware/parse_json.rb
json_api_client-0.2.4 lib/json_api_client/middleware/parse_json.rb
json_api_client-0.2.3 lib/json_api_client/middleware/parse_json.rb
json_api_client-0.2.2 lib/json_api_client/middleware/parse_json.rb