Class: ZendeskAPI::Middleware::Request::EncodeJson

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/zendesk_api/middleware/request/encode_json.rb

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'.freeze
MIME_TYPE =
'application/json'.freeze

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zendesk_api/middleware/request/encode_json.rb', line 11

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