Module: OpenAI::Internal::Util Private

Extended by:
SorbetRuntimeSupport
Defined in:
lib/openai/internal/util.rb,
lib/openai/internal/read_io_adapter.rb,
sig/openai/internal/util.rbs,
sig/openai/internal/read_io_adapter.rbs

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

Defined Under Namespace

Modules: SorbetRuntimeSupport Classes: ReadIOAdapter

Constant Summary collapse

RFC_3986_NOT_PCHARS =

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

https://www.rfc-editor.org/rfc/rfc3986.html#section-3.3

Returns:

  • (Regexp)
/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/
JSON_CONTENT =

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

Returns:

  • (Regexp)
%r{\Aapplication/(?:[a-z0-9.-]+\+)?json[ \t]*(?:;|\z)}i
JSONL_CONTENT =

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

Returns:

  • (Regexp)
%r{\Aapplication/(?:x-[nl]djson|(?:x-)?jsonl)[ \t]*(?:;|\z)}i
MULTIPART_CONTENT =

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

Returns:

  • (Regexp)
%r{\Amultipart/form-data[ \t]*(?:;|\z)}i

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SorbetRuntimeSupport

const_missing, define_sorbet_constant!, sorbet_constant_defined?, to_sorbet_type, to_sorbet_type

Class Method Details

.arch ⇒ 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.

Returns:

  • (String)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/openai/internal/util.rb', line 42

def arch
  case (arch = RbConfig::CONFIG["arch"])&.downcase
  in nil
    "unknown"
  in /aarch64|arm64/
    "arm64"
  in /x86_64/
    "x64"
  in /arm/
    "arm"
  else
    "other:#{arch}"
  end
end

.chain_fused(enum, &blk) {|, arg0| ... } ⇒ Enumerable<Object>

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:

  • enum (Enumerable<Object>, nil)
  • blk (Proc)

Yields:

Yield Parameters:

  • (Enumerator::Yielder)
  • arg0 (Enumerator::Yielder)

Yield Returns:

  • (void)

Returns:

  • (Enumerable<Object>)


865
866
867
868
# File 'lib/openai/internal/util.rb', line 865

def chain_fused(enum, &blk)
  iter = Enumerator.new { blk.call(_1) }
  fused_enum(iter) { close_fused!(enum) }
end

.close_fused!(enum) ⇒ void

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.

Parameters:

  • enum (Enumerable<Object>, nil)


850
851
852
853
854
855
856
# File 'lib/openai/internal/util.rb', line 850

def close_fused!(enum)
  return unless enum.is_a?(FusedEnumerator)

  # rubocop:disable Lint/UnreachableLoop
  enum.rewind.each { break }
  # rubocop:enable Lint/UnreachableLoop
end

.coerce_boolean(input) ⇒ Boolean, Object

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:

Returns:



100
101
102
103
104
105
106
107
108
109
# File 'lib/openai/internal/util.rb', line 100

def coerce_boolean(input)
  case input.is_a?(String) ? input.downcase : input
  in "true"
    true
  in "false"
    false
  else
    input
  end
end

.coerce_boolean!(input) ⇒ 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.

Parameters:

Returns:

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
# File 'lib/openai/internal/util.rb', line 117

def coerce_boolean!(input)
  case coerce_boolean(input)
  in true | false | nil => coerced
    coerced
  else
    raise ArgumentError.new("Unable to coerce #{input.inspect} into boolean value")
  end
end

.coerce_float(input) ⇒ Float, Object

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:

  • input (String, Integer, Float)

Returns:

  • (Float, Object)


140
141
142
# File 'lib/openai/internal/util.rb', line 140

def coerce_float(input)
  Float(input, exception: false) || input
end

.coerce_hash(input) ⇒ Hash{Object=>Object}, Object

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:

  • input (Object)

Returns:

  • (Hash{Object=>Object}, Object)


149
150
151
152
153
154
155
156
# File 'lib/openai/internal/util.rb', line 149

def coerce_hash(input)
  case input
  in NilClass | Array | Set | Enumerator | StringIO | IO
    input
  else
    input.respond_to?(:to_h) ? input.to_h : input
  end
end

.coerce_hash!(input) ⇒ Hash{Object=>Object}?

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:

  • input (Object)

