Sha256: 526c8964978d1cd49f2dd7d2d3b080b92a84b7c29fed157659d1e80fde02cfd6

Contents?: true

Size: 1.02 KB

Versions: 20

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

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

module HTTPX::Transcoder
  module Xml
    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

20 entries across 20 versions & 1 rubygems

Version Path
httpx-1.3.4 lib/httpx/transcoder/xml.rb
httpx-1.3.3 lib/httpx/transcoder/xml.rb
httpx-1.3.2 lib/httpx/transcoder/xml.rb
httpx-1.3.1 lib/httpx/transcoder/xml.rb
httpx-1.3.0 lib/httpx/transcoder/xml.rb
httpx-1.2.6 lib/httpx/transcoder/xml.rb
httpx-1.2.4 lib/httpx/transcoder/xml.rb
httpx-1.2.3 lib/httpx/transcoder/xml.rb
httpx-1.2.2 lib/httpx/transcoder/xml.rb
httpx-1.2.1 lib/httpx/transcoder/xml.rb
httpx-1.2.0 lib/httpx/transcoder/xml.rb
httpx-1.1.5 lib/httpx/transcoder/xml.rb
httpx-1.1.4 lib/httpx/transcoder/xml.rb
httpx-1.1.3 lib/httpx/transcoder/xml.rb
httpx-1.1.2 lib/httpx/transcoder/xml.rb
httpx-1.1.1 lib/httpx/transcoder/xml.rb
httpx-1.1.0 lib/httpx/transcoder/xml.rb
httpx-1.0.2 lib/httpx/transcoder/xml.rb
httpx-1.0.1 lib/httpx/transcoder/xml.rb
httpx-1.0.0 lib/httpx/transcoder/xml.rb