Sha256: 817c0b544315d925b6ce7f5abc24f472b209fbe5cbf5d6fdb511f89f4bf0179e

Contents?: true

Size: 903 Bytes

Versions: 1

Compression:

Stored size: 903 Bytes

Contents

require 'faraday'

module Saddle
  module Middleware
    module Request

      # Request middleware that encodes the body as JSON.
      #
      # Make sure you set request[:request_style] = :json
      # for it to be activated.

      class JsonEncoded < Faraday::Middleware
        CONTENT_TYPE = 'Content-Type'.freeze
        MIME_TYPE    = 'application/json'.freeze

        dependency do
          require 'json' unless defined?(::JSON)
        end if Faraday::VERSION < '2'

        def call(env)
          if env[:saddle][:request_style] == :json
            # Make sure we're working with a valid body that's not a String
            if env[:body] and !env[:body].respond_to?(:to_str)
              env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
              env[:body] = ::JSON.dump(env[:body])
            end
          end
          @app.call env
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
saddle-0.2.2 lib/saddle/middleware/request/encode_json.rb