class SlackResources::Generator::TypeDetection

Constants

AMBIENT_PROPERTIES
ARRAY_PROPERTIES
ARRAY_PROPERTIES_TYPE_MAP
BOOLEAN
CONST_TYPES
DEFAULT_TYPES
DIRECT_STRING_PROPERTIES
MULTIPLE_EXAMPLES
SPECIAL_TYPE
STRING_ID_PROPERTIES
STRING_ID_PROPERTIES_MAP
TOKENS_REVOKED_ARRAY_PROPERTIES
USER_COUNT_PROPERTIES

Public Class Methods

default_type?(type) click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 90
def default_type?(type)
  DEFAULT_TYPES.include?(type)
end
new(to_schema_instance:, parent_key:, prop_name:, value:, container:, preset:) click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 95
def initialize(to_schema_instance:, parent_key:, prop_name:, value:, container:, preset:)
  @to_schema = to_schema_instance
  @parent_key = parent_key
  @prop_name = prop_name
  @value = value
  @container = container
  @preset = preset
end

Public Instance Methods

execute!() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 104
def execute! # rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
  case
  when root_schema? && constantable?
    { 'const' => @value }
  when item_schema? && type?
    define_string("#{root_type_prefix}_#{@parent_key}_type")

  when string_id?
    define_string_id

  when emoji_list?
    define_string('emoji_name')
    '[]emoji_name'
  when emoji_alternative?
    define_string('emoji_name')

  when array?
    define_array_type

  when user_count?
    'user_count'
  when time_integer?
    'time_integer'
  when boolean?
    'boolean'
  when direct_string? || (on_url_verification? && @prop_name == 'token')
    'string'
  when timestamp?
    'timestamp'

  else
    define_default_type(normalized_prop_name, @value, @container, const_type?)
  end
end

Private Instance Methods

ambient?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 185
def ambient?
  AMBIENT_PROPERTIES.include?(@prop_name)
end
array?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 205
def array?
  ARRAY_PROPERTIES.include?(@prop_name) || tokens_revoked_array?
end
boolean?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 193
def boolean?
  BOOLEAN.include?(@value)
end
const_type?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 165
def const_type?
  CONST_TYPES.include?(@prop_name) || @prop_name.match(/_type$/)
end
constantable?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 173
def constantable?
  const_type? && (@value.is_a?(String) || @value.is_a?(Integer))
end
default_type?(type) click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 229
def default_type?(type)
  DEFAULT_TYPES.include?(type)
end
define_array_type() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 249
def define_array_type
  type = ARRAY_PROPERTIES_TYPE_MAP[@prop_name]
  array_type = "[]#{type}"

  return array_type if default_type?(type)
  return define_string(type) && array_type unless @value.is_a?(Hash)

  to_child_schema(
    prop_name: type,
    example: @value.first,
    key: @prop_name,
    parent: @container
  )

  array_type
end
define_string_id() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 266
def define_string_id
  type = STRING_ID_PROPERTIES_MAP[@prop_name] || @prop_name
  define_string(type)
end
direct_string?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 217
def direct_string?
  DIRECT_STRING_PROPERTIES.include?(@prop_name)
end
emoji_alternative?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 177
def emoji_alternative?
  @prop_name == 'reaction'
end
emoji_list?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 233
def emoji_list?
  on_emoji_changed? && @prop_name == 'names'
end
float_string?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 201
def float_string?
  @value.respond_to?(:to_f) && @value == @value.to_f.to_s
end
item_schema?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 181
def item_schema?
  @parent_key == 'item'
end
normalized_prop_name() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 153
def normalized_prop_name
  if ambient?
    root_schema? ? "#{root_type_prefix}_#{@prop_name}" : "#{@parent_key}_#{@prop_name}"
  else
    @prop_name
  end
end
on_emoji_changed?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 245
def on_emoji_changed?
  root_type == 'emoji_changed'
end
on_tokens_revoked?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 237
def on_tokens_revoked?
  root_type == 'tokens_revoked'
end
on_url_verification?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 241
def on_url_verification?
  root_type == 'url_verification'
end
string?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 169
def string?
  @value.is_a?(String)
end
string_id?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 221
def string_id?
  @prop_name.match(/_id$/) || (string_value? && STRING_ID_PROPERTIES.include?(@prop_name))
end
string_value?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 225
def string_value?
  @value.nil? || @value.is_a?(String)
end
time_integer?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 189
def time_integer?
  @value.is_a?(Integer)
end
timestamp?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 197
def timestamp?
  @prop_name == 'latest' || @prop_name.match(/.+_ts$/) || float_string?
end
tokens_revoked_array?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 209
def tokens_revoked_array?
  on_tokens_revoked? && TOKENS_REVOKED_ARRAY_PROPERTIES.include?(@prop_name)
end
type?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 161
def type?
  @prop_name == 'type'
end
user_count?() click to toggle source
# File lib/slack_resources/generator/event_api/type_detection.rb, line 213
def user_count?
  USER_COUNT_PROPERTIES.include?(@prop_name)
end