Sha256: 68305ac1a413fd5d77fd590fa9da865246c0e7e6f89720dd8c21de47d83aea44

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

require "delegate"
require "forwardable"
require "uri"

module HTTPX::Transcoder
  module Xml
    using HTTPX::RegexpExtensions

    module_function

    MIME_TYPES = %r{\b(application|text)/(.+\+)?xml\b}.freeze

    class Encoder
      def initialize(xml)
        @raw = xml
      end

      def content_type
        charset = @raw.respond_to?(:encoding) ? @raw.encoding.to_s.downcase : "utf-8"
        "application/xml; charset=#{charset}"
      end

      def bytesize
        @raw.to_s.bytesize
      end

      def to_s
        @raw.to_s
      end
    end

    def encode(xml)
      Encoder.new(xml)
    end

    begin
      require "nokogiri"

      def decode(response)
        content_type = response.content_type.mime_type

        raise HTTPX::Error, "invalid form mime type (#{content_type})" unless MIME_TYPES.match?(content_type)

        Nokogiri::XML.method(:parse)
      end
    rescue LoadError
      def decode(_response)
        raise HTTPX::Error, "\"nokogiri\" is required in order to decode XML"
      end
    end
  end
  register "xml", Xml
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
httpx-0.22.5 lib/httpx/transcoder/xml.rb
httpx-0.22.4 lib/httpx/transcoder/xml.rb
httpx-0.22.3 lib/httpx/transcoder/xml.rb
httpx-0.22.2 lib/httpx/transcoder/xml.rb
httpx-0.22.1 lib/httpx/transcoder/xml.rb
httpx-0.22.0 lib/httpx/transcoder/xml.rb