Sha256: d0354d00f2aa0713f38837666f63684d3702131208ff6cddede45c0d11f1aec4
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 KB
Contents
require 'faraday' module FaradayMiddleware # 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. class EncodeXml < Faraday::Middleware CONTENT_TYPE = 'Content-Type'.freeze MIME_TYPE = 'application/xml'.freeze dependency do require 'gyoku' unless defined?(::Gyoku) end def call(env) match_content_type(env) do |data| env[:body] = encode data end @app.call env end def encode(data) ::Gyoku.xml(data, :key_converter => :none) end def match_content_type(env) if process_request?(env) env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE yield env[:body] unless env[:body].respond_to?(:to_str) end end def process_request?(env) type = request_type(env) has_body?(env) and (type.empty? or type == MIME_TYPE) end def has_body?(env) body = env[:body] and !(body.respond_to?(:to_str) and body.empty?) end def request_type(env) type = env[:request_headers][CONTENT_TYPE].to_s type = type.split(';', 2).first if type.index(';') type end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
einvoice-0.1.0 | lib/faraday/request/encode_xml.rb |