Sha256: ae612a56a1b47fb8604b7df17e342a007637825d5ea3b916aa831a87fc78d4ee

Contents?: true

Size: 1.05 KB

Versions: 13

Compression:

Stored size: 1.05 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
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
httpx-0.24.7 lib/httpx/transcoder/xml.rb
httpx-0.24.6 lib/httpx/transcoder/xml.rb
httpx-0.24.5 lib/httpx/transcoder/xml.rb
httpx-0.24.4 lib/httpx/transcoder/xml.rb
httpx-0.24.3 lib/httpx/transcoder/xml.rb
httpx-0.24.2 lib/httpx/transcoder/xml.rb
httpx-0.24.1 lib/httpx/transcoder/xml.rb
httpx-0.24.0 lib/httpx/transcoder/xml.rb
httpx-0.23.4 lib/httpx/transcoder/xml.rb
httpx-0.23.3 lib/httpx/transcoder/xml.rb
httpx-0.23.2 lib/httpx/transcoder/xml.rb
httpx-0.23.1 lib/httpx/transcoder/xml.rb
httpx-0.23.0 lib/httpx/transcoder/xml.rb