Module: FinchAPI::Internal::Util Private
- Extended by:
- SorbetRuntimeSupport
- Defined in:
- lib/finch_api/internal/util.rb
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
- 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.
%r{^application/(?:vnd(?:\.[^.]+)*\+)?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.
%r{^application/(:?x-(?:n|l)djson)|(:?(?:x-)?jsonl)}
Class Method Summary collapse
- .arch ⇒ String private
- .chain_fused(enum, &blk) {|| ... } ⇒ Enumerable<Object> private
- .close_fused!(enum) ⇒ Object private
- .coerce_boolean(input) ⇒ Boolean, Object private
- .coerce_boolean!(input) ⇒ Boolean? private
- .coerce_float(input) ⇒ Float, Object private
- .coerce_hash(input) ⇒ Hash{Object=>Object}, Object private
- .coerce_hash!(input) ⇒ Hash{Object=>Object}? private
- .coerce_integer(input) ⇒ Integer, Object private
-
.decode_content(headers, stream:, suppress_error: false) ⇒ Object
private
Assumes each chunk in stream has ‘Encoding::BINARY`.
-
.decode_lines(enum) ⇒ Enumerable<String>
private
Assumes Strings have been forced into having ‘Encoding::BINARY`.
- .decode_query(query) ⇒ Hash{String=>Array<String>} private
- .decode_sse(lines) ⇒ Enumerable<Hash{Symbol=>Object}> private
-
.deep_merge(*values, sentinel: nil, concat: false) ⇒ Object
private
Recursively merge one hash with another.
- .dig(data, pick, &blk) ⇒ Object? private
- .encode_content(headers, body) ⇒ Object private
- .encode_query(query) ⇒ String? private
- .force_charset!(content_type, text:) ⇒ Object private
- .fused_enum(enum, external: false, &close) ⇒ Enumerable<Object> private
- .interpolate_path(path) ⇒ String private
- .join_parsed_uri(lhs, rhs) ⇒ URI::Generic private
- .monotonic_secs ⇒ Float private
- .normalized_headers(*headers) ⇒ Hash{String=>String} private
- .os ⇒ String private
- .parse_uri(url) ⇒ Hash{Symbol=>String, Integer, nil} private
- .primitive?(input) ⇒ Boolean private
- .unparse_uri(parsed) ⇒ URI::Generic private
- .uri_origin(uri) ⇒ String private
- .writable_enum(&blk) {|| ... } ⇒ Enumerable<String> private
Methods included from SorbetRuntimeSupport
const_missing, define_sorbet_constant!
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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/finch_api/internal/util.rb', line 16 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) {|| ... } ⇒ 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.
713 714 715 716 |
# File 'lib/finch_api/internal/util.rb', line 713 def chain_fused(enum, &blk) iter = Enumerator.new { blk.call(_1) } fused_enum(iter) { close_fused!(enum) } end |
.close_fused!(enum) ⇒ 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.
698 699 700 701 702 703 704 |
# File 'lib/finch_api/internal/util.rb', line 698 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.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/finch_api/internal/util.rb', line 74 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.
91 92 93 94 95 96 97 98 |
# File 'lib/finch_api/internal/util.rb', line 91 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.
114 115 116 |
# File 'lib/finch_api/internal/util.rb', line 114 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.
123 124 125 126 127 128 129 130 |
# File 'lib/finch_api/internal/util.rb', line 123 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.
138 139 140 141 142 143 144 145 146 |
# File 'lib/finch_api/internal/util.rb', line 138 def coerce_hash!(input) case coerce_hash(input) in Hash | nil => coerced coerced else = "Expected a #{Hash} or #{FinchAPI::Internal::Type::BaseModel}, got #{data.inspect}" raise ArgumentError.new() 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.
105 106 107 |
# File 'lib/finch_api/internal/util.rb', line 105 def coerce_integer(input) Integer(input, exception: false) || input end |
.decode_content(headers, stream:, suppress_error: 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`.
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'lib/finch_api/internal/util.rb', line 636 def decode_content(headers, stream:, suppress_error: false) case (content_type = headers["content-type"]) in FinchAPI::Internal::Util::JSON_CONTENT json = stream.to_a.join begin JSON.parse(json, symbolize_names: true) rescue JSON::ParserError => e raise e unless suppress_error json end in FinchAPI::Internal::Util::JSONL_CONTENT lines = decode_lines(stream) chain_fused(lines) do |y| lines.each { y << JSON.parse(_1, symbolize_names: true) } end in %r{^text/event-stream} 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.
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 |
# File 'lib/finch_api/internal/util.rb', line 730 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.
257 258 259 |
# File 'lib/finch_api/internal/util.rb', line 257 def decode_query(query) CGI.parse(query.to_s) 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.
html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream
Assumes that ‘lines` has been decoded with `#decode_lines`.
768 769 770 771 772 773 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 |
# File 'lib/finch_api/internal/util.rb', line 768 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_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.
180 181 182 183 184 185 186 187 188 189 |
# File 'lib/finch_api/internal/util.rb', line 180 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 |
.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.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/finch_api/internal/util.rb', line 198 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.
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 |
# File 'lib/finch_api/internal/util.rb', line 585 def encode_content(headers, body) content_type = headers["content-type"] case [content_type, body] in [FinchAPI::Internal::Util::JSON_CONTENT, Hash | Array | -> { primitive?(_1) }] [headers, JSON.generate(body)] in [FinchAPI::Internal::Util::JSONL_CONTENT, Enumerable] unless body.is_a?(FinchAPI::Internal::Type::FileInput) [headers, body.lazy.map { JSON.generate(_1) }] in [%r{^multipart/form-data}, Hash | FinchAPI::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] in [_, FinchAPI::FilePart] [headers, body.content] else [headers, body] end 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.
266 267 268 |
# File 'lib/finch_api/internal/util.rb', line 266 def encode_query(query) query.to_h.empty? ? nil : URI.encode_www_form(query) end |
.force_charset!(content_type, text:) ⇒ 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.
www.iana.org/assignments/character-sets/character-sets.xhtml
613 614 615 616 617 618 619 620 621 622 623 624 |
# File 'lib/finch_api/internal/util.rb', line 613 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.
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
# File 'lib/finch_api/internal/util.rb', line 672 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.
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/finch_api/internal/util.rb', line 238 def interpolate_path(path) case path in String path in [] "" in [String => p, *interpolations] encoded = interpolations.map { ERB::Util.url_encode(_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.
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/finch_api/internal/util.rb', line 328 def join_parsed_uri(lhs, rhs) base_path, base_query = lhs.fetch_values(:path, :query) slashed = base_path.end_with?("/") ? base_path : "#{base_path}/" parsed_path, parsed_query = parse_uri(rhs.fetch(:path)).fetch_values(:path, :query) override = URI::Generic.build(**rhs.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.
10 |
# File 'lib/finch_api/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.
354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/finch_api/internal/util.rb', line 354 def normalized_headers(*headers) {}.merge(*headers.compact).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.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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/finch_api/internal/util.rb', line 34 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.
277 278 279 280 |
# File 'lib/finch_api/internal/util.rb', line 277 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.
60 61 62 63 64 65 66 67 |
# File 'lib/finch_api/internal/util.rb', line 60 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.
297 298 299 |
# File 'lib/finch_api/internal/util.rb', line 297 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.
229 230 231 |
# File 'lib/finch_api/internal/util.rb', line 229 def uri_origin(uri) "#{uri.scheme}://#{uri.host}#{uri.port == uri.default_port ? '' : ":#{uri.port}"}" end |
.writable_enum(&blk) {|| ... } ⇒ 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.
457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/finch_api/internal/util.rb', line 457 def writable_enum(&blk) Enumerator.new do |y| buf = String.new y.define_singleton_method(:write) do self << buf.replace(_1) buf.bytesize end blk.call(y) end end |