Module: Pangea::Internal::Util Private
- Defined in:
- lib/pangea/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
Classes: ReadIOAdapter, SerializationAdapter
Class Method Summary collapse
- .close_fused!(enum) ⇒ Object private
- .coerce_hash(input) ⇒ Hash{Object=>Object}, Object private
-
.decode_content(headers, stream:, suppress_error: false) ⇒ Object
private
Assumes each chunk in stream has ‘Encoding::BINARY`.
- .decode_query(query) ⇒ Hash{String=>Array<String>} private
-
.deep_merge(*values, sentinel: nil, concat: false) ⇒ Object
private
Recursively merge one hash with another.
- .dig(data, pick, sentinel = nil, &blk) ⇒ Object? private
- .encode_content(headers, body) ⇒ Object private
- .encode_query(query) ⇒ String? 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
- .parse_uri(url) ⇒ Hash{Symbol=>String, Integer, nil} private
- .uri_origin(uri) ⇒ String private
Class Method Details
.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.
16 17 18 19 20 21 22 |
# File 'lib/pangea/internal/util.rb', line 16 def close_fused!(enum) return unless enum.is_a?(Enumerator) # rubocop:disable Lint/UnreachableLoop enum.rewind.each { break } # rubocop:enable Lint/UnreachableLoop 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.
29 30 31 32 33 34 35 36 |
# File 'lib/pangea/internal/util.rb', line 29 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 |
.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`.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pangea/internal/util.rb', line 48 def decode_content(headers, stream:, suppress_error: false) case (content_type = headers["content-type"]) in %r{^application/(?:vnd\.api\+)?json} json = stream.to_a.join begin JSON.parse(json, symbolize_names: true) rescue JSON::ParserError => e raise e unless suppress_error json end in %r{^application/(?:x-)?jsonl} 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_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.
78 79 80 |
# File 'lib/pangea/internal/util.rb', line 78 def decode_query(query) CGI.parse(query.to_s) 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.
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/pangea/internal/util.rb', line 112 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, sentinel = nil, &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.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/pangea/internal/util.rb', line 131 def dig(data, pick, sentinel = nil, &blk) case [data, pick, blk] in [_, nil, nil] data in [Hash, Symbol, _] | [Array, Integer, _] blk.nil? ? data.fetch(pick, sentinel) : data.fetch(pick, &blk) 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.nil? ? sentinel : blk.call end end in _ blk.nil? ? sentinel : 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.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/pangea/internal/util.rb', line 159 def encode_content(headers, body) content_type = headers["content-type"] body = body.inner if body.is_a?(Pangea::Internal::Util::SerializationAdapter) case [content_type, body] in [%r{^application/(?:vnd\.api\+)?json}, Hash | Array | -> { primitive?(_1) }] [headers, JSON.generate(body)] in [%r{^application/(?:x-)?jsonl}, Enumerable] unless body.is_a?(StringIO) || body.is_a?(IO) [headers, body.lazy.map { JSON.generate(_1) }] in [%r{^multipart/form-data}, Hash | Pathname | StringIO | IO] 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] 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.
186 187 188 |
# File 'lib/pangea/internal/util.rb', line 186 def encode_query(query) query.to_h.empty? ? nil : URI.encode_www_form(query) 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.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/pangea/internal/util.rb', line 199 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.
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/pangea/internal/util.rb', line 227 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.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/pangea/internal/util.rb', line 266 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/pangea/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.
290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/pangea/internal/util.rb', line 290 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 |
.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.
308 309 310 311 |
# File 'lib/pangea/internal/util.rb', line 308 def parse_uri(url) parsed = URI::Generic.component.zip(URI.split(url)).to_h {**parsed, query: decode_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.
318 319 320 |
# File 'lib/pangea/internal/util.rb', line 318 def uri_origin(uri) "#{uri.scheme}://#{uri.host}#{uri.port == uri.default_port ? '' : ":#{uri.port}"}" end |