class Faraday::EncodeXML::Middleware

Request middleware that encodes the body as XML.

Processes only requests with matching Content-type or those without a type. If a request doesn’t have a type but has a body, it sets the Content-type to XML MIME-type.

Doesn’t try to encode bodies that already are in string form.

Constants

CONTENT_TYPE
MIME_TYPE

Public Instance Methods

on_request(env) click to toggle source

This method will be called when the request is being prepared. You can alter it as you like, accessing things like request_body, request_headers, and more. Refer to Faraday::Env for a list of accessible fields: github.com/lostisland/faraday/blob/main/lib/faraday/options/env.rb

@param env [Faraday::Env] the environment of the request being processed

# File lib/faraday/encode_xml/middleware.rb, line 28
def on_request(env)
  match_content_type(env) do |data|
    env[:body] = encode data
  end
end

Private Instance Methods

body?(env) click to toggle source
# File lib/faraday/encode_xml/middleware.rb, line 52
def body?(env)
  (body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
end
encode(data) click to toggle source
# File lib/faraday/encode_xml/middleware.rb, line 36
def encode(data)
  ::Gyoku.xml(data, key_converter: :none)
end
match_content_type(env) { |env| ... } click to toggle source
# File lib/faraday/encode_xml/middleware.rb, line 40
def match_content_type(env)
  return unless process_request?(env)

  env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
  yield env[:body] unless env[:body].respond_to?(:to_str)
end
process_request?(env) click to toggle source
# File lib/faraday/encode_xml/middleware.rb, line 47
def process_request?(env)
  type = request_type(env)
  body?(env) && (type.empty? or type == MIME_TYPE)
end
request_type(env) click to toggle source
# File lib/faraday/encode_xml/middleware.rb, line 56
def request_type(env)
  type = env[:request_headers][CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end