Sha256: c34c314d1d67faa008fc360ac81fa4d9910d1e5af7d13d30a185df9eb62343bf

Contents?: true

Size: 685 Bytes

Versions: 2

Compression:

Stored size: 685 Bytes

Contents

module ZaloAPI
  module Middleware
    module Request
      class EncodeJson < Faraday::Middleware
        CONTENT_TYPE = 'Content-Type'.freeze
        MIME_TYPE = 'application/json'.freeze
        dependency 'json'

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

          if env[:body] && !(env[:body].respond_to?(:to_str) && env[:body].empty?) && (type.empty? || type == MIME_TYPE)
            env[:body] = JSON.dump(env[:body])
            env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
          end

          @app.call(env)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zalo-api-0.1.1 lib/zalo_api/middleware/request/encode_json.rb
zalo-api-0.1.0 lib/zalo_api/middleware/request/encode_json.rb