Sha256: 741e4523064b26c1df74040101e2cc8d5bf87a4f5ff38d55cf9838d624a955ec

Contents?: true

Size: 879 Bytes

Versions: 1

Compression:

Stored size: 879 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

        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.1.0 lib/saddle/middleware/request/encode_json.rb