ding_sdk 0.3.0
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/lib/ding/lookup.rb +64 -0
- data/lib/ding/models/operations/check.rb +39 -0
- data/lib/ding/models/operations/create_autentication.rb +39 -0
- data/lib/ding/models/operations/lookup.rb +56 -0
- data/lib/ding/models/operations/retry_.rb +39 -0
- data/lib/ding/models/shared/createauthenticationrequest.rb +65 -0
- data/lib/ding/models/shared/createauthenticationresponse.rb +52 -0
- data/lib/ding/models/shared/createcheckrequest.rb +31 -0
- data/lib/ding/models/shared/createcheckresponse.rb +55 -0
- data/lib/ding/models/shared/errorresponse.rb +82 -0
- data/lib/ding/models/shared/lookupresponse.rb +61 -0
- data/lib/ding/models/shared/retryauthenticationrequest.rb +28 -0
- data/lib/ding/models/shared/retryauthenticationresponse.rb +64 -0
- data/lib/ding/models/shared/security.rb +25 -0
- data/lib/ding/otp.rb +149 -0
- data/lib/ding/sdk.rb +89 -0
- data/lib/ding/sdkconfiguration.rb +55 -0
- data/lib/ding/utils/metadata_fields.rb +154 -0
- data/lib/ding/utils/utils.rb +774 -0
- data/lib/ding_sdk.rb +27 -0
- metadata +191 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
2
|
+
|
3
|
+
# typed: true
|
4
|
+
# frozen_string_literal: true
|
5
|
+
|
6
|
+
require 'sorbet-runtime'
|
7
|
+
require 'faraday'
|
8
|
+
|
9
|
+
module DingSDK
|
10
|
+
module Shared
|
11
|
+
# RetryAuthenticationResponseStatus - The status of the retry. Possible values are:
|
12
|
+
# * `approved` - The retry was approved and a new code was sent.
|
13
|
+
# * `denied` - The retry was denied.
|
14
|
+
# * `no_attempt` - No attempt was sent yet, so a retry cannot be completed.
|
15
|
+
# * `rate_limited` - The authentication was rate limited and cannot be retried.
|
16
|
+
# * `expired_auth` - The authentication has expired and cannot be retried.
|
17
|
+
# * `already_validated` - The authentication has already been validated.
|
18
|
+
#
|
19
|
+
class RetryAuthenticationResponseStatus < T::Enum
|
20
|
+
enums do
|
21
|
+
APPROVED = new('approved')
|
22
|
+
DENIED = new('denied')
|
23
|
+
NO_ATTEMPT = new('no_attempt')
|
24
|
+
RATE_LIMITED = new('rate_limited')
|
25
|
+
EXPIRED_AUTH = new('expired_auth')
|
26
|
+
ALREADY_VALIDATED = new('already_validated')
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
class RetryAuthenticationResponse < DingSDK::Utils::FieldAugmented
|
33
|
+
extend T::Sig
|
34
|
+
|
35
|
+
# The UUID of the corresponding authentication.
|
36
|
+
field :authentication_uuid, T.nilable(String), { 'format_json': { 'letter_case': OpenApiSDK::Utils.field_name('authentication_uuid') } }
|
37
|
+
|
38
|
+
field :created_at, T.nilable(DateTime), { 'format_json': { 'letter_case': OpenApiSDK::Utils.field_name('created_at'), 'decoder': Utils.datetime_from_iso_format(true) } }
|
39
|
+
# The time at which the next retry will be available.
|
40
|
+
field :next_retry_at, T.nilable(DateTime), { 'format_json': { 'letter_case': OpenApiSDK::Utils.field_name('next_retry_at'), 'decoder': Utils.datetime_from_iso_format(true) } }
|
41
|
+
# The number of remaining retries.
|
42
|
+
field :remaining_retry, T.nilable(Integer), { 'format_json': { 'letter_case': OpenApiSDK::Utils.field_name('remaining_retry') } }
|
43
|
+
# The status of the retry. Possible values are:
|
44
|
+
# * `approved` - The retry was approved and a new code was sent.
|
45
|
+
# * `denied` - The retry was denied.
|
46
|
+
# * `no_attempt` - No attempt was sent yet, so a retry cannot be completed.
|
47
|
+
# * `rate_limited` - The authentication was rate limited and cannot be retried.
|
48
|
+
# * `expired_auth` - The authentication has expired and cannot be retried.
|
49
|
+
# * `already_validated` - The authentication has already been validated.
|
50
|
+
#
|
51
|
+
field :status, T.nilable(Shared::RetryAuthenticationResponseStatus), { 'format_json': { 'letter_case': OpenApiSDK::Utils.field_name('status'), 'decoder': Utils.enum_from_string(Shared::RetryAuthenticationResponseStatus, true) } }
|
52
|
+
|
53
|
+
|
54
|
+
sig { params(authentication_uuid: T.nilable(String), created_at: T.nilable(DateTime), next_retry_at: T.nilable(DateTime), remaining_retry: T.nilable(Integer), status: T.nilable(Shared::RetryAuthenticationResponseStatus)).void }
|
55
|
+
def initialize(authentication_uuid: nil, created_at: nil, next_retry_at: nil, remaining_retry: nil, status: nil)
|
56
|
+
@authentication_uuid = authentication_uuid
|
57
|
+
@created_at = created_at
|
58
|
+
@next_retry_at = next_retry_at
|
59
|
+
@remaining_retry = remaining_retry
|
60
|
+
@status = status
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
2
|
+
|
3
|
+
# typed: true
|
4
|
+
# frozen_string_literal: true
|
5
|
+
|
6
|
+
require 'sorbet-runtime'
|
7
|
+
require 'faraday'
|
8
|
+
|
9
|
+
module DingSDK
|
10
|
+
module Shared
|
11
|
+
|
12
|
+
class Security < DingSDK::Utils::FieldAugmented
|
13
|
+
extend T::Sig
|
14
|
+
|
15
|
+
|
16
|
+
field :api_key, String, { 'security': { 'scheme': true, 'type': 'apiKey', 'sub_type': 'header', 'field_name': 'x-api-key' } }
|
17
|
+
|
18
|
+
|
19
|
+
sig { params(api_key: String).void }
|
20
|
+
def initialize(api_key: nil)
|
21
|
+
@api_key = api_key
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/ding/otp.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
2
|
+
|
3
|
+
# typed: true
|
4
|
+
# frozen_string_literal: true
|
5
|
+
|
6
|
+
require 'faraday'
|
7
|
+
require 'faraday/multipart'
|
8
|
+
require 'sorbet-runtime'
|
9
|
+
|
10
|
+
module DingSDK
|
11
|
+
extend T::Sig
|
12
|
+
class Otp
|
13
|
+
# Send OTP codes to your users using their phone numbers.
|
14
|
+
extend T::Sig
|
15
|
+
sig { params(sdk_config: SDKConfiguration).void }
|
16
|
+
def initialize(sdk_config)
|
17
|
+
@sdk_configuration = sdk_config
|
18
|
+
end
|
19
|
+
|
20
|
+
sig { params(request: T.nilable(Shared::CreateCheckRequest)).returns(Utils::FieldAugmented) }
|
21
|
+
def check(request)
|
22
|
+
# check - Check a code
|
23
|
+
url, params = @sdk_configuration.get_server_details
|
24
|
+
base_url = Utils.template_url(url, params)
|
25
|
+
url = "#{base_url}/check"
|
26
|
+
headers = {}
|
27
|
+
req_content_type, data, form = Utils.serialize_request_body(request, :request, :json)
|
28
|
+
headers['content-type'] = req_content_type
|
29
|
+
headers['Accept'] = 'application/json'
|
30
|
+
headers['user-agent'] = @sdk_configuration.user_agent
|
31
|
+
|
32
|
+
r = @sdk_configuration.client.post(url) do |req|
|
33
|
+
req.headers = headers
|
34
|
+
Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
|
35
|
+
if form
|
36
|
+
req.body = Utils.encode_form(form)
|
37
|
+
elsif Utils.match_content_type(req_content_type, 'application/x-www-form-urlencoded')
|
38
|
+
req.body = URI.encode_www_form(data)
|
39
|
+
else
|
40
|
+
req.body = data
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
content_type = r.headers.fetch('Content-Type', 'application/octet-stream')
|
45
|
+
|
46
|
+
res = Operations::CheckResponse.new(
|
47
|
+
status_code: r.status, content_type: content_type, raw_response: r
|
48
|
+
)
|
49
|
+
if r.status == 200
|
50
|
+
if Utils.match_content_type(content_type, 'application/json')
|
51
|
+
out = Utils.unmarshal_complex(r.env.response_body, Shared::CreateCheckResponse)
|
52
|
+
res.create_check_response = out
|
53
|
+
end
|
54
|
+
elsif r.status == 400
|
55
|
+
if Utils.match_content_type(content_type, 'application/json')
|
56
|
+
out = Utils.unmarshal_complex(r.env.response_body, Shared::ErrorResponse)
|
57
|
+
res.error_response = out
|
58
|
+
end
|
59
|
+
end
|
60
|
+
res
|
61
|
+
end
|
62
|
+
|
63
|
+
sig { params(request: T.nilable(Shared::CreateAuthenticationRequest)).returns(Utils::FieldAugmented) }
|
64
|
+
def create_autentication(request)
|
65
|
+
# create_autentication - Send a code
|
66
|
+
url, params = @sdk_configuration.get_server_details
|
67
|
+
base_url = Utils.template_url(url, params)
|
68
|
+
url = "#{base_url}/authentication"
|
69
|
+
headers = {}
|
70
|
+
req_content_type, data, form = Utils.serialize_request_body(request, :request, :json)
|
71
|
+
headers['content-type'] = req_content_type
|
72
|
+
headers['Accept'] = 'application/json'
|
73
|
+
headers['user-agent'] = @sdk_configuration.user_agent
|
74
|
+
|
75
|
+
r = @sdk_configuration.client.post(url) do |req|
|
76
|
+
req.headers = headers
|
77
|
+
Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
|
78
|
+
if form
|
79
|
+
req.body = Utils.encode_form(form)
|
80
|
+
elsif Utils.match_content_type(req_content_type, 'application/x-www-form-urlencoded')
|
81
|
+
req.body = URI.encode_www_form(data)
|
82
|
+
else
|
83
|
+
req.body = data
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
content_type = r.headers.fetch('Content-Type', 'application/octet-stream')
|
88
|
+
|
89
|
+
res = Operations::CreateAutenticationResponse.new(
|
90
|
+
status_code: r.status, content_type: content_type, raw_response: r
|
91
|
+
)
|
92
|
+
if r.status == 200
|
93
|
+
if Utils.match_content_type(content_type, 'application/json')
|
94
|
+
out = Utils.unmarshal_complex(r.env.response_body, Shared::CreateAuthenticationResponse)
|
95
|
+
res.create_authentication_response = out
|
96
|
+
end
|
97
|
+
elsif r.status == 400
|
98
|
+
if Utils.match_content_type(content_type, 'application/json')
|
99
|
+
out = Utils.unmarshal_complex(r.env.response_body, Shared::ErrorResponse)
|
100
|
+
res.error_response = out
|
101
|
+
end
|
102
|
+
end
|
103
|
+
res
|
104
|
+
end
|
105
|
+
|
106
|
+
sig { params(request: T.nilable(Shared::RetryAuthenticationRequest)).returns(Utils::FieldAugmented) }
|
107
|
+
def retry(request)
|
108
|
+
# retry - Perform a retry
|
109
|
+
url, params = @sdk_configuration.get_server_details
|
110
|
+
base_url = Utils.template_url(url, params)
|
111
|
+
url = "#{base_url}/retry"
|
112
|
+
headers = {}
|
113
|
+
req_content_type, data, form = Utils.serialize_request_body(request, :request, :json)
|
114
|
+
headers['content-type'] = req_content_type
|
115
|
+
headers['Accept'] = 'application/json'
|
116
|
+
headers['user-agent'] = @sdk_configuration.user_agent
|
117
|
+
|
118
|
+
r = @sdk_configuration.client.post(url) do |req|
|
119
|
+
req.headers = headers
|
120
|
+
Utils.configure_request_security(req, @sdk_configuration.security) if !@sdk_configuration.nil? && !@sdk_configuration.security.nil?
|
121
|
+
if form
|
122
|
+
req.body = Utils.encode_form(form)
|
123
|
+
elsif Utils.match_content_type(req_content_type, 'application/x-www-form-urlencoded')
|
124
|
+
req.body = URI.encode_www_form(data)
|
125
|
+
else
|
126
|
+
req.body = data
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
content_type = r.headers.fetch('Content-Type', 'application/octet-stream')
|
131
|
+
|
132
|
+
res = Operations::RetryResponse.new(
|
133
|
+
status_code: r.status, content_type: content_type, raw_response: r
|
134
|
+
)
|
135
|
+
if r.status == 200
|
136
|
+
if Utils.match_content_type(content_type, 'application/json')
|
137
|
+
out = Utils.unmarshal_complex(r.env.response_body, Shared::RetryAuthenticationResponse)
|
138
|
+
res.retry_authentication_response = out
|
139
|
+
end
|
140
|
+
elsif r.status == 400
|
141
|
+
if Utils.match_content_type(content_type, 'application/json')
|
142
|
+
out = Utils.unmarshal_complex(r.env.response_body, Shared::ErrorResponse)
|
143
|
+
res.error_response = out
|
144
|
+
end
|
145
|
+
end
|
146
|
+
res
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
data/lib/ding/sdk.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
2
|
+
|
3
|
+
# typed: true
|
4
|
+
# frozen_string_literal: true
|
5
|
+
|
6
|
+
require 'faraday'
|
7
|
+
require 'faraday/multipart'
|
8
|
+
require 'sorbet-runtime'
|
9
|
+
module DingSDK
|
10
|
+
extend T::Sig
|
11
|
+
|
12
|
+
class Ding
|
13
|
+
extend T::Sig
|
14
|
+
|
15
|
+
attr_accessor :otp, :lookup
|
16
|
+
|
17
|
+
attr_accessor :security, :language, :sdk_version, :gen_version
|
18
|
+
|
19
|
+
sig do
|
20
|
+
params(security: T.nilable(Shared::Security),
|
21
|
+
server: String,
|
22
|
+
server_url: String,
|
23
|
+
url_params: T::Hash[Symbol, String],
|
24
|
+
client: Faraday::Request).void
|
25
|
+
end
|
26
|
+
def initialize(security: nil,
|
27
|
+
server: nil,
|
28
|
+
server_url: nil,
|
29
|
+
url_params: nil,
|
30
|
+
client: nil)
|
31
|
+
|
32
|
+
## Instantiates the SDK configuring it with the provided parameters.
|
33
|
+
# @param [Shared::Security] security The security details required for authentication
|
34
|
+
# @param [String] server The server by name to use for all operations
|
35
|
+
# @param [String] server_url The server URL to use for all operations
|
36
|
+
# @param [Hash<Symbol, String>] url_params Parameters to optionally template the server URL with
|
37
|
+
# @param [Faraday::Request] client The faraday HTTP client to use for all operations
|
38
|
+
|
39
|
+
if client.nil?
|
40
|
+
client = Faraday.new(request: {
|
41
|
+
params_encoder: Faraday::FlatParamsEncoder
|
42
|
+
}) do |f|
|
43
|
+
f.request :multipart, {}
|
44
|
+
# f.response :logger
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
if !server_url.nil?
|
49
|
+
if !url_params.nil?
|
50
|
+
server_url = Utils.template_url(server_url, url_params)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
server = SERVER_PRODUCTION if server.nil?
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
@sdk_configuration = SDKConfiguration.new(client, security, server_url, server)
|
58
|
+
init_sdks
|
59
|
+
end
|
60
|
+
|
61
|
+
sig { params(params: T.nilable(T::Hash[Symbol, String])).void }
|
62
|
+
def config_server_url(params)
|
63
|
+
if !params.nil?
|
64
|
+
@server_url = Utils.template_url(@server_url, params)
|
65
|
+
end
|
66
|
+
init_sdks
|
67
|
+
end
|
68
|
+
|
69
|
+
sig { params(server: String, params: T.nilable(T::Hash[Symbol, String])).void }
|
70
|
+
def config_server(params)
|
71
|
+
raise StandardError, 'Invalid server' if !SERVERS.include? server
|
72
|
+
|
73
|
+
config_server_url(params)
|
74
|
+
init_sdks
|
75
|
+
end
|
76
|
+
|
77
|
+
sig { params(security: Shared::Security).void }
|
78
|
+
def config_security(security)
|
79
|
+
@security = security
|
80
|
+
@sdk_configuration.security = security
|
81
|
+
end
|
82
|
+
|
83
|
+
sig { void }
|
84
|
+
def init_sdks
|
85
|
+
@otp = Otp.new(@sdk_configuration)
|
86
|
+
@lookup = Lookup.new(@sdk_configuration)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
2
|
+
|
3
|
+
# typed: true
|
4
|
+
# frozen_string_literal: true
|
5
|
+
|
6
|
+
require 'faraday'
|
7
|
+
require 'faraday/multipart'
|
8
|
+
require 'sorbet-runtime'
|
9
|
+
|
10
|
+
module OpenApiSDK
|
11
|
+
extend T::Sig
|
12
|
+
|
13
|
+
|
14
|
+
SERVER_PRODUCTION = 'production' # The production Ding API server
|
15
|
+
SERVERS = {
|
16
|
+
SERVER_PRODUCTION: 'https://api.ding.live/v1',
|
17
|
+
}.freeze
|
18
|
+
# Contains the list of servers available to the SDK
|
19
|
+
|
20
|
+
|
21
|
+
class SDKConfiguration < OpenApiSDK::Utils::FieldAugmented
|
22
|
+
extend T::Sig
|
23
|
+
|
24
|
+
field :client, T.nilable(Faraday::Connection)
|
25
|
+
field :security, Shared::Security
|
26
|
+
field :server_url, T.nilable(String)
|
27
|
+
field :server, T.nilable(String)
|
28
|
+
field :server_defaults, Hash[Symbol, Hash[Symbol, String]], { 'default_factory': Hash}
|
29
|
+
field :language, String
|
30
|
+
field :openapi_doc_version, String
|
31
|
+
field :sdk_version, String
|
32
|
+
field :gen_version, String
|
33
|
+
field :user_agent, String
|
34
|
+
|
35
|
+
|
36
|
+
sig { params(client: Faraday::Connection, security: T.nilable(Shared::Security), server_url: T.nilable(String), server_idx: T.nilable(Integer), server_defaults: T::Hash[Symbol, String], globals: T::Hash[Symbol, T::Hash[Symbol, T::Hash[Symbol, Object]]]).void }
|
37
|
+
def initialize(client, security, server_url, server_idx, server_defaults, globals)
|
38
|
+
@client = client
|
39
|
+
@server = server_url.nil? ? '' : server_url
|
40
|
+
@language = 'ruby'
|
41
|
+
@openapi_doc_version = '1.0.0'
|
42
|
+
@sdk_version = '0.3.0'
|
43
|
+
@gen_version = '2.189.1'
|
44
|
+
@user_agent = 'speakeasy-sdk/ruby 0.3.0 2.189.1 1.0.0 ding_sdk'
|
45
|
+
end
|
46
|
+
|
47
|
+
sig { returns([String, T::Hash[Symbol, String]]) }
|
48
|
+
def get_server_details
|
49
|
+
return [@server_url.delete_suffix('/'), {}] if !@server_url.nil?
|
50
|
+
@server = SERVER_PRODUCTION if @server.nil?
|
51
|
+
|
52
|
+
[SERVERS[@server], {}]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
# Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
|
2
|
+
|
3
|
+
# typed: true
|
4
|
+
# frozen_string_literal: true
|
5
|
+
|
6
|
+
module DingSDK
|
7
|
+
module MetadataFields
|
8
|
+
extend T::Sig
|
9
|
+
|
10
|
+
class Field
|
11
|
+
attr_accessor :name, :type, :metadata
|
12
|
+
|
13
|
+
def initialize(name, type, metadata)
|
14
|
+
@name = name
|
15
|
+
@type = type
|
16
|
+
@metadata = metadata
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
extend T::Sig
|
22
|
+
|
23
|
+
def fields
|
24
|
+
@__fields__ = [] if @__fields__.nil?
|
25
|
+
|
26
|
+
@__fields__
|
27
|
+
end
|
28
|
+
|
29
|
+
def field(field_name, type, metadata = {})
|
30
|
+
attr_accessor field_name
|
31
|
+
|
32
|
+
fields << Field.new(field_name, type, metadata)
|
33
|
+
end
|
34
|
+
|
35
|
+
def unmarshal_single(field_type, value, decoder = nil)
|
36
|
+
if field_type.respond_to? :unmarshal_json
|
37
|
+
unmarshalled = field_type.unmarshal_json(value)
|
38
|
+
return unmarshalled
|
39
|
+
elsif field_type.to_s == 'Object'
|
40
|
+
# rubocop:disable Lint/SuppressedException
|
41
|
+
begin
|
42
|
+
value = JSON.parse(value)
|
43
|
+
rescue TypeError, JSON::ParserError
|
44
|
+
end
|
45
|
+
# rubocop:enable Lint/SuppressedException
|
46
|
+
return value
|
47
|
+
end
|
48
|
+
if decoder.nil?
|
49
|
+
value
|
50
|
+
else
|
51
|
+
decoder.call(value)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
sig { params(json_obj: T.any(String, T::Hash[Symbol, String])).returns(Utils::FieldAugmented) }
|
56
|
+
def unmarshal_json(json_obj)
|
57
|
+
to_build = new
|
58
|
+
begin
|
59
|
+
d = JSON.parse(json_obj)
|
60
|
+
rescue TypeError, JSON::ParserError
|
61
|
+
d = json_obj
|
62
|
+
end
|
63
|
+
fields.each do |field|
|
64
|
+
field_type = field.type
|
65
|
+
if field.type.respond_to?(:types) && field.type.types.length == 2
|
66
|
+
type2 = field.type.types[1]
|
67
|
+
if type2.raw_type == NilClass
|
68
|
+
field_type = field.type.types[0]
|
69
|
+
field_type = field_type.raw_type if field_type.respond_to? :raw_type
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
key = "#{field.name}="
|
75
|
+
lookup = field.metadata.fetch(:format_json, {}).fetch(:letter_case, nil).call
|
76
|
+
value = d[lookup]
|
77
|
+
next if value.nil?
|
78
|
+
|
79
|
+
if field_type.to_s.start_with? 'T::Array'
|
80
|
+
inner_type = field_type.type.raw_type
|
81
|
+
unmarshalled_array = value.map { |f| unmarshal_single(inner_type, f) }
|
82
|
+
to_build.send(key, unmarshalled_array)
|
83
|
+
elsif field_type.to_s.start_with? 'T::Hash'
|
84
|
+
_, val_type = field_type.type.types
|
85
|
+
# rubocop:disable Style/HashTransformValues
|
86
|
+
unmarshalled_hash = value.map { |k, v| [k, unmarshal_single(val_type.raw_type, v)] }.to_h
|
87
|
+
# rubocop:enable Style/HashTransformValues
|
88
|
+
to_build.send(key, unmarshalled_hash)
|
89
|
+
else
|
90
|
+
decoder = field.metadata.fetch(:format_json, {}).fetch(:decoder, nil)
|
91
|
+
final_value = unmarshal_single(field_type, value, decoder)
|
92
|
+
to_build.send(key, final_value)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
to_build
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def initialize
|
100
|
+
instance = allocate
|
101
|
+
fields.each { |mod| instance.singleton_class.include(mod) }
|
102
|
+
instance.send(:initialize)
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.included(base)
|
106
|
+
base.extend(ClassMethods)
|
107
|
+
end
|
108
|
+
|
109
|
+
def fields
|
110
|
+
self.class.fields
|
111
|
+
end
|
112
|
+
|
113
|
+
def field(field_name)
|
114
|
+
fields.each do |f|
|
115
|
+
return f if f.name == field_name
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def marshal_single(field)
|
120
|
+
if field.respond_to? :marshal_json
|
121
|
+
field.marshal_json(encode: false)
|
122
|
+
else
|
123
|
+
Utils.val_to_string(field, primitives: false)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def marshal_json(encode: true)
|
128
|
+
d = {}
|
129
|
+
fields.sort_by(&:name).each do |field|
|
130
|
+
f = send(field.name)
|
131
|
+
next if f.nil?
|
132
|
+
|
133
|
+
format_json_meta = field.metadata[:format_json]
|
134
|
+
if !format_json_meta.nil? && format_json_meta.include?(:letter_case)
|
135
|
+
key = format_json_meta[:letter_case].call(field.name)
|
136
|
+
else
|
137
|
+
key = field.name
|
138
|
+
end
|
139
|
+
if f.is_a? Array
|
140
|
+
d[key] = f.map { |o| marshal_single(o) }
|
141
|
+
elsif f.is_a? Hash
|
142
|
+
d[key] = f.map { |k, v| [k, marshal_single(v)] }
|
143
|
+
else
|
144
|
+
d[key] = marshal_single(f)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
if encode
|
148
|
+
JSON.dump(d)
|
149
|
+
else
|
150
|
+
d
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|