Sha256: 0dab8db8bb72f70fb181f0fb8c958861b6f92a77112c838388185a1af170adc3

Contents?: true

Size: 1.15 KB

Versions: 17

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

require "forwardable"

module HTTPX::Transcoder
  module Body
    class Error < HTTPX::Error; end

    module_function

    class Encoder
      extend Forwardable

      def_delegator :@raw, :to_s

      def initialize(body)
        @raw = body
      end

      def bytesize
        if @raw.respond_to?(:bytesize)
          @raw.bytesize
        elsif @raw.respond_to?(:to_ary)
          @raw.sum(&:bytesize)
        elsif @raw.respond_to?(:size)
          @raw.size || Float::INFINITY
        elsif @raw.respond_to?(:length)
          @raw.length || Float::INFINITY
        elsif @raw.respond_to?(:each)
          Float::INFINITY
        else
          raise Error, "cannot determine size of body: #{@raw.inspect}"
        end
      end

      def content_type
        "application/octet-stream"
      end

      private

      def respond_to_missing?(meth, *args)
        @raw.respond_to?(meth, *args) || super
      end

      def method_missing(meth, *args, &block)
        return super unless @raw.respond_to?(meth)

        @raw.__send__(meth, *args, &block)
      end
    end

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

Version data entries

17 entries across 17 versions & 1 rubygems

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