Sha256: d8c28e69ad3253a380d56e104366073532995c24f8fc7dfa43565847902f3348

Contents?: true

Size: 880 Bytes

Versions: 32

Compression:

Stored size: 880 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[:request][: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

32 entries across 32 versions & 1 rubygems

Version Path
saddle-0.0.53 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.51 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.50 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.49 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.48 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.46 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.45 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.44 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.43 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.42 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.41 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.38 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.37 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.36 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.35 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.34 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.33 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.32 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.31 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.30 lib/saddle/middleware/request/encode_json.rb