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.27 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.26 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.25 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.23 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.22 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.21 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.19 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.18 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.17 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.16 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.15 lib/saddle/middleware/request/encode_json.rb
saddle-0.0.14 lib/saddle/middleware/request/encode_json.rb