Module: Anthropic::Internal::Util Private

Extended by:
SorbetRuntimeSupport
Defined in:
lib/anthropic/internal/util.rb,
sig/anthropic/internal/util.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{^application/(?:[a-zA-Z0-9.-]+\+)?json(?!l)}
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{^application/(?:x-(?:n|l)djson|(?:x-)?jsonl)}

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)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/anthropic/internal/util.rb', line 65

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>)


895
896
897
898
# File 'lib/anthropic/internal/util.rb', line 895

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)


880
881
882
883
884
885
886
# File 'lib/anthropic/internal/util.rb', line 880

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

  # 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:



123
124
125
126
127
128
129
130
131
132
# File 'lib/anthropic/internal/util.rb', line 123

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)


140
141
142
143
144
145
146
147
# File 'lib/anthropic/internal/util.rb', line 140

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)


163
164
165
# File 'lib/anthropic/internal/util.rb', line 163

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)


172
173
174
175
176
177
178
179
# File 'lib/anthropic/internal/util.rb', line 172

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)


187
188
189
190
191
192
193
194
195
# File 'lib/anthropic/internal/util.rb', line 187

def coerce_hash!(input)
  case coerce_hash(input)
  in Hash | nil => coerced
    coerced
  else
    message = "Expected a #{Hash} or #{Anthropic::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)


154
155
156
# File 'lib/anthropic/internal/util.rb', line 154

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

.decode_content(headers, stream:, suppress_error: false, binary: 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.

Assumes each chunk in stream has Encoding::BINARY.

Parameters:

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

Returns:

  • (Object)

Raises:

  • (JSON::ParserError)


813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'lib/anthropic/internal/util.rb', line 813

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

    begin
      JSON.parse(json, symbolize_names: true)
    rescue JSON::ParserError => e
      raise e unless suppress_error
      json
    end
  in Anthropic::Internal::Util::JSONL_CONTENT unless binary
    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{^text/event-stream} unless binary
    lines = decode_lines(stream)
    decode_sse(lines)
  else
    text = stream.to_a.join
    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.

Assumes Strings have been forced into having Encoding::BINARY.

This decoder is responsible for reassembling lines split across multiple fragments.

Parameters:

  • enum (Enumerable<String>)

Returns:

  • (Enumerable<String>)


912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
# File 'lib/anthropic/internal/util.rb', line 912

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

  chain_fused(enum) do |y|
    enum.each do |row|
      offset = buffer.bytesize
      buffer << row
      while (match = re.match(buffer, cr_seen&.to_i || offset))
        case [match.captures.first, cr_seen]
        in ["\r", nil]
          cr_seen = match.end(1)
          next
        in ["\r" | "\r\n", Integer]
          y << buffer.slice!(..(cr_seen.pred))
        else
          y << buffer.slice!(..(match.end(1).pred))
        end
        offset = 0
        cr_seen = nil
      end
    end

    y << buffer.slice!(..(cr_seen.pred)) unless cr_seen.nil?
    y << buffer unless buffer.empty?
  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>})


329
330
331
332
333
# File 'lib/anthropic/internal/util.rb', line 329

def decode_query(query)
  URI.decode_www_form(query.to_s).each_with_object({}) do |(key, value), out|
    (out[key] ||= []) << value
  end
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}>)


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
977
978
979
980
981
982
983
984
# File 'lib/anthropic/internal/util.rb', line 950

def decode_sse(lines)
  # rubocop:disable Metrics/BlockLength
  chain_fused(lines) do |y|
    blank = {event: nil, data: nil, id: nil, retry: nil}
    current = {}

    lines.each do |line|
      case line.sub(/\R$/, "")
      in ""
        next if current.empty?
        y << {**blank, **current}
        current = {}
      in /^:/
        next
      in /^([^:]+):\s?(.*)$/
        field, value = Regexp.last_match.captures
        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))
        else
        end
      else
      end
    end
    # rubocop:enable Metrics/BlockLength

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

.deep_frozen_copy(obj) ⇒ 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.

Structurally copy and freeze obj (Hash/Array recursively; mutable String leaves dup-frozen; URI::Generic re-parsed with each component string frozen) so the result is fully de-aliased from the input and any in-place mutation raises FrozenError. Other leaves (Integer/Symbol — already frozen; typed models; IO) pass through, IO because it must stay live for streamed uploads.

Parameters:

  • obj (Object)

Returns:

  • (Object)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/anthropic/internal/util.rb', line 41

def deep_frozen_copy(obj)
  case obj
  when Hash
    obj.transform_values { deep_frozen_copy(_1) }.freeze
  when Array
    obj.map { deep_frozen_copy(_1) }.freeze
  when String
    obj.frozen? ? obj : obj.dup.freeze
  when URI::Generic
    # Re-parse de-aliases every component string; freezing each via its
    # public reader (then the URI) makes `url.path << "x"` raise without
    # reaching below `URI::Generic`'s public surface. Callers `dup` then
    # reassign components, so a frozen URI is fine to derive from.
    URI.parse(obj.to_s).tap do |u|
      [u.scheme, u.userinfo, u.host, u.path, u.query, u.opaque, u.fragment].each { _1&.freeze }
    end.freeze
  else
    obj
  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)


