Class: OpenAI::Internal::Util::ReadIOAdapter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/internal/read_io_adapter.rb,
sig/openai/internal/read_io_adapter.rbs

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

An adapter that satisfies the IO interface required by ::IO.copy_stream

Instance Method Summary collapse

Constructor Details

#initialize(src, &blk) {|| ... } ⇒ ReadIOAdapter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ReadIOAdapter.

Parameters:

  • src (String, Pathname, StringIO, Enumerable<String>)
  • blk (Proc)

Yield Parameters:

  • (String)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/openai/internal/read_io_adapter.rb', line 145

def initialize(src, &blk)
  @stream = case src
  in String
    StringIO.new(src)
  in Pathname
    @closing = true
    src.open(binmode: true)
  in Enumerator
    @closing = true
    InterruptibleEnumerator.new(src)
  else
    src
  end

  @buf = String.new.b
  @blk = blk
end

Instance Method Details

#closevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



62
63
64
65
66
67
68
69
70
# File 'lib/openai/internal/read_io_adapter.rb', line 62

def close
  case @stream
  in Enumerator
    OpenAI::Internal::Util.close_fused!(@stream)
  in InterruptibleEnumerator | IO if close?
    @stream.close
  else
  end
end

#close?Boolean?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



59
# File 'lib/openai/internal/read_io_adapter.rb', line 59

def close? = @closing

#read(max_len = nil, out_string = nil) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • max_len (Integer, nil) (defaults to: nil)
  • out_string (String, nil) (defaults to: nil)

Returns:

  • (String, nil)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/openai/internal/read_io_adapter.rb', line 115

def read(max_len = nil, out_string = nil)
  if max_len.is_a?(Integer) && max_len.negative?
    raise ArgumentError, "negative length #{max_len} given"
  end

  read = case @stream
  in nil
    max_len.nil? || max_len.zero? ? +"" : nil
  in IO | StringIO
    return @stream.read(max_len, out_string).tap(&@blk)
  in Enumerator | InterruptibleEnumerator
    read_enum(max_len)
  end

  case out_string
  in String
    out_string.replace(read || +"")
    read.nil? ? nil : out_string
  in nil
    read
  end
    .tap(&@blk)
end