Class: HTTPX::Transcoder::BodyReader

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/transcoder/utils/body_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ BodyReader

Returns a new instance of BodyReader.



8
9
10
11
12
13
14
15
16
17
# File 'lib/httpx/transcoder/utils/body_reader.rb', line 8

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

Instance Method Details

#bytesizeObject



19
20
21
22
23
# File 'lib/httpx/transcoder/utils/body_reader.rb', line 19

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

  Float::INFINITY
end

#closeObject



41
42
43
# File 'lib/httpx/transcoder/utils/body_reader.rb', line 41

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

#read(length = nil, outbuf = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/httpx/transcoder/utils/body_reader.rb', line 25

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