finch-api 0.1.0.pre.alpha.4 → 0.1.0.pre.alpha.5

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.
Files changed (204) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -8
  3. data/lib/finch-api/client.rb +1 -1
  4. data/lib/finch-api/individuals_page.rb +4 -4
  5. data/lib/finch-api/models/access_token_create_params.rb +1 -1
  6. data/lib/finch-api/models/account_disconnect_params.rb +1 -1
  7. data/lib/finch-api/models/account_introspect_params.rb +1 -1
  8. data/lib/finch-api/models/connect/session_new_params.rb +1 -1
  9. data/lib/finch-api/models/connect/session_reauthenticate_params.rb +1 -1
  10. data/lib/finch-api/models/hris/benefit_create_params.rb +1 -1
  11. data/lib/finch-api/models/hris/benefit_list_params.rb +1 -1
  12. data/lib/finch-api/models/hris/benefit_list_supported_benefits_params.rb +1 -1
  13. data/lib/finch-api/models/hris/benefit_retrieve_params.rb +1 -1
  14. data/lib/finch-api/models/hris/benefit_update_params.rb +1 -1
  15. data/lib/finch-api/models/hris/benefits/individual_enroll_many_params.rb +1 -1
  16. data/lib/finch-api/models/hris/benefits/individual_enrolled_ids_params.rb +1 -1
  17. data/lib/finch-api/models/hris/benefits/individual_retrieve_many_benefits_params.rb +1 -1
  18. data/lib/finch-api/models/hris/benefits/individual_unenroll_many_params.rb +1 -1
  19. data/lib/finch-api/models/hris/company_retrieve_params.rb +1 -1
  20. data/lib/finch-api/models/hris/directory_list_individuals_params.rb +1 -1
  21. data/lib/finch-api/models/hris/directory_list_params.rb +1 -1
  22. data/lib/finch-api/models/hris/document_list_params.rb +1 -1
  23. data/lib/finch-api/models/hris/document_retreive_params.rb +1 -1
  24. data/lib/finch-api/models/hris/employment_retrieve_many_params.rb +1 -1
  25. data/lib/finch-api/models/hris/individual_retrieve_many_params.rb +1 -1
  26. data/lib/finch-api/models/hris/pay_statement_retrieve_many_params.rb +1 -1
  27. data/lib/finch-api/models/hris/payment_list_params.rb +1 -1
  28. data/lib/finch-api/models/jobs/automated_create_params.rb +1 -1
  29. data/lib/finch-api/models/jobs/automated_list_params.rb +1 -1
  30. data/lib/finch-api/models/jobs/automated_retrieve_params.rb +1 -1
  31. data/lib/finch-api/models/jobs/manual_retrieve_params.rb +1 -1
  32. data/lib/finch-api/models/payroll/pay_group_list_params.rb +1 -1
  33. data/lib/finch-api/models/payroll/pay_group_retrieve_params.rb +1 -1
  34. data/lib/finch-api/models/provider_list_params.rb +1 -1
  35. data/lib/finch-api/models/request_forwarding_forward_params.rb +1 -1
  36. data/lib/finch-api/models/sandbox/company_update_params.rb +1 -1
  37. data/lib/finch-api/models/sandbox/connection_create_params.rb +1 -1
  38. data/lib/finch-api/models/sandbox/connections/account_create_params.rb +1 -1
  39. data/lib/finch-api/models/sandbox/connections/account_update_params.rb +1 -1
  40. data/lib/finch-api/models/sandbox/directory_create_params.rb +1 -1
  41. data/lib/finch-api/models/sandbox/employment_update_params.rb +1 -1
  42. data/lib/finch-api/models/sandbox/individual_update_params.rb +1 -1
  43. data/lib/finch-api/models/sandbox/job_create_params.rb +1 -1
  44. data/lib/finch-api/models/sandbox/jobs/configuration_retrieve_params.rb +1 -1
  45. data/lib/finch-api/models/sandbox/jobs/configuration_update_params.rb +1 -1
  46. data/lib/finch-api/models/sandbox/payment_create_params.rb +1 -1
  47. data/lib/finch-api/page.rb +4 -4
  48. data/lib/finch-api/request_options.rb +0 -33
  49. data/lib/finch-api/responses_page.rb +3 -3
  50. data/lib/finch-api/single_page.rb +3 -3
  51. data/lib/finch-api/transport/base_client.rb +459 -0
  52. data/lib/finch-api/transport/pooled_net_requester.rb +182 -0
  53. data/lib/finch-api/type/array_of.rb +110 -0
  54. data/lib/finch-api/type/base_model.rb +355 -0
  55. data/lib/finch-api/type/base_page.rb +61 -0
  56. data/lib/finch-api/type/boolean_model.rb +52 -0
  57. data/lib/finch-api/type/converter.rb +211 -0
  58. data/lib/finch-api/type/enum.rb +105 -0
  59. data/lib/finch-api/type/hash_of.rb +136 -0
  60. data/lib/finch-api/type/request_parameters.rb +38 -0
  61. data/lib/finch-api/type/union.rb +204 -0
  62. data/lib/finch-api/type/unknown.rb +56 -0
  63. data/lib/finch-api/type.rb +23 -0
  64. data/lib/finch-api/version.rb +1 -1
  65. data/lib/finch-api.rb +13 -4
  66. data/rbi/lib/finch-api/client.rbi +1 -1
  67. data/rbi/lib/finch-api/individuals_page.rbi +1 -1
  68. data/rbi/lib/finch-api/models/access_token_create_params.rbi +1 -1
  69. data/rbi/lib/finch-api/models/account_disconnect_params.rbi +1 -1
  70. data/rbi/lib/finch-api/models/account_introspect_params.rbi +1 -1
  71. data/rbi/lib/finch-api/models/connect/session_new_params.rbi +1 -1
  72. data/rbi/lib/finch-api/models/connect/session_reauthenticate_params.rbi +1 -1
  73. data/rbi/lib/finch-api/models/hris/benefit_create_params.rbi +1 -1
  74. data/rbi/lib/finch-api/models/hris/benefit_list_params.rbi +1 -1
  75. data/rbi/lib/finch-api/models/hris/benefit_list_supported_benefits_params.rbi +1 -1
  76. data/rbi/lib/finch-api/models/hris/benefit_retrieve_params.rbi +1 -1
  77. data/rbi/lib/finch-api/models/hris/benefit_update_params.rbi +1 -1
  78. data/rbi/lib/finch-api/models/hris/benefits/individual_enroll_many_params.rbi +1 -1
  79. data/rbi/lib/finch-api/models/hris/benefits/individual_enrolled_ids_params.rbi +1 -1
  80. data/rbi/lib/finch-api/models/hris/benefits/individual_retrieve_many_benefits_params.rbi +1 -1
  81. data/rbi/lib/finch-api/models/hris/benefits/individual_unenroll_many_params.rbi +1 -1
  82. data/rbi/lib/finch-api/models/hris/company_retrieve_params.rbi +1 -1
  83. data/rbi/lib/finch-api/models/hris/directory_list_individuals_params.rbi +1 -1
  84. data/rbi/lib/finch-api/models/hris/directory_list_params.rbi +1 -1
  85. data/rbi/lib/finch-api/models/hris/document_list_params.rbi +1 -1
  86. data/rbi/lib/finch-api/models/hris/document_retreive_params.rbi +1 -1
  87. data/rbi/lib/finch-api/models/hris/employment_retrieve_many_params.rbi +1 -1
  88. data/rbi/lib/finch-api/models/hris/individual_retrieve_many_params.rbi +1 -1
  89. data/rbi/lib/finch-api/models/hris/pay_statement_retrieve_many_params.rbi +1 -1
  90. data/rbi/lib/finch-api/models/hris/payment_list_params.rbi +1 -1
  91. data/rbi/lib/finch-api/models/jobs/automated_create_params.rbi +1 -1
  92. data/rbi/lib/finch-api/models/jobs/automated_list_params.rbi +1 -1
  93. data/rbi/lib/finch-api/models/jobs/automated_retrieve_params.rbi +1 -1
  94. data/rbi/lib/finch-api/models/jobs/manual_retrieve_params.rbi +1 -1
  95. data/rbi/lib/finch-api/models/payroll/pay_group_list_params.rbi +1 -1
  96. data/rbi/lib/finch-api/models/payroll/pay_group_retrieve_params.rbi +1 -1
  97. data/rbi/lib/finch-api/models/provider_list_params.rbi +1 -1
  98. data/rbi/lib/finch-api/models/request_forwarding_forward_params.rbi +1 -1
  99. data/rbi/lib/finch-api/models/sandbox/company_update_params.rbi +1 -1
  100. data/rbi/lib/finch-api/models/sandbox/connection_create_params.rbi +1 -1
  101. data/rbi/lib/finch-api/models/sandbox/connections/account_create_params.rbi +1 -1
  102. data/rbi/lib/finch-api/models/sandbox/connections/account_update_params.rbi +1 -1
  103. data/rbi/lib/finch-api/models/sandbox/directory_create_params.rbi +1 -1
  104. data/rbi/lib/finch-api/models/sandbox/directory_create_response.rbi +1 -1
  105. data/rbi/lib/finch-api/models/sandbox/employment_update_params.rbi +1 -1
  106. data/rbi/lib/finch-api/models/sandbox/individual_update_params.rbi +1 -1
  107. data/rbi/lib/finch-api/models/sandbox/job_create_params.rbi +1 -1
  108. data/rbi/lib/finch-api/models/sandbox/jobs/configuration_retrieve_params.rbi +1 -1
  109. data/rbi/lib/finch-api/models/sandbox/jobs/configuration_retrieve_response.rbi +4 -1
  110. data/rbi/lib/finch-api/models/sandbox/jobs/configuration_update_params.rbi +1 -1
  111. data/rbi/lib/finch-api/models/sandbox/payment_create_params.rbi +1 -1
  112. data/rbi/lib/finch-api/page.rbi +1 -1
  113. data/rbi/lib/finch-api/request_options.rbi +0 -15
  114. data/rbi/lib/finch-api/responses_page.rbi +1 -1
  115. data/rbi/lib/finch-api/single_page.rbi +1 -1
  116. data/rbi/lib/finch-api/transport/base_client.rbi +204 -0
  117. data/rbi/lib/finch-api/transport/pooled_net_requester.rbi +64 -0
  118. data/rbi/lib/finch-api/type/array_of.rbi +82 -0
  119. data/rbi/lib/finch-api/type/base_model.rbi +191 -0
  120. data/rbi/lib/finch-api/type/base_page.rbi +38 -0
  121. data/rbi/lib/finch-api/type/boolean_model.rbi +41 -0
  122. data/rbi/lib/finch-api/type/converter.rbi +101 -0
  123. data/rbi/lib/finch-api/type/enum.rbi +58 -0
  124. data/rbi/lib/finch-api/type/hash_of.rbi +86 -0
  125. data/rbi/lib/finch-api/type/request_parameters.rbi +20 -0
  126. data/rbi/lib/finch-api/type/union.rbi +66 -0
  127. data/rbi/lib/finch-api/type/unknown.rbi +37 -0
  128. data/rbi/lib/finch-api/type.rbi +23 -0
  129. data/rbi/lib/finch-api/version.rbi +1 -1
  130. data/sig/finch-api/client.rbs +1 -1
  131. data/sig/finch-api/individuals_page.rbs +1 -1
  132. data/sig/finch-api/models/access_token_create_params.rbs +1 -1
  133. data/sig/finch-api/models/account_disconnect_params.rbs +1 -1
  134. data/sig/finch-api/models/account_introspect_params.rbs +1 -1
  135. data/sig/finch-api/models/connect/session_new_params.rbs +1 -1
  136. data/sig/finch-api/models/connect/session_reauthenticate_params.rbs +1 -1
  137. data/sig/finch-api/models/hris/benefit_create_params.rbs +1 -1
  138. data/sig/finch-api/models/hris/benefit_list_params.rbs +1 -1
  139. data/sig/finch-api/models/hris/benefit_list_supported_benefits_params.rbs +1 -1
  140. data/sig/finch-api/models/hris/benefit_retrieve_params.rbs +1 -1
  141. data/sig/finch-api/models/hris/benefit_update_params.rbs +1 -1
  142. data/sig/finch-api/models/hris/benefits/individual_enroll_many_params.rbs +1 -1
  143. data/sig/finch-api/models/hris/benefits/individual_enrolled_ids_params.rbs +1 -1
  144. data/sig/finch-api/models/hris/benefits/individual_retrieve_many_benefits_params.rbs +1 -1
  145. data/sig/finch-api/models/hris/benefits/individual_unenroll_many_params.rbs +1 -1
  146. data/sig/finch-api/models/hris/company_retrieve_params.rbs +1 -1
  147. data/sig/finch-api/models/hris/directory_list_individuals_params.rbs +1 -1
  148. data/sig/finch-api/models/hris/directory_list_params.rbs +1 -1
  149. data/sig/finch-api/models/hris/document_list_params.rbs +1 -1
  150. data/sig/finch-api/models/hris/document_retreive_params.rbs +1 -1
  151. data/sig/finch-api/models/hris/employment_retrieve_many_params.rbs +1 -1
  152. data/sig/finch-api/models/hris/individual_retrieve_many_params.rbs +1 -1
  153. data/sig/finch-api/models/hris/pay_statement_retrieve_many_params.rbs +1 -1
  154. data/sig/finch-api/models/hris/payment_list_params.rbs +1 -1
  155. data/sig/finch-api/models/jobs/automated_create_params.rbs +1 -1
  156. data/sig/finch-api/models/jobs/automated_list_params.rbs +1 -1
  157. data/sig/finch-api/models/jobs/automated_retrieve_params.rbs +1 -1
  158. data/sig/finch-api/models/jobs/manual_retrieve_params.rbs +1 -1
  159. data/sig/finch-api/models/payroll/pay_group_list_params.rbs +1 -1
  160. data/sig/finch-api/models/payroll/pay_group_retrieve_params.rbs +1 -1
  161. data/sig/finch-api/models/provider_list_params.rbs +1 -1
  162. data/sig/finch-api/models/request_forwarding_forward_params.rbs +1 -1
  163. data/sig/finch-api/models/sandbox/company_update_params.rbs +1 -1
  164. data/sig/finch-api/models/sandbox/connection_create_params.rbs +1 -1
  165. data/sig/finch-api/models/sandbox/connections/account_create_params.rbs +1 -1
  166. data/sig/finch-api/models/sandbox/connections/account_update_params.rbs +1 -1
  167. data/sig/finch-api/models/sandbox/directory_create_params.rbs +1 -1
  168. data/sig/finch-api/models/sandbox/employment_update_params.rbs +1 -1
  169. data/sig/finch-api/models/sandbox/individual_update_params.rbs +1 -1
  170. data/sig/finch-api/models/sandbox/job_create_params.rbs +1 -1
  171. data/sig/finch-api/models/sandbox/jobs/configuration_retrieve_params.rbs +1 -1
  172. data/sig/finch-api/models/sandbox/jobs/configuration_update_params.rbs +1 -1
  173. data/sig/finch-api/models/sandbox/payment_create_params.rbs +1 -1
  174. data/sig/finch-api/page.rbs +1 -1
  175. data/sig/finch-api/request_options.rbs +0 -10
  176. data/sig/finch-api/responses_page.rbs +1 -1
  177. data/sig/finch-api/single_page.rbs +1 -1
  178. data/sig/finch-api/transport/base_client.rbs +110 -0
  179. data/sig/finch-api/transport/pooled_net_requester.rbs +39 -0
  180. data/sig/finch-api/type/array_of.rbs +36 -0
  181. data/sig/finch-api/type/base_model.rbs +73 -0
  182. data/sig/finch-api/type/base_page.rbs +22 -0
  183. data/sig/finch-api/type/boolean_model.rbs +18 -0
  184. data/sig/finch-api/type/converter.rbs +36 -0
  185. data/sig/finch-api/type/enum.rbs +22 -0
  186. data/sig/finch-api/type/hash_of.rbs +36 -0
  187. data/sig/finch-api/type/request_parameters.rbs +13 -0
  188. data/sig/finch-api/type/union.rbs +37 -0
  189. data/sig/finch-api/type/unknown.rbs +18 -0
  190. data/sig/finch-api/type.rbs +22 -0
  191. data/sig/finch-api/version.rbs +1 -1
  192. metadata +40 -13
  193. data/lib/finch-api/base_client.rb +0 -457
  194. data/lib/finch-api/base_model.rb +0 -1174
  195. data/lib/finch-api/base_page.rb +0 -59
  196. data/lib/finch-api/pooled_net_requester.rb +0 -180
  197. data/rbi/lib/finch-api/base_client.rbi +0 -196
  198. data/rbi/lib/finch-api/base_model.rbi +0 -609
  199. data/rbi/lib/finch-api/base_page.rbi +0 -36
  200. data/rbi/lib/finch-api/pooled_net_requester.rbi +0 -59
  201. data/sig/finch-api/base_client.rbs +0 -106
  202. data/sig/finch-api/base_model.rbs +0 -248
  203. data/sig/finch-api/base_page.rbs +0 -20
  204. data/sig/finch-api/pooled_net_requester.rbs +0 -37
