apologist 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.fern/metadata.json +13 -0
- data/.fernignore +2 -0
- data/.rubocop.yml +108 -0
- data/CONTRIBUTING.md +120 -0
- data/README.md +169 -0
- data/Rakefile +20 -0
- data/custom.gemspec.rb +17 -0
- data/lib/Apologist/benchmarks/client.rb +160 -0
- data/lib/Apologist/benchmarks/types/benchmark_run_request.rb +39 -0
- data/lib/Apologist/benchmarks/types/benchmark_run_request_content.rb +16 -0
- data/lib/Apologist/benchmarks/types/benchmark_run_request_reasoning_effort.rb +15 -0
- data/lib/Apologist/benchmarks/types/benchmark_run_request_verbosity.rb +16 -0
- data/lib/Apologist/benchmarks/types/get_benchmark_run_request.rb +13 -0
- data/lib/Apologist/benchmarks/types/get_benchmark_run_response.rb +11 -0
- data/lib/Apologist/benchmarks/types/list_benchmark_runs_request.rb +33 -0
- data/lib/Apologist/benchmarks/types/list_benchmark_runs_response.rb +17 -0
- data/lib/Apologist/channels/client.rb +319 -0
- data/lib/Apologist/channels/types/get_discord_channel_status_request.rb +11 -0
- data/lib/Apologist/channels/types/get_discord_channel_status_response.rb +15 -0
- data/lib/Apologist/channels/types/get_instagram_privacy_policy_request.rb +11 -0
- data/lib/Apologist/channels/types/receive_discord_interaction_request.rb +17 -0
- data/lib/Apologist/channels/types/receive_facebook_message_request.rb +13 -0
- data/lib/Apologist/channels/types/receive_telegram_update_request.rb +13 -0
- data/lib/Apologist/channels/types/receive_twilio_message_request.rb +15 -0
- data/lib/Apologist/channels/types/verify_facebook_webhook_request.rb +17 -0
- data/lib/Apologist/channels/types/verify_facebook_webhook_request_hub_mode.rb +13 -0
- data/lib/Apologist/chat/client.rb +345 -0
- data/lib/Apologist/chat/types/feedback_request.rb +13 -0
- data/lib/Apologist/chat/types/flag_request.rb +13 -0
- data/lib/Apologist/chat/types/get_chat_completion_request.rb +11 -0
- data/lib/Apologist/chat/types/get_chat_completion_response.rb +11 -0
- data/lib/Apologist/chat/types/like_request.rb +13 -0
- data/lib/Apologist/chat/types/list_chat_completions_request.rb +45 -0
- data/lib/Apologist/chat/types/list_chat_completions_response.rb +17 -0
- data/lib/Apologist/chat/types/share_request.rb +17 -0
- data/lib/Apologist/client.rb +71 -0
- data/lib/Apologist/corpus/client.rb +244 -0
- data/lib/Apologist/corpus/types/corpus_search_request.rb +17 -0
- data/lib/Apologist/corpus/types/corpus_search_request_filters.rb +25 -0
- data/lib/Apologist/corpus/types/impression_request.rb +17 -0
- data/lib/Apologist/corpus/types/log_corpus_referral_redirect_request.rb +19 -0
- data/lib/Apologist/corpus/types/referral_request.rb +17 -0
- data/lib/Apologist/corpus/types/search_corpus_response.rb +11 -0
- data/lib/Apologist/corpus/types/view_request.rb +17 -0
- data/lib/Apologist/ct_as/client.rb +98 -0
- data/lib/Apologist/ct_as/types/cta_click_request.rb +13 -0
- data/lib/Apologist/ct_as/types/match_ctas_response.rb +11 -0
- data/lib/Apologist/environment.rb +7 -0
- data/lib/Apologist/errors/api_error.rb +8 -0
- data/lib/Apologist/errors/client_error.rb +17 -0
- data/lib/Apologist/errors/redirect_error.rb +8 -0
- data/lib/Apologist/errors/response_error.rb +42 -0
- data/lib/Apologist/errors/server_error.rb +11 -0
- data/lib/Apologist/errors/timeout_error.rb +8 -0
- data/lib/Apologist/evaluators/client.rb +164 -0
- data/lib/Apologist/evaluators/types/evaluate_content_response.rb +11 -0
- data/lib/Apologist/evaluators/types/evaluator_request.rb +31 -0
- data/lib/Apologist/evaluators/types/evaluator_request_content.rb +15 -0
- data/lib/Apologist/evaluators/types/evaluator_request_reasoning_effort.rb +15 -0
- data/lib/Apologist/evaluators/types/evaluator_request_verbosity.rb +16 -0
- data/lib/Apologist/evaluators/types/get_evaluation_request.rb +13 -0
- data/lib/Apologist/evaluators/types/get_evaluation_response.rb +11 -0
- data/lib/Apologist/evaluators/types/list_evaluations_request.rb +35 -0
- data/lib/Apologist/evaluators/types/list_evaluations_response.rb +17 -0
- data/lib/Apologist/internal/errors/constraint_error.rb +10 -0
- data/lib/Apologist/internal/errors/type_error.rb +10 -0
- data/lib/Apologist/internal/http/base_request.rb +51 -0
- data/lib/Apologist/internal/http/raw_client.rb +252 -0
- data/lib/Apologist/internal/iterators/cursor_item_iterator.rb +28 -0
- data/lib/Apologist/internal/iterators/cursor_page_iterator.rb +63 -0
- data/lib/Apologist/internal/iterators/item_iterator.rb +65 -0
- data/lib/Apologist/internal/iterators/offset_item_iterator.rb +30 -0
- data/lib/Apologist/internal/iterators/offset_page_iterator.rb +103 -0
- data/lib/Apologist/internal/json/request.rb +41 -0
- data/lib/Apologist/internal/json/serializable.rb +25 -0
- data/lib/Apologist/internal/multipart/multipart_encoder.rb +141 -0
- data/lib/Apologist/internal/multipart/multipart_form_data.rb +78 -0
- data/lib/Apologist/internal/multipart/multipart_form_data_part.rb +51 -0
- data/lib/Apologist/internal/multipart/multipart_request.rb +40 -0
- data/lib/Apologist/internal/types/array.rb +47 -0
- data/lib/Apologist/internal/types/boolean.rb +34 -0
- data/lib/Apologist/internal/types/enum.rb +56 -0
- data/lib/Apologist/internal/types/hash.rb +36 -0
- data/lib/Apologist/internal/types/model/field.rb +38 -0
- data/lib/Apologist/internal/types/model.rb +208 -0
- data/lib/Apologist/internal/types/type.rb +35 -0
- data/lib/Apologist/internal/types/union.rb +161 -0
- data/lib/Apologist/internal/types/unknown.rb +15 -0
- data/lib/Apologist/internal/types/utils.rb +116 -0
- data/lib/Apologist/internal/url_encoded/request.rb +41 -0
- data/lib/Apologist/shares/client.rb +52 -0
- data/lib/Apologist/shares/types/get_shared_messages_request.rb +11 -0
- data/lib/Apologist/shares/types/get_shared_messages_response.rb +11 -0
- data/lib/Apologist/types/chat_completion_request.rb +11 -0
- data/lib/Apologist/types/chat_completion_request_logprobs.rb +13 -0
- data/lib/Apologist/types/chat_completion_request_metadata.rb +29 -0
- data/lib/Apologist/types/chat_completion_request_reasoning_effort.rb +13 -0
- data/lib/Apologist/types/chat_completion_request_response_format.rb +11 -0
- data/lib/Apologist/types/chat_completion_request_response_format_json_schema.rb +17 -0
- data/lib/Apologist/types/chat_completion_request_response_format_type.rb +15 -0
- data/lib/Apologist/types/chat_completion_request_tool_choice.rb +13 -0
- data/lib/Apologist/types/chat_completion_request_tool_choice_zero.rb +13 -0
- data/lib/Apologist/types/chat_completion_request_verbosity.rb +14 -0
- data/lib/Apologist/types/chat_completion_response.rb +21 -0
- data/lib/Apologist/types/chat_completion_response_choices_item.rb +15 -0
- data/lib/Apologist/types/chat_completion_response_usage.rb +13 -0
- data/lib/Apologist/types/chat_message.rb +11 -0
- data/lib/Apologist/types/chat_message_role.rb +13 -0
- data/lib/Apologist/types/cta_match_request.rb +11 -0
- data/lib/Apologist/types/error.rb +11 -0
- data/lib/Apologist/types/success_response.rb +11 -0
- data/lib/Apologist/types/tag_ref.rb +11 -0
- data/lib/Apologist/types/user.rb +23 -0
- data/lib/Apologist/types/user_flag.rb +18 -0
- data/lib/Apologist/types/webhook_agent_ref.rb +11 -0
- data/lib/Apologist/types/webhook_cta.rb +13 -0
- data/lib/Apologist/types/webhook_evaluation.rb +14 -0
- data/lib/Apologist/types/webhook_event_info.rb +13 -0
- data/lib/Apologist/types/webhook_event_info_key.rb +28 -0
- data/lib/Apologist/types/webhook_named_ref.rb +11 -0
- data/lib/Apologist/types/webhook_notification_ref.rb +12 -0
- data/lib/Apologist/types/webhook_payload.rb +30 -0
- data/lib/Apologist/users/client.rb +193 -0
- data/lib/Apologist/users/types/get_user_request.rb +11 -0
- data/lib/Apologist/users/types/get_user_response.rb +11 -0
- data/lib/Apologist/users/types/list_user_flags_request.rb +13 -0
- data/lib/Apologist/users/types/list_user_flags_response.rb +17 -0
- data/lib/Apologist/users/types/list_users_request.rb +23 -0
- data/lib/Apologist/users/types/list_users_response.rb +17 -0
- data/lib/Apologist/users/types/update_user_response.rb +11 -0
- data/lib/Apologist/users/types/user_update_request.rb +17 -0
- data/lib/Apologist/users/types/user_update_request_tags_item.rb +15 -0
- data/lib/Apologist/version.rb +5 -0
- data/lib/Apologist.rb +131 -0
- data/reference.md +2927 -0
- metadata +183 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apologist
|
|
4
|
+
module Internal
|
|
5
|
+
module Types
|
|
6
|
+
# An array of a specific type
|
|
7
|
+
class Array
|
|
8
|
+
include Apologist::Internal::Types::Type
|
|
9
|
+
|
|
10
|
+
attr_reader :type
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# Instantiates a new `Array` of a given type
|
|
14
|
+
#
|
|
15
|
+
# @param type [Object] The member type of this array
|
|
16
|
+
#
|
|
17
|
+
# @return [Apologist::Internal::Types::Array]
|
|
18
|
+
def [](type)
|
|
19
|
+
new(type)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @api private
|
|
24
|
+
def initialize(type)
|
|
25
|
+
@type = type
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Coerces a value into this array
|
|
29
|
+
#
|
|
30
|
+
# @param value [Object]
|
|
31
|
+
# @option strict [Boolean]
|
|
32
|
+
# @return [::Array]
|
|
33
|
+
def coerce(value, strict: strict?)
|
|
34
|
+
unless value.is_a?(::Array)
|
|
35
|
+
raise Errors::TypeError, "cannot coerce `#{value.class}` to Array<#{type}>" if strict
|
|
36
|
+
|
|
37
|
+
return value
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
value.map do |element|
|
|
41
|
+
Utils.coerce(type, element, strict: strict)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apologist
|
|
4
|
+
module Internal
|
|
5
|
+
module Types
|
|
6
|
+
module Boolean
|
|
7
|
+
extend Apologist::Internal::Types::Union
|
|
8
|
+
|
|
9
|
+
member TrueClass
|
|
10
|
+
member FalseClass
|
|
11
|
+
|
|
12
|
+
# Overrides the base coercion method for enums to allow integer and string values to become booleans
|
|
13
|
+
#
|
|
14
|
+
# @param value [Object]
|
|
15
|
+
# @option strict [Boolean]
|
|
16
|
+
# @return [Object]
|
|
17
|
+
def self.coerce(value, strict: strict?)
|
|
18
|
+
case value
|
|
19
|
+
when TrueClass, FalseClass
|
|
20
|
+
return value
|
|
21
|
+
when Integer
|
|
22
|
+
return value == 1
|
|
23
|
+
when String
|
|
24
|
+
return %w[1 true].include?(value)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
raise Errors::TypeError, "cannot coerce `#{value.class}` to Boolean" if strict
|
|
28
|
+
|
|
29
|
+
value
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apologist
|
|
4
|
+
module Internal
|
|
5
|
+
module Types
|
|
6
|
+
# Module for defining enums
|
|
7
|
+
module Enum
|
|
8
|
+
include Type
|
|
9
|
+
|
|
10
|
+
# @api private
|
|
11
|
+
#
|
|
12
|
+
# @return [Array<Object>]
|
|
13
|
+
def values
|
|
14
|
+
@values ||= constants.map { |c| const_get(c) }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @api private
|
|
18
|
+
def finalize!
|
|
19
|
+
values
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @api private
|
|
23
|
+
def strict?
|
|
24
|
+
@strict ||= false
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @api private
|
|
28
|
+
def strict!
|
|
29
|
+
@strict = true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def coerce(value, strict: strict?)
|
|
33
|
+
coerced_value = Utils.coerce(Symbol, value)
|
|
34
|
+
|
|
35
|
+
return coerced_value if values.include?(coerced_value)
|
|
36
|
+
|
|
37
|
+
raise Errors::TypeError, "`#{value}` not in enum #{self}" if strict
|
|
38
|
+
|
|
39
|
+
value
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Parse JSON string and coerce to the enum value
|
|
43
|
+
#
|
|
44
|
+
# @param str [String] JSON string to parse
|
|
45
|
+
# @return [String] The enum value
|
|
46
|
+
def load(str)
|
|
47
|
+
coerce(::JSON.parse(str))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def inspect
|
|
51
|
+
"#{name}[#{values.join(", ")}]"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apologist
|
|
4
|
+
module Internal
|
|
5
|
+
module Types
|
|
6
|
+
class Hash
|
|
7
|
+
include Type
|
|
8
|
+
|
|
9
|
+
attr_reader :key_type, :value_type
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def [](key_type, value_type)
|
|
13
|
+
new(key_type, value_type)
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def initialize(key_type, value_type)
|
|
18
|
+
@key_type = key_type
|
|
19
|
+
@value_type = value_type
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def coerce(value, strict: strict?)
|
|
23
|
+
unless value.is_a?(::Hash)
|
|
24
|
+
raise Errors::TypeError, "not hash" if strict
|
|
25
|
+
|
|
26
|
+
return value
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
value.to_h do |k, v|
|
|
30
|
+
[Utils.coerce(key_type, k, strict: strict), Utils.coerce(value_type, v, strict: strict)]
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apologist
|
|
4
|
+
module Internal
|
|
5
|
+
module Types
|
|
6
|
+
class Model
|
|
7
|
+
# Definition of a field on a model
|
|
8
|
+
class Field
|
|
9
|
+
SENSITIVE_FIELD_NAMES = %i[
|
|
10
|
+
password secret token api_key apikey access_token refresh_token
|
|
11
|
+
client_secret client_id credential bearer authorization
|
|
12
|
+
].freeze
|
|
13
|
+
|
|
14
|
+
attr_reader :name, :type, :optional, :nullable, :api_name, :value, :default
|
|
15
|
+
|
|
16
|
+
def initialize(name:, type:, optional: false, nullable: false, api_name: nil, value: nil, default: nil)
|
|
17
|
+
@name = name.to_sym
|
|
18
|
+
@type = type
|
|
19
|
+
@optional = optional
|
|
20
|
+
@nullable = nullable
|
|
21
|
+
@api_name = api_name || name.to_s
|
|
22
|
+
@value = value
|
|
23
|
+
@default = default
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def literal?
|
|
27
|
+
!value.nil?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def sensitive?
|
|
31
|
+
SENSITIVE_FIELD_NAMES.include?(@name) ||
|
|
32
|
+
SENSITIVE_FIELD_NAMES.any? { |sensitive| @name.to_s.include?(sensitive.to_s) }
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apologist
|
|
4
|
+
module Internal
|
|
5
|
+
module Types
|
|
6
|
+
# @abstract
|
|
7
|
+
#
|
|
8
|
+
# An abstract model that all data objects will inherit from
|
|
9
|
+
class Model
|
|
10
|
+
include Type
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# The defined fields for this model
|
|
14
|
+
#
|
|
15
|
+
# @api private
|
|
16
|
+
#
|
|
17
|
+
# @return [Hash<Symbol, Field>]
|
|
18
|
+
def fields
|
|
19
|
+
@fields ||= if self < Apologist::Internal::Types::Model
|
|
20
|
+
superclass.fields.dup
|
|
21
|
+
else
|
|
22
|
+
{}
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Any extra fields that have been created from instantiation
|
|
27
|
+
#
|
|
28
|
+
# @api private
|
|
29
|
+
#
|
|
30
|
+
# @return [Hash<Symbol, Field>]
|
|
31
|
+
def extra_fields
|
|
32
|
+
@extra_fields ||= {}
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Define a new field on this model
|
|
36
|
+
#
|
|
37
|
+
# @param name [Symbol] The name of the field
|
|
38
|
+
# @param type [Class] Type of the field
|
|
39
|
+
# @option optional [Boolean] If it is an optional field
|
|
40
|
+
# @option nullable [Boolean] If it is a nullable field
|
|
41
|
+
# @option api_name [Symbol, String] Name in the API of this field. When serializing/deserializing, will use
|
|
42
|
+
# this field name
|
|
43
|
+
# @return [void]
|
|
44
|
+
def field(name, type, optional: false, nullable: false, api_name: nil, default: nil)
|
|
45
|
+
add_field_definition(name: name, type: type, optional: optional, nullable: nullable, api_name: api_name,
|
|
46
|
+
default: default)
|
|
47
|
+
|
|
48
|
+
define_accessor(name)
|
|
49
|
+
define_setter(name)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Define a new literal for this model
|
|
53
|
+
#
|
|
54
|
+
# @param name [Symbol]
|
|
55
|
+
# @param value [Object]
|
|
56
|
+
# @option api_name [Symbol, String]
|
|
57
|
+
# @return [void]
|
|
58
|
+
def literal(name, value, api_name: nil)
|
|
59
|
+
add_field_definition(name: name, type: value.class, optional: false, nullable: false, api_name: api_name,
|
|
60
|
+
value: value)
|
|
61
|
+
|
|
62
|
+
define_accessor(name)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Adds a new field definition into the class's fields registry
|
|
66
|
+
#
|
|
67
|
+
# @api private
|
|
68
|
+
#
|
|
69
|
+
# @param name [Symbol]
|
|
70
|
+
# @param type [Class]
|
|
71
|
+
# @option optional [Boolean]
|
|
72
|
+
# @return [void]
|
|
73
|
+
private def add_field_definition(name:, type:, optional:, nullable:, api_name:, default: nil, value: nil)
|
|
74
|
+
fields[name.to_sym] =
|
|
75
|
+
Field.new(name: name, type: type, optional: optional, nullable: nullable, api_name: api_name,
|
|
76
|
+
value: value, default: default)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Adds a new field definition into the class's extra fields registry
|
|
80
|
+
#
|
|
81
|
+
# @api private
|
|
82
|
+
#
|
|
83
|
+
# @param name [Symbol]
|
|
84
|
+
# @param type [Class]
|
|
85
|
+
# @option required [Boolean]
|
|
86
|
+
# @option optional [Boolean]
|
|
87
|
+
# @return [void]
|
|
88
|
+
def add_extra_field_definition(name:, type:)
|
|
89
|
+
return if extra_fields.key?(name.to_sym)
|
|
90
|
+
|
|
91
|
+
extra_fields[name.to_sym] = Field.new(name: name, type: type, optional: true, nullable: false)
|
|
92
|
+
|
|
93
|
+
define_accessor(name)
|
|
94
|
+
define_setter(name)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# @api private
|
|
98
|
+
private def define_accessor(name)
|
|
99
|
+
method_name = name.to_sym
|
|
100
|
+
|
|
101
|
+
define_method(method_name) do
|
|
102
|
+
@data[name]
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# @api private
|
|
107
|
+
private def define_setter(name)
|
|
108
|
+
method_name = :"#{name}="
|
|
109
|
+
|
|
110
|
+
define_method(method_name) do |val|
|
|
111
|
+
@data[name] = val
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def coerce(value, strict: (respond_to?(:strict?) ? strict? : false)) # rubocop:disable Lint/UnusedMethodArgument
|
|
116
|
+
return value if value.is_a?(self)
|
|
117
|
+
|
|
118
|
+
return value unless value.is_a?(::Hash)
|
|
119
|
+
|
|
120
|
+
new(value)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def load(str)
|
|
124
|
+
coerce(::JSON.parse(str, symbolize_names: true))
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def ===(instance)
|
|
128
|
+
instance.class.ancestors.include?(self)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Creates a new instance of this model
|
|
133
|
+
# TODO: Should all this logic be in `#coerce` instead?
|
|
134
|
+
#
|
|
135
|
+
# @param values [Hash]
|
|
136
|
+
# @option strict [Boolean]
|
|
137
|
+
# @return [self]
|
|
138
|
+
def initialize(values = {})
|
|
139
|
+
@data = {}
|
|
140
|
+
|
|
141
|
+
values = Utils.symbolize_keys(values.dup)
|
|
142
|
+
|
|
143
|
+
self.class.fields.each do |field_name, field|
|
|
144
|
+
value = values.delete(field.api_name.to_sym) || values.delete(field.api_name) || values.delete(field_name)
|
|
145
|
+
|
|
146
|
+
field_value = value || (if field.literal?
|
|
147
|
+
field.value
|
|
148
|
+
elsif field.default
|
|
149
|
+
field.default
|
|
150
|
+
end)
|
|
151
|
+
|
|
152
|
+
@data[field_name] = Utils.coerce(field.type, field_value)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Any remaining values in the input become extra fields
|
|
156
|
+
values.each do |name, value|
|
|
157
|
+
self.class.add_extra_field_definition(name: name, type: value.class)
|
|
158
|
+
|
|
159
|
+
@data[name.to_sym] = value
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def to_h
|
|
164
|
+
result = self.class.fields.merge(self.class.extra_fields).each_with_object({}) do |(name, field), acc|
|
|
165
|
+
# If there is a value present in the data, use that value
|
|
166
|
+
# If there is a `nil` value present in the data, and it is optional but NOT nullable, exclude key altogether
|
|
167
|
+
# If there is a `nil` value present in the data, and it is optional and nullable, use the nil value
|
|
168
|
+
|
|
169
|
+
value = @data[name]
|
|
170
|
+
|
|
171
|
+
next if value.nil? && field.optional && !field.nullable
|
|
172
|
+
|
|
173
|
+
if value.is_a?(::Array)
|
|
174
|
+
value = value.map { |item| item.respond_to?(:to_h) ? item.to_h : item }
|
|
175
|
+
elsif value.respond_to?(:to_h)
|
|
176
|
+
value = value.to_h
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
acc[field.api_name] = value
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Inject union discriminant if this instance was coerced from a discriminated union
|
|
183
|
+
# and the discriminant key is not already present in the result
|
|
184
|
+
discriminant_key = instance_variable_get(:@_fern_union_discriminant_key)
|
|
185
|
+
discriminant_value = instance_variable_get(:@_fern_union_discriminant_value)
|
|
186
|
+
result[discriminant_key] = discriminant_value if discriminant_key && discriminant_value && !result.key?(discriminant_key)
|
|
187
|
+
|
|
188
|
+
result
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def ==(other)
|
|
192
|
+
self.class == other.class && to_h == other.to_h
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# @return [String]
|
|
196
|
+
def inspect
|
|
197
|
+
attrs = @data.map do |name, value|
|
|
198
|
+
field = self.class.fields[name] || self.class.extra_fields[name]
|
|
199
|
+
display_value = field&.sensitive? ? "[REDACTED]" : value.inspect
|
|
200
|
+
"#{name}=#{display_value}"
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
"#<#{self.class.name}:0x#{object_id&.to_s(16)} #{attrs.join(" ")}>"
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apologist
|
|
4
|
+
module Internal
|
|
5
|
+
module Types
|
|
6
|
+
# @abstract
|
|
7
|
+
module Type
|
|
8
|
+
include Apologist::Internal::JSON::Serializable
|
|
9
|
+
|
|
10
|
+
# Coerces a value to this type
|
|
11
|
+
#
|
|
12
|
+
# @param value [unknown]
|
|
13
|
+
# @option strict [Boolean] If we should strictly coerce this value
|
|
14
|
+
def coerce(value, strict: strict?)
|
|
15
|
+
raise NotImplementedError
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Returns if strictness is on for this type, defaults to `false`
|
|
19
|
+
#
|
|
20
|
+
# @return [Boolean]
|
|
21
|
+
def strict?
|
|
22
|
+
@strict ||= false
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Enable strictness by default for this type
|
|
26
|
+
#
|
|
27
|
+
# @return [void]
|
|
28
|
+
def strict!
|
|
29
|
+
@strict = true
|
|
30
|
+
self
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Apologist
|
|
4
|
+
module Internal
|
|
5
|
+
module Types
|
|
6
|
+
# Define a union between two types
|
|
7
|
+
module Union
|
|
8
|
+
include Apologist::Internal::Types::Type
|
|
9
|
+
|
|
10
|
+
def members
|
|
11
|
+
@members ||= []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Add a member to this union
|
|
15
|
+
#
|
|
16
|
+
# @param type [Object]
|
|
17
|
+
# @option key [Symbol, String]
|
|
18
|
+
# @return [void]
|
|
19
|
+
def member(type, key: nil)
|
|
20
|
+
members.push([key, Utils.wrap_type(type)])
|
|
21
|
+
self
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def type_member?(type)
|
|
25
|
+
members.any? { |_key, type_fn| type == type_fn.call }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Set the discriminant for this union
|
|
29
|
+
#
|
|
30
|
+
# @param key [Symbol, String]
|
|
31
|
+
# @return [void]
|
|
32
|
+
def discriminant(key)
|
|
33
|
+
@discriminant = key
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @api private
|
|
37
|
+
private def discriminated?
|
|
38
|
+
!@discriminant.nil?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Check if value matches a type, handling type wrapper instances
|
|
42
|
+
# (Internal::Types::Hash and Internal::Types::Array instances)
|
|
43
|
+
#
|
|
44
|
+
# @param value [Object]
|
|
45
|
+
# @param member_type [Object]
|
|
46
|
+
# @return [Boolean]
|
|
47
|
+
private def type_matches?(value, member_type)
|
|
48
|
+
case member_type
|
|
49
|
+
when Apologist::Internal::Types::Hash
|
|
50
|
+
value.is_a?(::Hash)
|
|
51
|
+
when Apologist::Internal::Types::Array
|
|
52
|
+
value.is_a?(::Array)
|
|
53
|
+
when Class, Module
|
|
54
|
+
value.is_a?(member_type)
|
|
55
|
+
else
|
|
56
|
+
false
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Resolves the type of a value to be one of the members
|
|
61
|
+
#
|
|
62
|
+
# @param value [Object]
|
|
63
|
+
# @return [Class]
|
|
64
|
+
private def resolve_member(value)
|
|
65
|
+
if discriminated? && value.is_a?(::Hash)
|
|
66
|
+
# Try both symbol and string keys for the discriminant
|
|
67
|
+
discriminant_value = value.fetch(@discriminant, nil) || value.fetch(@discriminant.to_s, nil)
|
|
68
|
+
|
|
69
|
+
return if discriminant_value.nil?
|
|
70
|
+
|
|
71
|
+
# Convert to string for consistent comparison
|
|
72
|
+
discriminant_str = discriminant_value.to_s
|
|
73
|
+
|
|
74
|
+
# First try exact match
|
|
75
|
+
members_hash = members.to_h
|
|
76
|
+
result = members_hash[discriminant_str]&.call
|
|
77
|
+
return result if result
|
|
78
|
+
|
|
79
|
+
# Try case-insensitive match as fallback
|
|
80
|
+
discriminant_lower = discriminant_str.downcase
|
|
81
|
+
matching_keys = members_hash.keys.select { |k| k.to_s.downcase == discriminant_lower }
|
|
82
|
+
|
|
83
|
+
# Only use case-insensitive match if exactly one key matches (avoid ambiguity)
|
|
84
|
+
return members_hash[matching_keys.first]&.call if matching_keys.length == 1
|
|
85
|
+
|
|
86
|
+
nil
|
|
87
|
+
else
|
|
88
|
+
# First try exact type matching
|
|
89
|
+
result = members.find do |_key, mem|
|
|
90
|
+
member_type = Utils.unwrap_type(mem)
|
|
91
|
+
type_matches?(value, member_type)
|
|
92
|
+
end&.last&.call
|
|
93
|
+
|
|
94
|
+
return result if result
|
|
95
|
+
|
|
96
|
+
# For Hash values, try to coerce into Model member types
|
|
97
|
+
if value.is_a?(::Hash)
|
|
98
|
+
members.find do |_key, mem|
|
|
99
|
+
member_type = Utils.unwrap_type(mem)
|
|
100
|
+
# Check if member_type is a Model class
|
|
101
|
+
next unless member_type.is_a?(Class) && member_type <= Model
|
|
102
|
+
|
|
103
|
+
# Try to coerce the hash into this model type with strict mode
|
|
104
|
+
begin
|
|
105
|
+
candidate = Utils.coerce(member_type, value, strict: true)
|
|
106
|
+
|
|
107
|
+
# Validate that all required (non-optional) fields are present
|
|
108
|
+
# This ensures undiscriminated unions properly distinguish between member types
|
|
109
|
+
member_type.fields.each do |field_name, field|
|
|
110
|
+
raise Errors::TypeError, "Required field `#{field_name}` missing for union member #{member_type.name}" if candidate.instance_variable_get(:@data)[field_name].nil? && !field.optional
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
true
|
|
114
|
+
rescue Errors::TypeError
|
|
115
|
+
false
|
|
116
|
+
end
|
|
117
|
+
end&.last&.call
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def coerce(value, strict: strict?)
|
|
123
|
+
type = resolve_member(value)
|
|
124
|
+
|
|
125
|
+
unless type
|
|
126
|
+
return value unless strict
|
|
127
|
+
|
|
128
|
+
if discriminated?
|
|
129
|
+
raise Errors::TypeError,
|
|
130
|
+
"value of type `#{value.class}` not member of union #{self}"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
raise Errors::TypeError, "could not resolve to member of union #{self}"
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
coerced = Utils.coerce(type, value, strict: strict)
|
|
137
|
+
|
|
138
|
+
# For discriminated unions, store the discriminant info on the coerced instance
|
|
139
|
+
# so it can be injected back during serialization (to_h)
|
|
140
|
+
if discriminated? && value.is_a?(::Hash) && coerced.is_a?(Model)
|
|
141
|
+
discriminant_value = value.fetch(@discriminant, nil) || value.fetch(@discriminant.to_s, nil)
|
|
142
|
+
if discriminant_value
|
|
143
|
+
coerced.instance_variable_set(:@_fern_union_discriminant_key, @discriminant.to_s)
|
|
144
|
+
coerced.instance_variable_set(:@_fern_union_discriminant_value, discriminant_value)
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
coerced
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Parse JSON string and coerce to the correct union member type
|
|
152
|
+
#
|
|
153
|
+
# @param str [String] JSON string to parse
|
|
154
|
+
# @return [Object] Coerced value matching a union member
|
|
155
|
+
def load(str)
|
|
156
|
+
coerce(::JSON.parse(str, symbolize_names: true))
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|