Sha256: 18f30afb69ce59d6f1b50f1baf5adfce554bc79c79d290387ad49deda07a968b

Contents?: true

Size: 1009 Bytes

Versions: 20

Compression:

Stored size: 1009 Bytes

Contents

# frozen_string_literal: true

require "stringio"

module HTTPX
  module Transcoder
    class BodyReader
      def initialize(body)
        @body = if body.respond_to?(:read)
          body.rewind if body.respond_to?(:rewind)
          body
        elsif body.respond_to?(:each)
          body.enum_for(:each)
        else
          StringIO.new(body.to_s)
        end
      end

      def bytesize
        return @body.bytesize if @body.respond_to?(:bytesize)

        Float::INFINITY
      end

      def read(length = nil, outbuf = nil)
        return @body.read(length, outbuf) if @body.respond_to?(:read)

        begin
          chunk = @body.next
          if outbuf
            outbuf.clear.force_encoding(Encoding::BINARY)
            outbuf << chunk
          else
            outbuf = chunk
          end
          outbuf unless length && outbuf.empty?
        rescue StopIteration
        end
      end

      def close
        @body.close if @body.respond_to?(:close)
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

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