Sha256: d015eb5db4430719cdfa1b058883dc25d3607953ba3aef77b84aeff15bc3eb57

Contents?: true

Size: 1.65 KB

Versions: 24

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require 'json'

module Faraday
  class Request
    # Request middleware that encodes the body as JSON.
    #
    # Processes only requests with matching Content-type or those without a type.
    # If a request doesn't have a type but has a body, it sets the Content-type
    # to JSON MIME-type.
    #
    # Doesn't try to encode bodies that already are in string form.
    class Json < Middleware
      MIME_TYPE = 'application/json'
      MIME_TYPE_REGEX = %r{^application/(vnd\..+\+)?json$}.freeze

      def on_request(env)
        match_content_type(env) do |data|
          env[:body] = encode(data)
        end
      end

      private

      def encode(data)
        ::JSON.generate(data)
      end

      def match_content_type(env)
        return unless process_request?(env)

        env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
        yield env[:body] unless env[:body].respond_to?(:to_str)
      end

      def process_request?(env)
        type = request_type(env)
        body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX))
      end

      def body?(env)
        body = env[:body]
        case body
        when true, false
          true
        when nil
          # NOTE: nil can be converted to `"null"`, but this middleware doesn't process `nil` for the compatibility.
          false
        else
          !(body.respond_to?(:to_str) && body.empty?)
        end
      end

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

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

Version data entries

24 entries across 24 versions & 2 rubygems

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