Returns:

  • (Hash{Object=>Object}, nil)

Raises:

  • (ArgumentError)


164
165
166
167
168
169
170
171
172
# File 'lib/openai/internal/util.rb', line 164

def coerce_hash!(input)
  case coerce_hash(input)
  in Hash | nil => coerced
    coerced
  else
    message = "Expected a #{Hash} or #{OpenAI::Internal::Type::BaseModel}, got #{input.inspect}"
    raise ArgumentError.new(message)
  end
end

.coerce_integer(input) ⇒ Integer, Object

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:

  • input (String, Integer)

Returns:

  • (Integer, Object)


131
132
133
# File 'lib/openai/internal/util.rb', line 131

def coerce_integer(input)
  Integer(input, exception: false) || input
end

.decode_content(headers, stream:, suppress_error: false) {|The, body| ... } ⇒ Object

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.

Assumes each chunk in stream has Encoding::BINARY.

Parameters:

  • headers (Hash{String=>String})
  • stream (Enumerable<String>)
  • suppress_error (Boolean) (defaults to: false)
  • stream: (Enumerable[String])
  • suppress_error: (Boolean) (defaults to: false)

Yields:

Yield Parameters:

  • The (String) —

    buffered response body before decoding.

  • body (String)

Yield Returns:

  • (void)

Returns:

  • (Object)

Raises:

  • (JSON::ParserError)


774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/openai/internal/util.rb', line 774

def decode_content(headers, stream:, suppress_error: false, &body_observer)
  case (content_type = headers["content-type"])
  in OpenAI::Internal::Util::JSON_CONTENT
    json = stream.to_a.join
    body_observer&.call(json)
    return nil if json.empty?

    begin
      JSON.parse(json, symbolize_names: true)
    rescue JSON::ParserError => e
      raise e unless suppress_error
      json
    end

  in OpenAI::Internal::Util::JSONL_CONTENT
    lines = decode_lines(stream)
    chain_fused(lines) do |y|
      lines.each do
        next if _1.empty?

        y << JSON.parse(_1, symbolize_names: true)
      end
    end

  in %r{\Atext/event-stream[ \t]*(?:;|\z)}i
    lines = decode_lines(stream)
    decode_sse(lines)
  else
    text = stream.to_a.join
    body_observer&.call(text)
    force_charset!(content_type, text: text)
    StringIO.new(text)
  end
end

.decode_lines(enum) ⇒ Enumerable<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.

This decoder is responsible for reassembling bytes split across multiple String fragments without depending on each fragment's encoding label while preserving the String encoding callers supplied.

Parameters:

  • enum (Enumerable<String>)

Returns:

  • (Enumerable<String>)


881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/openai/internal/util.rb', line 881

def decode_lines(enum)
  re = /(\r\n|\r|\n)/
  buffer = String.new(encoding: Encoding::BINARY)
  encoding = nil
  cr_seen = nil

  chain_fused(enum) do |y|
    enum.each do |row|
      encoding = row.encoding if !row.empty? && (encoding.nil? || encoding == Encoding::US_ASCII)
      offset = buffer.bytesize
      buffer << (row.encoding == Encoding::BINARY ? row : row.b)
      while (match = re.match(buffer, cr_seen || offset))
        case [match.captures.first, cr_seen]
        in ["\r", nil]
          cr_seen = match.end(1)
          next
        in [terminator, Integer] if terminator != "\n" || match.begin(1) != cr_seen
          line = buffer.slice!(..(cr_seen.pred))
          line.force_encoding(encoding) unless encoding.nil?
          y << line
        else
          line = buffer.slice!(..(match.end(1).pred))
          line.force_encoding(encoding) unless encoding.nil?
          y << line
        end

        offset = 0
        cr_seen = nil
      end
    end

    unless cr_seen.nil?
      line = buffer.slice!(..(cr_seen.pred))
      line.force_encoding(encoding) unless encoding.nil?
      y << line
    end

    unless buffer.empty?
      buffer.force_encoding(encoding) unless encoding.nil?
      y << buffer
    end
  end
end