@@ -0,0 +1,211 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FinchAPI
4
+ module Type
5
+ # @api private
6
+ module Converter
7
+ # rubocop:disable Lint/UnusedMethodArgument
8
+
9
+ # @api private
10
+ #
11
+ # @param value [Object]
12
+ #
13
+ # @param state [Hash{Symbol=>Object}] .
14
+ #
15
+ # @option state [Boolean, :strong] :strictness
16
+ #
17
+ # @option state [Hash{Symbol=>Object}] :exactness
18
+ #
19
+ # @option state [Integer] :branched
20
+ #
21
+ # @return [Object]
22
+ def coerce(value, state:) = (raise NotImplementedError)
23
+
24
+ # @api private
25
+ #
26
+ # @param value [Object]
27
+ #
28
+ # @return [Object]
29
+ def dump(value)
30
+ case value
31
+ in Array
32
+ value.map { FinchAPI::Unknown.dump(_1) }
33
+ in Hash
34
+ value.transform_values { FinchAPI::Unknown.dump(_1) }
35
+ in FinchAPI::BaseModel
36
+ value.class.dump(value)
37
+ else
38
+ value
39
+ end
40
+ end
41
+
42
+ # rubocop:enable Lint/UnusedMethodArgument
43
+
44
+ class << self
45
+ # @api private
46
+ #
47
+ # @param spec [Hash{Symbol=>Object}, Proc, FinchAPI::Type::Converter, Class] .
48
+ #
49
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
50
+ #
51
+ # @option spec [Proc] :enum
52
+ #
53
+ # @option spec [Proc] :union
54
+ #
55
+ # @option spec [Boolean] :"nil?"
56
+ #
57
+ # @return [Proc]
58
+ def type_info(spec)
59
+ case spec
60
+ in Proc
61
+ spec
62
+ in Hash
63
+ type_info(spec.slice(:const, :enum, :union).first&.last)
64
+ in true | false
65
+ -> { FinchAPI::BooleanModel }
66
+ in FinchAPI::Type::Converter | Class | Symbol
67
+ -> { spec }
68
+ in NilClass | Integer | Float
69
+ -> { spec.class }
70
+ end
71
+ end
72
+
73
+ # @api private
74
+ #
75
+ # Based on `target`, transform `value` into `target`, to the extent possible:
76
+ #
77
+ # 1. if the given `value` conforms to `target` already, return the given `value`
78
+ # 2. if it's possible and safe to convert the given `value` to `target`, then the
79
+ # converted value
80
+ # 3. otherwise, the given `value` unaltered
81
+ #
82
+ # The coercion process is subject to improvement between minor release versions.
83
+ # See https://docs.pydantic.dev/latest/concepts/unions/#smart-mode
84
+ #
85
+ # @param target [FinchAPI::Type::Converter, Class]
86
+ #
87
+ # @param value [Object]
88
+ #
89
+ # @param state [Hash{Symbol=>Object}] The `strictness` is one of `true`, `false`, or `:strong`. This informs the
90
+ # coercion strategy when we have to decide between multiple possible conversion
91
+ # targets:
92
+ #
93
+ # - `true`: the conversion must be exact, with minimum coercion.
94
+ # - `false`: the conversion can be approximate, with some coercion.
95
+ # - `:strong`: the conversion must be exact, with no coercion, and raise an error
96
+ # if not possible.
97
+ #
98
+ # The `exactness` is `Hash` with keys being one of `yes`, `no`, or `maybe`. For
99
+ # any given conversion attempt, the exactness will be updated based on how closely
100
+ # the value recursively matches the target type:
101
+ #
102
+ # - `yes`: the value can be converted to the target type with minimum coercion.
103
+ # - `maybe`: the value can be converted to the target type with some reasonable
104
+ # coercion.
105
+ # - `no`: the value cannot be converted to the target type.
106
+ #
107
+ # See implementation below for more details.
108
+ #
109
+ # @option state [Boolean, :strong] :strictness
110
+ #
111
+ # @option state [Hash{Symbol=>Object}] :exactness
112
+ #
113
+ # @option state [Integer] :branched
114
+ #
115
+ # @return [Object]
116
+ def coerce(
117
+ target,
118
+ value,
119
+ state: {strictness: true, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
120
+ )
121
+ strictness, exactness = state.fetch_values(:strictness, :exactness)
122
+
123
+ case target
124
+ in FinchAPI::Type::Converter
125
+ return target.coerce(value, state: state)
126
+ in Class
127
+ if value.is_a?(target)
128
+ exactness[:yes] += 1
129
+ return value
130
+ end
131
+
132
+ case target
133
+ in -> { _1 <= NilClass }
134
+ exactness[value.nil? ? :yes : :maybe] += 1
135
+ return nil
136
+ in -> { _1 <= Integer }
137
+ if value.is_a?(Integer)
138
+ exactness[:yes] += 1
139
+ return value
140
+ elsif strictness == :strong
141
+ message = "no implicit conversion of #{value.class} into #{target.inspect}"
142
+ raise TypeError.new(message)
143
+ else
144
+ Kernel.then do
145
+ return Integer(value).tap { exactness[:maybe] += 1 }
146
+ rescue ArgumentError, TypeError
147
+ end
148
+ end
149
+ in -> { _1 <= Float }
150
+ if value.is_a?(Numeric)
151
+ exactness[:yes] += 1
152
+ return Float(value)
153
+ elsif strictness == :strong
154
+ message = "no implicit conversion of #{value.class} into #{target.inspect}"
155
+ raise TypeError.new(message)
156
+ else
157
+ Kernel.then do
158
+ return Float(value).tap { exactness[:maybe] += 1 }
159
+ rescue ArgumentError, TypeError
160
+ end
161
+ end
162
+ in -> { _1 <= String }
163
+ case value
164
+ in String | Symbol | Numeric
165
+ exactness[value.is_a?(Numeric) ? :maybe : :yes] += 1
166
+ return value.to_s
167
+ else
168
+ if strictness == :strong
169
+ message = "no implicit conversion of #{value.class} into #{target.inspect}"
170
+ raise TypeError.new(message)
171
+ end
172
+ end
173
+ in -> { _1 <= Date || _1 <= Time }
174
+ Kernel.then do
175
+ return target.parse(value).tap { exactness[:yes] += 1 }
176
+ rescue ArgumentError, TypeError => e
177
+ raise e if strictness == :strong
178
+ end
179
+ in -> { _1 <= IO } if value.is_a?(String)
180
+ exactness[:yes] += 1
181
+ return StringIO.new(value.b)
182
+ else
183
+ end
184
+ in Symbol
185
+ if (value.is_a?(Symbol) || value.is_a?(String)) && value.to_sym == target
186
+ exactness[:yes] += 1
187
+ return target
188
+ elsif strictness == :strong
189
+ message = "cannot convert non-matching #{value.class} into #{target.inspect}"
190
+ raise ArgumentError.new(message)
191
+ end
192
+ else
193
+ end
194
+
195
+ exactness[:no] += 1
196
+ value
197
+ end
198
+
199
+ # @api private
200
+ #
201
+ # @param target [FinchAPI::Type::Converter, Class]
202
+ # @param value [Object]
203
+ #
204
+ # @return [Object]
205
+ def dump(target, value)
206
+ target.is_a?(FinchAPI::Type::Converter) ? target.dump(value) : FinchAPI::Unknown.dump(value)
207
+ end
208
+ end
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FinchAPI
4
+ module Type
5
+ # @api private
6
+ #
7
+ # A value from among a specified list of options. OpenAPI enum values map to Ruby
8
+ # values in the SDK as follows:
9
+ #
10
+ # 1. boolean => true | false
11
+ # 2. integer => Integer
12
+ # 3. float => Float
13
+ # 4. string => Symbol
14
+ #
15
+ # We can therefore convert string values to Symbols, but can't convert other
16
+ # values safely.
17
+ #
18
+ # @example
19
+ # # `connection_status_type` is a `FinchAPI::Models::ConnectionStatusType`
20
+ # case connection_status_type
21
+ # when FinchAPI::Models::ConnectionStatusType::PENDING
22
+ # # ...
23
+ # when FinchAPI::Models::ConnectionStatusType::PROCESSING
24
+ # # ...
25
+ # when FinchAPI::Models::ConnectionStatusType::CONNECTED
26
+ # # ...
27
+ # else
28
+ # puts(connection_status_type)
29
+ # end
30
+ #
31
+ # @example
32
+ # case connection_status_type
33
+ # in :pending
34
+ # # ...
35
+ # in :processing
36
+ # # ...
37
+ # in :connected
38
+ # # ...
39
+ # else
40
+ # puts(connection_status_type)
41
+ # end
42
+ module Enum
43
+ include FinchAPI::Type::Converter
44
+
45
+ # All of the valid Symbol values for this enum.
46
+ #
47
+ # @return [Array<NilClass, Boolean, Integer, Float, Symbol>]
48
+ def values = (@values ||= constants.map { const_get(_1) })
49
+
50
+ # @api private
51
+ #
52
+ # Guard against thread safety issues by instantiating `@values`.
53
+ private def finalize! = values
54
+
55
+ # @param other [Object]
56
+ #
57
+ # @return [Boolean]
58
+ def ===(other) = values.include?(other)
59
+
60
+ # @param other [Object]
61
+ #
62
+ # @return [Boolean]
63
+ def ==(other)
64
+ other.is_a?(Module) && other.singleton_class <= FinchAPI::Enum && other.values.to_set == values.to_set
65
+ end
66
+
67
+ # @api private
68
+ #
69
+ # Unlike with primitives, `Enum` additionally validates that the value is a member
70
+ # of the enum.
71
+ #
72
+ # @param value [String, Symbol, Object]
73
+ #
74
+ # @param state [Hash{Symbol=>Object}] .
75
+ #
76
+ # @option state [Boolean, :strong] :strictness
77
+ #
78
+ # @option state [Hash{Symbol=>Object}] :exactness
79
+ #
80
+ # @option state [Integer] :branched
81
+ #
82
+ # @return [Symbol, Object]
83
+ def coerce(value, state:)
84
+ exactness = state.fetch(:exactness)
85
+ val = value.is_a?(String) ? value.to_sym : value
86
+
87
+ if values.include?(val)
88
+ exactness[:yes] += 1
89
+ val
90
+ else
91
+ exactness[values.first&.class == val.class ? :maybe : :no] += 1
92
+ value
93
+ end
94
+ end
95
+
96
+ # @!parse
97
+ # # @api private
98
+ # #
99
+ # # @param value [Symbol, Object]
100
+ # #
101
+ # # @return [Symbol, Object]
102
+ # def dump(value) = super
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,136 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FinchAPI
4
+ module Type
5
+ # @api private
6
+ #
7
+ # @abstract
8
+ #
9
+ # Hash of items of a given type.
10
+ class HashOf
11
+ include FinchAPI::Type::Converter
12
+
13
+ # @param type_info [Hash{Symbol=>Object}, Proc, FinchAPI::Type::Converter, Class]
14
+ #
15
+ # @param spec [Hash{Symbol=>Object}] .
16
+ #
17
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
18
+ #
19
+ # @option spec [Proc] :enum
20
+ #
21
+ # @option spec [Proc] :union
22
+ #
23
+ # @option spec [Boolean] :"nil?"
24
+ def self.[](type_info, spec = {}) = new(type_info, spec)
25
+
26
+ # @param other [Object]
27
+ #
28
+ # @return [Boolean]
29
+ def ===(other)
30
+ type = item_type
31
+ case other
32
+ in Hash
33
+ other.all? do |key, val|
34
+ case [key, val]
35
+ in [Symbol | String, ^type]
36
+ true
37
+ else
38
+ false
39
+ end
40
+ end
41
+ else
42
+ false
43
+ end
44
+ end
45
+
46
+ # @param other [Object]
47
+ #
48
+ # @return [Boolean]
49
+ def ==(other) = other.is_a?(FinchAPI::HashOf) && other.nilable? == nilable? && other.item_type == item_type
50
+
51
+ # @api private
52
+ #
53
+ # @param value [Hash{Object=>Object}, Object]
54
+ #
55
+ # @param state [Hash{Symbol=>Object}] .
56
+ #
57
+ # @option state [Boolean, :strong] :strictness
58
+ #
59
+ # @option state [Hash{Symbol=>Object}] :exactness
60
+ #
61
+ # @option state [Integer] :branched
62
+ #
63
+ # @return [Hash{Symbol=>Object}, Object]
64
+ def coerce(value, state:)
65
+ exactness = state.fetch(:exactness)
66
+
67
+ unless value.is_a?(Hash)
68
+ exactness[:no] += 1
69
+ return value
70
+ end
71
+
72
+ target = item_type
73
+ exactness[:yes] += 1
74
+ value
75
+ .to_h do |key, val|
76
+ k = key.is_a?(String) ? key.to_sym : key
77
+ v =
78
+ case [nilable?, val]
79
+ in [true, nil]
80
+ exactness[:yes] += 1
81
+ nil
82
+ else
83
+ FinchAPI::Type::Converter.coerce(target, val, state: state)
84
+ end
85
+
86
+ exactness[:no] += 1 unless k.is_a?(Symbol)
87
+ [k, v]
88
+ end
89
+ end
90
+
91
+ # @api private
92
+ #
93
+ # @param value [Hash{Object=>Object}, Object]
94
+ #
95
+ # @return [Hash{Symbol=>Object}, Object]
96
+ def dump(value)
97
+ target = item_type
98
+ if value.is_a?(Hash)
99
+ value.transform_values do
100
+ FinchAPI::Type::Converter.dump(target, _1)
101
+ end
102
+ else
103
+ super
104
+ end
105
+ end
106
+
107
+ # @api private
108
+ #
109
+ # @return [FinchAPI::Type::Converter, Class]
110
+ protected def item_type = @item_type_fn.call
111
+
112
+ # @api private
113
+ #
114
+ # @return [Boolean]
115
+ protected def nilable? = @nilable
116
+
117
+ # @api private
118
+ #
119
+ # @param type_info [Hash{Symbol=>Object}, Proc, FinchAPI::Type::Converter, Class]
120
+ #
121
+ # @param spec [Hash{Symbol=>Object}] .
122
+ #
123
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
124
+ #
125
+ # @option spec [Proc] :enum
126
+ #
127
+ # @option spec [Proc] :union
128
+ #
129
+ # @option spec [Boolean] :"nil?"
130
+ def initialize(type_info, spec = {})
131
+ @item_type_fn = FinchAPI::Type::Converter.type_info(type_info || spec)
132
+ @nilable = spec[:nil?]
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FinchAPI
4
+ module Type
5
+ # @api private
6
+ module RequestParameters
7
+ # @!parse
8
+ # # Options to specify HTTP behaviour for this request.
9
+ # # @return [FinchAPI::RequestOptions, Hash{Symbol=>Object}]
10
+ # attr_accessor :request_options
11
+
12
+ # @param mod [Module]
13
+ def self.included(mod)
14
+ return unless mod <= FinchAPI::BaseModel
15
+
16
+ mod.extend(FinchAPI::Type::RequestParameters::Converter)
17
+ mod.optional(:request_options, FinchAPI::RequestOptions)
18
+ end
19
+
20
+ # @api private
21
+ module Converter
22
+ # @api private
23
+ #
24
+ # @param params [Object]
25
+ #
26
+ # @return [Array(Object, Hash{Symbol=>Object})]
27
+ def dump_request(params)
28
+ case (dumped = dump(params))
29
+ in Hash
30
+ [dumped.except(:request_options), dumped[:request_options]]
31
+ else
32
+ [dumped, nil]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,204 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FinchAPI
4
+ module Type
5
+ # @api private
6
+ #
7
+ # @example
8
+ # # `document_retreive_response` is a `FinchAPI::Models::HRIS::DocumentRetreiveResponse`
9
+ # case document_retreive_response
10
+ # when FinchAPI::Models::HRIS::W42020
11
+ # puts(document_retreive_response.data)
12
+ # when FinchAPI::Models::HRIS::W42005
13
+ # puts(document_retreive_response.type)
14
+ # else
15
+ # puts(document_retreive_response)
16
+ # end
17
+ #
18
+ # @example
19
+ # case document_retreive_response
20
+ # in {type: :w4_2020, data: data, year: year}
21
+ # puts(data)
22
+ # in {type: :w4_2005, data: data, year: year}
23
+ # puts(year)
24
+ # else
25
+ # puts(document_retreive_response)
26
+ # end
27
+ module Union
28
+ include FinchAPI::Type::Converter
29
+
30
+ # @api private
31
+ #
32
+ # All of the specified variant info for this union.
33
+ #
34
+ # @return [Array<Array(Symbol, Proc)>]
35
+ private def known_variants = (@known_variants ||= [])
36
+
37
+ # @api private
38
+ #
39
+ # @return [Array<Array(Symbol, Object)>]
40
+ protected def derefed_variants
41
+ @known_variants.map { |key, variant_fn| [key, variant_fn.call] }
42
+ end
43
+
44
+ # All of the specified variants for this union.
45
+ #
46
+ # @return [Array<Object>]
47
+ def variants = derefed_variants.map(&:last)
48
+
49
+ # @api private
50
+ #
51
+ # @param property [Symbol]
52
+ private def discriminator(property)
53
+ case property
54
+ in Symbol
55
+ @discriminator = property
56
+ end
57
+ end
58
+
59
+ # @api private
60
+ #
61
+ # @param key [Symbol, Hash{Symbol=>Object}, Proc, FinchAPI::Type::Converter, Class]
62
+ #
63
+ # @param spec [Hash{Symbol=>Object}, Proc, FinchAPI::Type::Converter, Class] .
64
+ #
65
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
66
+ #
67
+ # @option spec [Proc] :enum
68
+ #
69
+ # @option spec [Proc] :union
70
+ #
71
+ # @option spec [Boolean] :"nil?"
72
+ private def variant(key, spec = nil)
73
+ variant_info =
74
+ case key
75
+ in Symbol
76
+ [key, FinchAPI::Type::Converter.type_info(spec)]
77
+ in Proc | FinchAPI::Type::Converter | Class | Hash
78
+ [nil, FinchAPI::Type::Converter.type_info(key)]
79
+ end
80
+
81
+ known_variants << variant_info
82
+ end
83
+
84
+ # @api private
85
+ #
86
+ # @param value [Object]
87
+ #
88
+ # @return [FinchAPI::Type::Converter, Class, nil]
89
+ private def resolve_variant(value)
90
+ case [@discriminator, value]
91
+ in [_, FinchAPI::BaseModel]
92
+ value.class
93
+ in [Symbol, Hash]
94
+ key = value.fetch(@discriminator) do
95
+ value.fetch(@discriminator.to_s, FinchAPI::Util::OMIT)
96
+ end
97
+
98
+ return nil if key == FinchAPI::Util::OMIT
99
+
100
+ key = key.to_sym if key.is_a?(String)
101
+ known_variants.find { |k,| k == key }&.last&.call
102
+ else
103
+ nil
104
+ end
105
+ end
106
+
107
+ # rubocop:disable Style/HashEachMethods
108
+ # rubocop:disable Style/CaseEquality
109
+
110
+ # @param other [Object]
111
+ #
112
+ # @return [Boolean]
113
+ def ===(other)
114
+ known_variants.any? do |_, variant_fn|
115
+ variant_fn.call === other
116
+ end
117
+ end
118
+
119
+ # @param other [Object]
120
+ #
121
+ # @return [Boolean]
122
+ def ==(other)
123
+ other.is_a?(Module) && other.singleton_class <= FinchAPI::Union && other.derefed_variants == derefed_variants
124
+ end
125
+
126
+ # @api private
127
+ #
128
+ # @param value [Object]
129
+ #
130
+ # @param state [Hash{Symbol=>Object}] .
131
+ #
132
+ # @option state [Boolean, :strong] :strictness
133
+ #
134
+ # @option state [Hash{Symbol=>Object}] :exactness
135
+ #
136
+ # @option state [Integer] :branched
137
+ #
138
+ # @return [Object]
139
+ def coerce(value, state:)
140
+ if (target = resolve_variant(value))
141
+ return FinchAPI::Type::Converter.coerce(target, value, state: state)
142
+ end
143
+
144
+ strictness = state.fetch(:strictness)
145
+ exactness = state.fetch(:exactness)
146
+ state[:strictness] = strictness == :strong ? true : strictness
147
+
148
+ alternatives = []
149
+ known_variants.each do |_, variant_fn|
150
+ target = variant_fn.call
151
+ exact = state[:exactness] = {yes: 0, no: 0, maybe: 0}
152
+ state[:branched] += 1
153
+
154
+ coerced = FinchAPI::Type::Converter.coerce(target, value, state: state)
155
+ yes, no, maybe = exact.values
156
+ if (no + maybe).zero? || (!strictness && yes.positive?)
157
+ exact.each { exactness[_1] += _2 }
158
+ state[:exactness] = exactness
159
+ return coerced
160
+ elsif maybe.positive?
161
+ alternatives << [[-yes, -maybe, no], exact, coerced]
162
+ end
163
+ end
164
+
165
+ case alternatives.sort_by(&:first)
166
+ in []
167
+ exactness[:no] += 1
168
+ if strictness == :strong
169
+ message = "no possible conversion of #{value.class} into a variant of #{target.inspect}"
170
+ raise ArgumentError.new(message)
171
+ end
172
+ value
173
+ in [[_, exact, coerced], *]
174
+ exact.each { exactness[_1] += _2 }
175
+ coerced
176
+ end
177
+ .tap { state[:exactness] = exactness }
178
+ ensure
179
+ state[:strictness] = strictness
180
+ end
181
+
182
+ # @api private
183
+ #
184
+ # @param value [Object]
185
+ #
186
+ # @return [Object]
187
+ def dump(value)
188
+ if (target = resolve_variant(value))
189
+ return FinchAPI::Type::Converter.dump(target, value)
190
+ end
191
+
192
+ known_variants.each do
193
+ target = _2.call
194
+ return FinchAPI::Type::Converter.dump(target, value) if target === value
195
+ end
196
+
197
+ super
198
+ end
199
+
200
+ # rubocop:enable Style/CaseEquality
201
+ # rubocop:enable Style/HashEachMethods
202
+ end
203
+ end
204
+ end