Sha256: 7b6becafa05b6aace704a9fca80fcc68862372a8d85ddefa6e70f86a575d5114

Contents?: true

Size: 1.43 KB

Versions: 48

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

require 'json'

module Faraday
  class Response
    # Parse response bodies as JSON.
    class Json < Middleware
      def initialize(app = nil, parser_options: nil, content_type: /\bjson$/, preserve_raw: false)
        super(app)
        @parser_options = parser_options
        @content_types = Array(content_type)
        @preserve_raw = preserve_raw
      end

      def on_complete(env)
        process_response(env) if parse_response?(env)
      end

      private

      def process_response(env)
        env[:raw_body] = env[:body] if @preserve_raw
        env[:body] = parse(env[:body])
      rescue StandardError, SyntaxError => e
        raise Faraday::ParsingError.new(e, env[:response])
      end

      def parse(body)
        ::JSON.parse(body, @parser_options || {}) unless body.strip.empty?
      end

      def parse_response?(env)
        process_response_type?(env) &&
          env[:body].respond_to?(:to_str)
      end

      def process_response_type?(env)
        type = response_type(env)
        @content_types.empty? || @content_types.any? do |pattern|
          pattern.is_a?(Regexp) ? type.match?(pattern) : type == pattern
        end
      end

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

Faraday::Response.register_middleware(json: Faraday::Response::Json)

Version data entries

48 entries across 48 versions & 5 rubygems

Version Path
moneykit-0.1.15 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.14 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.13 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.12 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.11 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.10 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.9 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.6 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.5 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.4 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.3 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.2 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.1 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.0 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.0.alpha.2 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
moneykit-0.1.0.alpha.1 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/lib/faraday/response/json.rb
faraday-2.7.12 lib/faraday/response/json.rb
faraday-2.7.11 lib/faraday/response/json.rb
faraday-2.7.10 lib/faraday/response/json.rb
faraday-2.7.9 lib/faraday/response/json.rb