229
230
231
232
233
234
235
236
237
238
# File 'lib/anthropic/internal/util.rb', line 229

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)


32
# File 'sig/anthropic/internal/util.rbs', line 32

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)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/anthropic/internal/util.rb', line 247

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)


753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
# File 'lib/anthropic/internal/util.rb', line 753

def encode_content(headers, body)
  # rubocop:disable Style/CaseEquality
  # rubocop:disable Layout/LineLength
  content_type = headers["content-type"]
  case [content_type, body]
  in [Anthropic::Internal::Util::JSON_CONTENT, Hash | Array | -> { primitive?(_1) }]
    [headers, JSON.generate(body)]
  in [Anthropic::Internal::Util::JSONL_CONTENT, Enumerable] unless Anthropic::Internal::Type::FileInput === body
    [headers, body.lazy.map { JSON.generate(_1) }]
  # A `boundary=` already in the content-type means the body was
  # encoded upstream (e.g. SigV4 signing encodes-then-signs and the
  # terminal calls this again) — fall through to the pass-through arms
  # instead of re-wrapping the signed bytes with a second boundary.
  in [%r{^multipart/form-data}, Hash | Anthropic::Internal::Type::FileInput] unless content_type.include?("boundary=")
    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]
  in [_, Anthropic::FilePart]
    [headers, body.content]
  else
    [headers, body]
  end
  # rubocop:enable Layout/LineLength
  # rubocop:enable Style/CaseEquality
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]])


142
143
144
# File 'sig/anthropic/internal/util.rbs', line 142

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)


292
293
294
# File 'lib/anthropic/internal/util.rb', line 292

def encode_path(path)
  path.to_s.gsub(Anthropic::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)


340
341
342
343
344
# File 'lib/anthropic/internal/util.rb', line 340

def encode_query(query)
  return if query.to_h.empty?

  URI.encode_www_form(query).gsub(/[+*]|%7E/, {"+" => "%20", "*" => "%2A", "%7E" => "~"})
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})


581
582
583
584
585
# File 'lib/anthropic/internal/util.rb', line 581

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

.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)


789
790
791
792
793
794
795
796
797
798
799
800
# File 'lib/anthropic/internal/util.rb', line 789

def force_charset!(content_type, text:)
  charset = /charset=([^;\s]+)/.match(content_type)&.captures&.first

  return unless charset

  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>)


854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
# File 'lib/anthropic/internal/util.rb', line 854

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.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)

Raises:

  • (ArgumentError)


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

def interpolate_path(path)
  case path
  in String
    path
  in []
    ""
  in [String => p, *interpolations]
    encoded = interpolations.map { encode_path(_1) }
    interpolated = format(p, *encoded)
    dot_segment = encoded.find { _1 == "." || _1 == ".." || _1.start_with?(".;", "..;") }
    if dot_segment
      raise ArgumentError.new("The request was not sent: path #{interpolated.inspect} has the dot segment #{dot_segment.inspect}, which would send it to a different URL")
    end
    if encoded.any? { _1.empty? || _1.start_with?(";") }
      raise ArgumentError.new("The request was not sent: path #{interpolated.inspect} has an empty path parameter, which would send it to a different URL")
    end
    interpolated
  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)


404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/anthropic/internal/util.rb', line 404

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)


10
# File 'lib/anthropic/internal/util.rb', line 10

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.

Later hashes take precedence on key collision, except anthropic-beta which is additive: values from every input are collected so endpoint defaults don't drop the caller's betas: or the client OAuth beta.

Parameters:

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

Returns:

  • (Hash{String=>String})


435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/anthropic/internal/util.rb', line 435

def normalized_headers(*headers)
  merged = {}
  headers.each do |hdrs|
    hdrs.to_h.each do |key, val|
      k = key.to_s.downcase
      merged[k] = k == "anthropic-beta" && merged.key?(k) ? [*merged[k], *val] : val
    end
  end
  merged.transform_values do |val|
    case val
    in Array
      val.filter_map { _1&.to_s&.strip }.join(", ")
    else
      val&.to_s&.strip
    end
  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)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/anthropic/internal/util.rb', line 83

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})


353
354
355
356
# File 'lib/anthropic/internal/util.rb', line 353

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:



109
110
111
112
113
114
115
116
# File 'lib/anthropic/internal/util.rb', line 109

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)


373
374
375
# File 'lib/anthropic/internal/util.rb', line 373

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)


283
284
285
# File 'lib/anthropic/internal/util.rb', line 283

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>)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/anthropic/internal/util.rb', line 17

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>)


556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/anthropic/internal/util.rb', line 556

def writable_enum(&blk)
  Enumerator.new do |y|
    y.define_singleton_method(:<<) { super(_1.b) }
    y.define_singleton_method(:write) do
      # `IO.copy_stream` reuses its buffer. With Ruby bug #21131, a `dup` or `b` of it changes on the next read.
      self << (String.new(capacity: _1.bytesize) << _1)
      _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])


134
135
136
137
138
139
140
# File 'sig/anthropic/internal/util.rbs', line 134

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)


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

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


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

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