Module: XTwitterScraper::Internal::Type::Union Private

Includes:
Converter, Util::SorbetRuntimeSupport
Included in:
Models::ComposeCreateParams::Body, Models::ComposeCreateResponse, Models::Error::Error, Models::X::AccountConnectionAttemptRetrieveResponse, Models::X::AccountCreateResponse
Defined in:
lib/x_twitter_scraper/internal/type/union.rb,
sig/x_twitter_scraper/internal/type/union.rbs

Overview

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.

Examples:

# `account_connection_attempt_retrieve_response` is a `XTwitterScraper::Models::X::AccountConnectionAttemptRetrieveResponse`
case 
when XTwitterScraper::Models::X::AccountConnectionAttemptRetrieveResponse::Pending
  puts(.id)
when XTwitterScraper::Models::X::AccountConnectionAttemptRetrieveResponse::Success
  puts(.object)
when XTwitterScraper::Models::X::AccountConnectionAttemptRetrieveResponse::Failed
  puts(.error)
when XTwitterScraper::Models::X::AccountConnectionAttemptRetrieveResponse::RequiresEmailCode
  puts(.message)
else
  puts()
end
case 
in {status: :pending, id: id, object: object, poll_after_ms: poll_after_ms}
  puts(id)
in {status: :success, id: id, object: object}
  puts(object)
in {status: :failed, id: id, error: error, object: object}
  puts(error)
in {status: :requires_email_code, id: id, message: message}
  puts(message)
else
  puts()
end

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, to_sorbet_type

Methods included from Converter

coerce, dump, inspect, meta_info, new_coerce_state, type_info

Class Method Details

.derefed_variants::Array[[Symbol?, top, ::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.

Returns:

  • (::Array[[Symbol?, top, ::Hash[Symbol, top]]])


14
# File 'sig/x_twitter_scraper/internal/type/union.rbs', line 14

def self.derefed_variants: -> ::Array[[Symbol?, top, ::Hash[Symbol, top]]]

.variants::Array[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.

Returns:

  • (::Array[top])


16
# File 'sig/x_twitter_scraper/internal/type/union.rbs', line 16

def self.variants: -> ::Array[top]

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



142
143
144
# File 'lib/x_twitter_scraper/internal/type/union.rb', line 142

def ==(other)
  XTwitterScraper::Internal::Type::Union === other && other.derefed_variants == derefed_variants
end

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



131
132
133
134
135
# File 'lib/x_twitter_scraper/internal/type/union.rb', line 131

def ===(other)
  known_variants.any? do |_, variant_fn|
    variant_fn.call === other
  end
end

#coerce(value, state:) ⇒ 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.

Tries to efficiently coerce the given value to one of the known variants.

If the value cannot match any of the known variants, the coercion is considered non-viable and returns the original value.

Parameters:

  • value (Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class] :error

    @option state [Integer] :branched

Returns:

  • (Object)


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/x_twitter_scraper/internal/type/union.rb', line 173

def coerce(value, state:)
  if (target = resolve_variant(value))
    return XTwitterScraper::Internal::Type::Converter.coerce(target, value, state: state)
  end

  strictness = state.fetch(:strictness)
  exactness = state.fetch(:exactness)

  alternatives = []
  known_variants.each do |_, variant_fn|
    target = variant_fn.call
    exact = state[:exactness] = {yes: 0, no: 0, maybe: 0}
    state[:branched] += 1

    coerced = XTwitterScraper::Internal::Type::Converter.coerce(target, value, state: state)
    yes, no, maybe = exact.values
    if (no + maybe).zero? || (!strictness && yes.positive?)
      exact.each { exactness[_1] += _2 }
      state[:exactness] = exactness
      return coerced
    elsif maybe.positive?
      alternatives << [[-yes, -maybe, no], exact, coerced]
    end
  end

  case alternatives.sort_by!(&:first)
  in []
    exactness[:no] += 1
    state[:error] = ArgumentError.new("no matching variant for #{value.inspect}")
    value
  in [[_, exact, coerced], *]
    exact.each { exactness[_1] += _2 }
    coerced
  end
    .tap { state[:exactness] = exactness }
ensure
  state[:strictness] = strictness
end

#dump(value, state:) ⇒ 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:

  • value (Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :can_retry

Returns:

  • (Object)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/x_twitter_scraper/internal/type/union.rb', line 221

def dump(value, state:)
  if (target = resolve_variant(value))
    return XTwitterScraper::Internal::Type::Converter.dump(target, value, state: state)
  end

  known_variants.each do
    target = _2.call
    if target === value
      return XTwitterScraper::Internal::Type::Converter.dump(
        target,
        value,
        state: state
      )
    end
  end

  super
end

#hashInteger

Returns:

  • (Integer)


149
# File 'lib/x_twitter_scraper/internal/type/union.rb', line 149

def hash = variants.hash

#inspect(depth: 0) ⇒ 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:

  • depth (Integer) (defaults to: 0)
  • depth: (Integer) (defaults to: 0)

Returns:

  • (String)


265
266
267
268
269
270
271
272
273
274
# File 'lib/x_twitter_scraper/internal/type/union.rb', line 265

def inspect(depth: 0)
  if depth.positive?
    return is_a?(Module) ? super() : self.class.name
  end

  members = variants.map { XTwitterScraper::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
  prefix = is_a?(Module) ? name : self.class.name

  "#{prefix}[#{members.join(' | ')}]"
end

#to_sorbet_typeObject

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:

  • (Object)


243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/x_twitter_scraper/internal/type/union.rb', line 243

def to_sorbet_type
  types = variants.map do
    XTwitterScraper::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1)
  end.uniq
  case types
  in []
    T.noreturn
  in [type]
    type
  else
    T.any(*types)
  end
end

#variantsArray<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.

All of the specified variants for this union.

Returns:

  • (Array<Object>)


61
# File 'lib/x_twitter_scraper/internal/type/union.rb', line 61

def variants = derefed_variants.map { _2 }