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

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.

Parameters:

  • enum (Enumerable<Object>, nil)


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.

Parameters:

  • input (Object)

Returns:

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


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

Parameters:

  • headers (Hash{String=>String}, Net::HTTPHeader)
  • stream (Enumerable<String>)
  • suppress_error (Boolean) (defaults to: false)

Returns:

  • (Object)

Raises:

  • (JSON::ParserError)


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.

Parameters:

  • query (String, nil)

Returns:

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


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.

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.

Returns:

  • (Object)


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.

Parameters:

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

Returns:

  • (Object, nil)


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.

Parameters:

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

Returns:

  • (Object)


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.

Parameters:

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

Returns:

  • (String, nil)


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.

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

Parameters:

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

Returns:

  • (Enumerable<Object>)


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.

Parameters:

  • path (String, Array<String>)

Returns:

  • (String)


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.

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)


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_secsFloat

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

Parameters:

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

Returns:

  • (Hash{String=>String})


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.

Parameters:

  • url (URI::Generic, String)

Returns:

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


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.

Parameters:

  • uri (URI::Generic)

Returns:

  • (String)


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