.decode_query(query) ⇒ Hash{String=>Array<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:

  • query (String, nil)

Returns:

  • (Hash{String=>Array<String>})


302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/openai/internal/util.rb', line 302

def decode_query(query)
  params = {}
  query.to_s.split(/[&;]/).each do |pair|
    key, value = pair.split("=", 2).map do |component|
      # URI rejects malformed escapes, while CGI leaves them literal and stops at a trailing "%+".
      escaped = component.gsub(/%(?![0-9a-fA-F]{2})/, "%25")
      escaped = escaped.delete_suffix("+") + "%2B" if component.end_with?("%+")
      decoded = URI.decode_www_form_component(escaped).force_encoding(Encoding::UTF_8)
      decoded.valid_encoding? ? decoded : decoded.force_encoding(component.encoding)
    end

    next unless key

    params[key] ||= []
    params[key] << value if value
  end

  params.default = [].freeze
  params
end

.decode_sse(lines) ⇒ Enumerable<Hash{Symbol=>Object}>

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.

https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream

Assumes that lines has been decoded with #decode_lines.

Parameters:

  • lines (Enumerable<String>)

Returns:

  • (Enumerable<Hash{Symbol=>Object}>)


934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/openai/internal/util.rb', line 934

def decode_sse(lines)
  chain_fused(lines) do |y|
    blank = {event: nil, data: nil, id: nil, retry: nil}
    current = {}
    first_line = true

    lines.each do |line|
      if first_line
        line = line.byteslice(3..) || "" if line.byteslice(0, 3)&.bytes == [0xEF, 0xBB, 0xBF]
        first_line = false
      end

      case line.sub(/\R$/, "")
      in ""
        next if current.empty?
        y << {**blank, **current}
        current = {}
      in /^:/
        next
      in /^([^:]+)(?::\s?(.*))?$/
        field, value = Regexp.last_match.captures
        # A line with no colon names a field whose value is the empty
        # string, so a bare `data` line still contributes a blank line to
        # the data buffer.
        value = value.to_s
        case field
        in "event"
          current.merge!(event: value)
        in "data"
          (current[:data] ||= String.new) << value << "\n"
        in "id" unless value.include?("\0")
          current.merge!(id: value)
        in "retry" if /^\d+$/ =~ value
          current.merge!(retry: Integer(value, 10))
        else
        end
      else
      end
    end

    y << {**blank, **current} unless current.empty?
  end
end

.deep_merge(*values, sentinel: nil, concat: false) ⇒ Object

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.

Recursively merge one hash with another. If the values at a given key are not both hashes, just take the new value.

Parameters:

  • values (Array<Object>)
  • sentinel (Object, nil) (defaults to: nil) —

    the value to return if no values are provided.

  • concat (Boolean) (defaults to: false) —

    whether to merge sequences by concatenation.

  • sentinel: (top, nil) (defaults to: nil)
  • concat: (Boolean) (defaults to: false)

Returns:

  • (Object)


206
207
208
209
210
211
212
213
214
215
# File 'lib/openai/internal/util.rb', line 206

def deep_merge(*values, sentinel: nil, concat: false)
  case values
  in [value, *values]
    values.reduce(value) do |acc, val|
      deep_merge_lr(acc, val, concat: concat)
    end
  else
    sentinel
  end
end

.deep_merge_lr ⇒ top

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:

  • lhs (top)
  • rhs (top)
  • concat: (Boolean)

Returns:

  • (top)


30
# File 'sig/openai/internal/util.rbs', line 30

def self?.deep_merge_lr: (top lhs, top rhs, ?concat: bool) -> top

.dig(data, pick, &blk) { ... } ⇒ Object?

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:

  • data (Hash{Symbol=>Object}, Array<Object>, Object)
  • pick (Symbol, Integer, Array<Symbol, Integer>, Proc, nil)
  • blk (Proc, nil)

Yields:

Yield Returns:

  • (top, nil)

Returns:

  • (Object, nil)


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/openai/internal/util.rb', line 224

def dig(data, pick, &blk)
  case [data, pick]
  in [_, nil]
    data
  in [Hash, Symbol] | [Array, Integer]
    data.fetch(pick) { blk&.call }
  in [Hash | Array, Array]
    pick.reduce(data) do |acc, key|
      case acc
      in Hash if acc.key?(key)
        acc.fetch(key)
      in Array if key.is_a?(Integer) && key < acc.length
        acc[key]
      else
        return blk&.call
      end
    end

  in [_, Proc]
    pick.call(data)
  else
    blk&.call
  end
end

.encode_content(headers, body) ⇒ Object

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:

  • headers (Hash{String=>String})
  • body (Object)

Returns:

  • (Object)


631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/openai/internal/util.rb', line 631

def encode_content(headers, body)
  content_type = headers["content-type"]
  case [content_type, body]
  in [OpenAI::Internal::Util::JSON_CONTENT, Hash | Array | -> { primitive?(_1) }]
    [headers, JSON.generate(body)]
  in [OpenAI::Internal::Util::JSONL_CONTENT, Enumerable] unless OpenAI::Internal::Type::FileInput === body
    [headers, body.lazy.map { "#{JSON.generate(_1)}\n" }]
  in [OpenAI::Internal::Util::MULTIPART_CONTENT, Hash | OpenAI::Internal::Type::FileInput]
    boundary, strio = encode_multipart_streaming(body)
    headers = {**headers, "content-type" => "#{content_type}; boundary=#{boundary}"}
    [headers, strio]
  in [_, Symbol | Numeric]
    [headers, body.to_s]
  in [_, StringIO]
    [headers, body.string.byteslice(body.pos..) || body.string.byteslice(0, 0)]
  in [_, OpenAI::FilePart] if body.content.is_a?(StringIO)
    content = body.content
    [headers, content.string.byteslice(content.pos..) || content.string.byteslice(0, 0)]
  in [_, OpenAI::FilePart]
    [headers, body.content]
  else
    [headers, body]
  end
end

.encode_multipart_streaming ⇒ [String, Enumerable[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:

  • body (top)

Returns:

  • ([String, Enumerable[String]])


135
136
137
# File 'sig/openai/internal/util.rbs', line 135

def self?.encode_multipart_streaming: (
  top body
) -> [String, Enumerable[String]]

.encode_path(path) ⇒ 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:

  • path (String, Integer)

Returns:

  • (String)

Raises:

  • (ArgumentError)


271
272
273
274
275
276
# File 'lib/openai/internal/util.rb', line 271

def encode_path(path)
  path = path.to_s
  raise ArgumentError, "path segment cannot be '.' or '..'" if path == "." || path == ".."

  path.gsub(OpenAI::Internal::Util::RFC_3986_NOT_PCHARS) { ERB::Util.url_encode(_1) }
end

.encode_query(query) ⇒ 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:

  • query (Hash{String=>Array<String>, String, nil}, nil)

Returns:

  • (String, nil)


328
329
330
# File 'lib/openai/internal/util.rb', line 328

def encode_query(query)
  query.to_h.empty? ? nil : URI.encode_www_form(query)
end

.encode_query_params(query) ⇒ Hash{Symbol=>Object}

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:

  • query (Hash{Symbol=>Object})

Returns:

  • (Hash{Symbol=>Object})


461
462
463
464
465
# File 'lib/openai/internal/util.rb', line 461

def encode_query_params(query)
  out = {}
  query.each { write_query_param_element!(out, _1, _2) }
  out
end

.escape_multipart_header_param ⇒ 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:

  • value (Pathname, String, Symbol)

Returns:

  • (String)


115
116
117
# File 'sig/openai/internal/util.rbs', line 115

def self?.escape_multipart_header_param: (
  Pathname | String | Symbol value
) -> String

.force_charset!(content_type, text:) ⇒ void

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.

https://www.iana.org/assignments/character-sets/character-sets.xhtml

Parameters:

  • content_type (String)
  • text (String)
  • text: (String)


750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/openai/internal/util.rb', line 750

def force_charset!(content_type, text:)
  charset = media_type_charset(content_type)

  return if charset.nil? || charset.empty?

  begin
    encoding = Encoding.find(charset)
    text.force_encoding(encoding)
  rescue ArgumentError
    nil
  end
end

.fused_enum(enum, external: false, &close) { ... } ⇒ Enumerable<Object>

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.

https://doc.rust-lang.org/std/iter/trait.FusedIterator.html

Parameters:

  • enum (Enumerable<Object>)
  • external (Boolean) (defaults to: false)
  • close (Proc)
  • external: (Boolean) (defaults to: false)

Yields:

Yield Returns:

  • (void)

Returns:

  • (Enumerable<Object>)


820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/openai/internal/util.rb', line 820

def fused_enum(enum, external: false, &close)
  fused = false
  iter = Enumerator.new do |y|
    next if fused

    fused = true
    if external
      loop { y << enum.next }
    else
      enum.each(&y)
    end

  ensure
    close&.call
    close = nil
  end

  iter.extend(FusedEnumerator)

  iter.define_singleton_method(:rewind) do
    fused = true
    self
  end

  iter
end

.interpolate_path(path) ⇒ 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:

  • path (String, Array<String>)

Returns:

  • (String)


283
284
285
286
287
288
289
290
291
292
293
# File 'lib/openai/internal/util.rb', line 283

def interpolate_path(path)
  case path
  in String
    path
  in []
    ""
  in [String => p, *interpolations]
    encoded = interpolations.map { encode_path(_1) }
    format(p, *encoded)
  end
end

.join_parsed_uri(lhs, rhs) ⇒ URI::Generic

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:

  • lhs (Hash{Symbol=>String, Integer, nil}) —

    .

    @option lhs [String, nil] :scheme

    @option lhs [String, nil] :host

    @option lhs [Integer, nil] :port

    @option lhs [String, nil] :path

    @option lhs [HashString=>Array<String>] :query

  • rhs (Hash{Symbol=>String, Integer, nil}) —

    .

    @option rhs [String, nil] :scheme

    @option rhs [String, nil] :host

    @option rhs [Integer, nil] :port

    @option rhs [String, nil] :path

    @option rhs [HashString=>Array<String>] :query

Returns:

  • (URI::Generic)


390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/openai/internal/util.rb', line 390

def join_parsed_uri(lhs, rhs)
  base_path, base_query = lhs.fetch_values(:path, :query)
  slashed = base_path.end_with?("/") ? base_path : "#{base_path}/"

  merged = {**parse_uri(rhs.fetch(:path)), **rhs.except(:path, :query)}
  parsed_path, parsed_query = merged.fetch_values(:path, :query)
  override = URI::Generic.build(**merged.slice(:scheme, :host, :port), path: parsed_path)

  joined = URI.join(URI::Generic.build(lhs.except(:path, :query)), slashed, override)
  query = deep_merge(
    joined.path == base_path ? base_query : {},
    parsed_query,
    rhs[:query].to_h,
    concat: true
  )

  joined.query = encode_query(query)
  joined
end

.monotonic_secs ⇒ Float

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:

  • (Float)


16
# File 'lib/openai/internal/util.rb', line 16

def self.monotonic_secs = Process.clock_gettime(Process::CLOCK_MONOTONIC)

.normalized_headers(*headers) ⇒ Hash{String=>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:

  • headers (Hash{String=>String, Integer, Array<String, Integer, nil>, nil})

Returns:

  • (Hash{String=>String})


417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/openai/internal/util.rb', line 417

def normalized_headers(*headers)
  headers.compact.flat_map(&:to_a).to_h do |key, val|
    value = case val
    in Array
      val.filter_map { _1&.to_s&.strip }.join(", ")
    else
      val&.to_s&.strip
    end

    [key.to_s.downcase, value]
  end
end

.os ⇒ 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.

Returns:

  • (String)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/openai/internal/util.rb', line 60

def os
  case (host = RbConfig::CONFIG["host_os"])&.downcase
  in nil
    "Unknown"
  in /linux/
    "Linux"
  in /darwin/
    "MacOS"
  in /freebsd/
    "FreeBSD"
  in /openbsd/
    "OpenBSD"
  in /mswin|mingw|cygwin|ucrt/
    "Windows"
  else
    "Other:#{host}"
  end
end

.parse_uri(url) ⇒ Hash{Symbol=>String, Integer, nil}

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:

  • url (URI::Generic, String)

Returns:

  • (Hash{Symbol=>String, Integer, nil})


339
340
341
342
# File 'lib/openai/internal/util.rb', line 339

def parse_uri(url)
  parsed = URI::Generic.component.zip(URI.split(url)).to_h
  {**parsed, query: decode_query(parsed.fetch(:query))}
end

.primitive?(input) ⇒ 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.

Parameters:

  • input (Object)

Returns:



86
87
88
89
90
91
92
93
# File 'lib/openai/internal/util.rb', line 86

def primitive?(input)
  case input
  in true | false | Numeric | Symbol | String
    true
  else
    false
  end
end

.unparse_uri(parsed) ⇒ URI::Generic

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:

  • parsed (Hash{Symbol=>String, Integer, nil}) —

    .

    @option parsed [String, nil] :scheme

    @option parsed [String, nil] :host

    @option parsed [Integer, nil] :port

    @option parsed [String, nil] :path

    @option parsed [HashString=>Array<String>] :query

Returns:

  • (URI::Generic)


359
360
361
# File 'lib/openai/internal/util.rb', line 359

def unparse_uri(parsed)
  URI::Generic.build(**parsed, query: encode_query(parsed.fetch(:query)))
end

.uri_origin(uri) ⇒ 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:

  • uri (URI::Generic)

Returns:

  • (String)


261
262
263
# File 'lib/openai/internal/util.rb', line 261

def uri_origin(uri)
  "#{uri.scheme}://#{uri.host}#{":#{uri.port}" unless uri.port == uri.default_port}"
end

.walk_namespaces(ns) ⇒ Enumerable<Module, Class>

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:

  • ns (Module, Class)

Returns:

  • (Enumerable<Module, Class>)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openai/internal/util.rb', line 23

def self.walk_namespaces(ns)
  ns
    .constants(false)
    .lazy
    .flat_map do
      case (c = ns.const_get(_1, false))
      in Module | Class
        walk_namespaces(c)
      else
        []
      end
    end
    .chain([ns])
end

.writable_enum(&blk) {|, y| ... } ⇒ Enumerable<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:

  • blk (Proc)

Yields:

Yield Parameters:

  • (Enumerator::Yielder)
  • y (Enumerator::Yielder)

Yield Returns:

  • (void)

Returns:

  • (Enumerable<String>)


436
437
438
439
440
441
442
443
444
445
# File 'lib/openai/internal/util.rb', line 436

def writable_enum(&blk)
  Enumerator.new do |y|
    y.define_singleton_method(:write) do
      self << _1.dup
      _1.bytesize
    end

    blk.call(y)
  end
end

.write_multipart_chunk ⇒ void

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.

Parameters:

  • y (Enumerator::Yielder)
  • boundary: (String)
  • key: (Symbol, String)
  • val: (top)
  • closing: (::Array[^-> void])


119
120
121
122
123
124
125
# File 'sig/openai/internal/util.rbs', line 119

def self?.write_multipart_chunk: (
  Enumerator::Yielder y,
  boundary: String,
  key: Symbol | String,
  val: top,
  closing: ::Array[^-> void]
) -> void

.write_multipart_content ⇒ void

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.

Parameters:

  • y (Enumerator::Yielder)
  • val: (top)
  • closing: (::Array[^-> void])
  • content_type: (String, nil)


108
109
110
111
112
113
# File 'sig/openai/internal/util.rbs', line 108

def self?.write_multipart_content: (
  Enumerator::Yielder y,
  val: top,
  closing: ::Array[^-> void],
  ?content_type: String?
) -> void

.write_multipart_value ⇒ void

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.

Parameters:

  • y (Enumerator::Yielder)
  • boundary: (String)
  • key: (Symbol, String)
  • val: (top)
  • closing: (::Array[^-> void])


127
128
129
130
131
132
133
# File 'sig/openai/internal/util.rbs', line 127

def self?.write_multipart_value: (
  Enumerator::Yielder y,
  boundary: String,
  key: Symbol | String,
  val: top,
  closing: ::Array[^-> void]
) -> void

Instance Method Details

#encode_query_params ⇒ ::Hash[Symbol, top]

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:

  • query (::Hash[Symbol, top])

Returns:

  • (::Hash[Symbol, top])


98
99
100
# File 'sig/openai/internal/util.rbs', line 98

def encode_query_params: (
  ::Hash[Symbol, top] query
) -> ::Hash[Symbol, top]