e-invoice-api 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 (233) hide show
  1. checksums.yaml +7 -0
  2. data/.ignore +2 -0
  3. data/CHANGELOG.md +57 -0
  4. data/README.md +285 -0
  5. data/SECURITY.md +27 -0
  6. data/lib/e_invoice_api/client.rb +94 -0
  7. data/lib/e_invoice_api/errors.rb +192 -0
  8. data/lib/e_invoice_api/file_part.rb +55 -0
  9. data/lib/e_invoice_api/internal/documents_number_page.rb +96 -0
  10. data/lib/e_invoice_api/internal/transport/base_client.rb +555 -0
  11. data/lib/e_invoice_api/internal/transport/pooled_net_requester.rb +209 -0
  12. data/lib/e_invoice_api/internal/type/array_of.rb +162 -0
  13. data/lib/e_invoice_api/internal/type/base_model.rb +482 -0
  14. data/lib/e_invoice_api/internal/type/base_page.rb +55 -0
  15. data/lib/e_invoice_api/internal/type/boolean.rb +71 -0
  16. data/lib/e_invoice_api/internal/type/converter.rb +292 -0
  17. data/lib/e_invoice_api/internal/type/enum.rb +148 -0
  18. data/lib/e_invoice_api/internal/type/file_input.rb +103 -0
  19. data/lib/e_invoice_api/internal/type/hash_of.rb +182 -0
  20. data/lib/e_invoice_api/internal/type/request_parameters.rb +42 -0
  21. data/lib/e_invoice_api/internal/type/union.rb +236 -0
  22. data/lib/e_invoice_api/internal/type/unknown.rb +75 -0
  23. data/lib/e_invoice_api/internal/util.rb +914 -0
  24. data/lib/e_invoice_api/internal.rb +20 -0
  25. data/lib/e_invoice_api/models/certificate.rb +34 -0
  26. data/lib/e_invoice_api/models/currency_code.rb +28 -0
  27. data/lib/e_invoice_api/models/document_attachment_create.rb +33 -0
  28. data/lib/e_invoice_api/models/document_create.rb +521 -0
  29. data/lib/e_invoice_api/models/document_create_params.rb +14 -0
  30. data/lib/e_invoice_api/models/document_delete_params.rb +14 -0
  31. data/lib/e_invoice_api/models/document_delete_response.rb +16 -0
  32. data/lib/e_invoice_api/models/document_direction.rb +15 -0
  33. data/lib/e_invoice_api/models/document_response.rb +418 -0
  34. data/lib/e_invoice_api/models/document_retrieve_params.rb +14 -0
  35. data/lib/e_invoice_api/models/document_send_params.rb +44 -0
  36. data/lib/e_invoice_api/models/document_state.rb +18 -0
  37. data/lib/e_invoice_api/models/document_type.rb +15 -0
  38. data/lib/e_invoice_api/models/documents/attachment_add_params.rb +22 -0
  39. data/lib/e_invoice_api/models/documents/attachment_delete_params.rb +22 -0
  40. data/lib/e_invoice_api/models/documents/attachment_delete_response.rb +18 -0
  41. data/lib/e_invoice_api/models/documents/attachment_list_params.rb +16 -0
  42. data/lib/e_invoice_api/models/documents/attachment_list_response.rb +11 -0
  43. data/lib/e_invoice_api/models/documents/attachment_retrieve_params.rb +22 -0
  44. data/lib/e_invoice_api/models/documents/document_attachment.rb +44 -0
  45. data/lib/e_invoice_api/models/documents/ubl_get_params.rb +16 -0
  46. data/lib/e_invoice_api/models/documents/ubl_get_response.rb +72 -0
  47. data/lib/e_invoice_api/models/inbox_list_credit_notes_params.rb +30 -0
  48. data/lib/e_invoice_api/models/inbox_list_invoices_params.rb +30 -0
  49. data/lib/e_invoice_api/models/inbox_list_params.rb +78 -0
  50. data/lib/e_invoice_api/models/lookup_retrieve_params.rb +26 -0
  51. data/lib/e_invoice_api/models/lookup_retrieve_response.rb +491 -0
  52. data/lib/e_invoice_api/models/outbox_list_draft_documents_params.rb +30 -0
  53. data/lib/e_invoice_api/models/outbox_list_received_documents_params.rb +78 -0
  54. data/lib/e_invoice_api/models/paginated_document_response.rb +39 -0
  55. data/lib/e_invoice_api/models/payment_detail_create.rb +33 -0
  56. data/lib/e_invoice_api/models/ubl_document_validation.rb +97 -0
  57. data/lib/e_invoice_api/models/unit_of_measure_code.rb +104 -0
  58. data/lib/e_invoice_api/models/validate_validate_json_params.rb +14 -0
  59. data/lib/e_invoice_api/models/validate_validate_peppol_id_params.rb +26 -0
  60. data/lib/e_invoice_api/models/validate_validate_peppol_id_response.rb +80 -0
  61. data/lib/e_invoice_api/models/validate_validate_ubl_params.rb +20 -0
  62. data/lib/e_invoice_api/models/webhook_create_params.rb +32 -0
  63. data/lib/e_invoice_api/models/webhook_delete_params.rb +14 -0
  64. data/lib/e_invoice_api/models/webhook_delete_response.rb +18 -0
  65. data/lib/e_invoice_api/models/webhook_list_params.rb +14 -0
  66. data/lib/e_invoice_api/models/webhook_list_response.rb +8 -0
  67. data/lib/e_invoice_api/models/webhook_response.rb +42 -0
  68. data/lib/e_invoice_api/models/webhook_retrieve_params.rb +14 -0
  69. data/lib/e_invoice_api/models/webhook_update_params.rb +32 -0
  70. data/lib/e_invoice_api/models.rb +105 -0
  71. data/lib/e_invoice_api/request_options.rb +77 -0
  72. data/lib/e_invoice_api/resources/documents/attachments.rb +109 -0
  73. data/lib/e_invoice_api/resources/documents/ubl.rb +35 -0
  74. data/lib/e_invoice_api/resources/documents.rb +188 -0
  75. data/lib/e_invoice_api/resources/inbox.rb +101 -0
  76. data/lib/e_invoice_api/resources/lookup.rb +43 -0
  77. data/lib/e_invoice_api/resources/outbox.rb +76 -0
  78. data/lib/e_invoice_api/resources/validate.rb +168 -0
  79. data/lib/e_invoice_api/resources/webhooks.rb +117 -0
  80. data/lib/e_invoice_api/version.rb +5 -0
  81. data/lib/e_invoice_api.rb +107 -0
  82. data/manifest.yaml +15 -0
  83. data/rbi/e_invoice_api/client.rbi +64 -0
  84. data/rbi/e_invoice_api/errors.rbi +162 -0
  85. data/rbi/e_invoice_api/file_part.rbi +37 -0
  86. data/rbi/e_invoice_api/internal/documents_number_page.rbi +28 -0
  87. data/rbi/e_invoice_api/internal/transport/base_client.rbi +298 -0
  88. data/rbi/e_invoice_api/internal/transport/pooled_net_requester.rbi +80 -0
  89. data/rbi/e_invoice_api/internal/type/array_of.rbi +104 -0
  90. data/rbi/e_invoice_api/internal/type/base_model.rbi +304 -0
  91. data/rbi/e_invoice_api/internal/type/base_page.rbi +43 -0
  92. data/rbi/e_invoice_api/internal/type/boolean.rbi +56 -0
  93. data/rbi/e_invoice_api/internal/type/converter.rbi +162 -0
  94. data/rbi/e_invoice_api/internal/type/enum.rbi +82 -0
  95. data/rbi/e_invoice_api/internal/type/file_input.rbi +59 -0
  96. data/rbi/e_invoice_api/internal/type/hash_of.rbi +104 -0
  97. data/rbi/e_invoice_api/internal/type/request_parameters.rbi +31 -0
  98. data/rbi/e_invoice_api/internal/type/union.rbi +116 -0
  99. data/rbi/e_invoice_api/internal/type/unknown.rbi +56 -0
  100. data/rbi/e_invoice_api/internal/util.rbi +487 -0
  101. data/rbi/e_invoice_api/internal.rbi +18 -0
  102. data/rbi/e_invoice_api/models/certificate.rbi +54 -0
  103. data/rbi/e_invoice_api/models/currency_code.rbi +34 -0
  104. data/rbi/e_invoice_api/models/document_attachment_create.rbi +57 -0
  105. data/rbi/e_invoice_api/models/document_create.rbi +638 -0
  106. data/rbi/e_invoice_api/models/document_create_params.rbi +30 -0
  107. data/rbi/e_invoice_api/models/document_delete_params.rbi +30 -0
  108. data/rbi/e_invoice_api/models/document_delete_response.rbi +26 -0
  109. data/rbi/e_invoice_api/models/document_direction.rbi +22 -0
  110. data/rbi/e_invoice_api/models/document_response.rbi +504 -0
  111. data/rbi/e_invoice_api/models/document_retrieve_params.rbi +30 -0
  112. data/rbi/e_invoice_api/models/document_send_params.rbi +65 -0
  113. data/rbi/e_invoice_api/models/document_state.rbi +24 -0
  114. data/rbi/e_invoice_api/models/document_type.rbi +21 -0
  115. data/rbi/e_invoice_api/models/documents/attachment_add_params.rbi +43 -0
  116. data/rbi/e_invoice_api/models/documents/attachment_delete_params.rbi +43 -0
  117. data/rbi/e_invoice_api/models/documents/attachment_delete_response.rbi +28 -0
  118. data/rbi/e_invoice_api/models/documents/attachment_list_params.rbi +34 -0
  119. data/rbi/e_invoice_api/models/documents/attachment_list_response.rbi +15 -0
  120. data/rbi/e_invoice_api/models/documents/attachment_retrieve_params.rbi +43 -0
  121. data/rbi/e_invoice_api/models/documents/document_attachment.rbi +72 -0
  122. data/rbi/e_invoice_api/models/documents/ubl_get_params.rbi +34 -0
  123. data/rbi/e_invoice_api/models/documents/ubl_get_response.rbi +97 -0
  124. data/rbi/e_invoice_api/models/inbox_list_credit_notes_params.rbi +60 -0
  125. data/rbi/e_invoice_api/models/inbox_list_invoices_params.rbi +60 -0
  126. data/rbi/e_invoice_api/models/inbox_list_params.rbi +105 -0
  127. data/rbi/e_invoice_api/models/lookup_retrieve_params.rbi +45 -0
  128. data/rbi/e_invoice_api/models/lookup_retrieve_response.rbi +852 -0
  129. data/rbi/e_invoice_api/models/outbox_list_draft_documents_params.rbi +60 -0
  130. data/rbi/e_invoice_api/models/outbox_list_received_documents_params.rbi +108 -0
  131. data/rbi/e_invoice_api/models/paginated_document_response.rbi +56 -0
  132. data/rbi/e_invoice_api/models/payment_detail_create.rbi +56 -0
  133. data/rbi/e_invoice_api/models/ubl_document_validation.rbi +159 -0
  134. data/rbi/e_invoice_api/models/unit_of_measure_code.rbi +149 -0
  135. data/rbi/e_invoice_api/models/validate_validate_json_params.rbi +30 -0
  136. data/rbi/e_invoice_api/models/validate_validate_peppol_id_params.rbi +45 -0
  137. data/rbi/e_invoice_api/models/validate_validate_peppol_id_response.rbi +141 -0
  138. data/rbi/e_invoice_api/models/validate_validate_ubl_params.rbi +41 -0
  139. data/rbi/e_invoice_api/models/webhook_create_params.rbi +54 -0
  140. data/rbi/e_invoice_api/models/webhook_delete_params.rbi +30 -0
  141. data/rbi/e_invoice_api/models/webhook_delete_response.rbi +27 -0
  142. data/rbi/e_invoice_api/models/webhook_list_params.rbi +27 -0
  143. data/rbi/e_invoice_api/models/webhook_list_response.rbi +11 -0
  144. data/rbi/e_invoice_api/models/webhook_response.rbi +57 -0
  145. data/rbi/e_invoice_api/models/webhook_retrieve_params.rbi +30 -0
  146. data/rbi/e_invoice_api/models/webhook_update_params.rbi +51 -0
  147. data/rbi/e_invoice_api/models.rbi +70 -0
  148. data/rbi/e_invoice_api/request_options.rbi +59 -0
  149. data/rbi/e_invoice_api/resources/documents/attachments.rbi +58 -0
  150. data/rbi/e_invoice_api/resources/documents/ubl.rbi +24 -0
  151. data/rbi/e_invoice_api/resources/documents.rbi +164 -0
  152. data/rbi/e_invoice_api/resources/inbox.rbi +93 -0
  153. data/rbi/e_invoice_api/resources/lookup.rbi +31 -0
  154. data/rbi/e_invoice_api/resources/outbox.rbi +72 -0
  155. data/rbi/e_invoice_api/resources/validate.rbi +145 -0
  156. data/rbi/e_invoice_api/resources/webhooks.rbi +72 -0
  157. data/rbi/e_invoice_api/version.rbi +5 -0
  158. data/sig/e_invoice_api/client.rbs +36 -0
  159. data/sig/e_invoice_api/errors.rbs +101 -0
  160. data/sig/e_invoice_api/file_part.rbs +21 -0
  161. data/sig/e_invoice_api/internal/documents_number_page.rbs +17 -0
  162. data/sig/e_invoice_api/internal/transport/base_client.rbs +131 -0
  163. data/sig/e_invoice_api/internal/transport/pooled_net_requester.rbs +45 -0
  164. data/sig/e_invoice_api/internal/type/array_of.rbs +48 -0
  165. data/sig/e_invoice_api/internal/type/base_model.rbs +102 -0
  166. data/sig/e_invoice_api/internal/type/base_page.rbs +24 -0
  167. data/sig/e_invoice_api/internal/type/boolean.rbs +26 -0
  168. data/sig/e_invoice_api/internal/type/converter.rbs +56 -0
  169. data/sig/e_invoice_api/internal/type/enum.rbs +32 -0
  170. data/sig/e_invoice_api/internal/type/file_input.rbs +25 -0
  171. data/sig/e_invoice_api/internal/type/hash_of.rbs +48 -0
  172. data/sig/e_invoice_api/internal/type/request_parameters.rbs +19 -0
  173. data/sig/e_invoice_api/internal/type/union.rbs +52 -0
  174. data/sig/e_invoice_api/internal/type/unknown.rbs +26 -0
  175. data/sig/e_invoice_api/internal/util.rbs +185 -0
  176. data/sig/e_invoice_api/internal.rbs +9 -0
  177. data/sig/e_invoice_api/models/certificate.rbs +26 -0
  178. data/sig/e_invoice_api/models/currency_code.rbs +42 -0
  179. data/sig/e_invoice_api/models/document_attachment_create.rbs +39 -0
  180. data/sig/e_invoice_api/models/document_create.rbs +403 -0
  181. data/sig/e_invoice_api/models/document_create_params.rbs +15 -0
  182. data/sig/e_invoice_api/models/document_delete_params.rbs +15 -0
  183. data/sig/e_invoice_api/models/document_delete_response.rbs +13 -0
  184. data/sig/e_invoice_api/models/document_direction.rbs +14 -0
  185. data/sig/e_invoice_api/models/document_response.rbs +358 -0
  186. data/sig/e_invoice_api/models/document_retrieve_params.rbs +15 -0
  187. data/sig/e_invoice_api/models/document_send_params.rbs +46 -0
  188. data/sig/e_invoice_api/models/document_state.rbs +17 -0
  189. data/sig/e_invoice_api/models/document_type.rbs +14 -0
  190. data/sig/e_invoice_api/models/documents/attachment_add_params.rbs +26 -0
  191. data/sig/e_invoice_api/models/documents/attachment_delete_params.rbs +26 -0
  192. data/sig/e_invoice_api/models/documents/attachment_delete_response.rbs +15 -0
  193. data/sig/e_invoice_api/models/documents/attachment_list_params.rbs +17 -0
  194. data/sig/e_invoice_api/models/documents/attachment_list_response.rbs +10 -0
  195. data/sig/e_invoice_api/models/documents/attachment_retrieve_params.rbs +26 -0
  196. data/sig/e_invoice_api/models/documents/document_attachment.rbs +48 -0
  197. data/sig/e_invoice_api/models/documents/ubl_get_params.rbs +17 -0
  198. data/sig/e_invoice_api/models/documents/ubl_get_response.rbs +69 -0
  199. data/sig/e_invoice_api/models/inbox_list_credit_notes_params.rbs +32 -0
  200. data/sig/e_invoice_api/models/inbox_list_invoices_params.rbs +32 -0
  201. data/sig/e_invoice_api/models/inbox_list_params.rbs +65 -0
  202. data/sig/e_invoice_api/models/lookup_retrieve_params.rbs +23 -0
  203. data/sig/e_invoice_api/models/lookup_retrieve_response.rbs +365 -0
  204. data/sig/e_invoice_api/models/outbox_list_draft_documents_params.rbs +32 -0
  205. data/sig/e_invoice_api/models/outbox_list_received_documents_params.rbs +65 -0
  206. data/sig/e_invoice_api/models/paginated_document_response.rbs +40 -0
  207. data/sig/e_invoice_api/models/payment_detail_create.rbs +35 -0
  208. data/sig/e_invoice_api/models/ubl_document_validation.rbs +98 -0
  209. data/sig/e_invoice_api/models/unit_of_measure_code.rbs +192 -0
  210. data/sig/e_invoice_api/models/validate_validate_json_params.rbs +15 -0
  211. data/sig/e_invoice_api/models/validate_validate_peppol_id_params.rbs +23 -0
  212. data/sig/e_invoice_api/models/validate_validate_peppol_id_response.rbs +65 -0
  213. data/sig/e_invoice_api/models/validate_validate_ubl_params.rbs +24 -0
  214. data/sig/e_invoice_api/models/webhook_create_params.rbs +34 -0
  215. data/sig/e_invoice_api/models/webhook_delete_params.rbs +15 -0
  216. data/sig/e_invoice_api/models/webhook_delete_response.rbs +13 -0
  217. data/sig/e_invoice_api/models/webhook_list_params.rbs +15 -0
  218. data/sig/e_invoice_api/models/webhook_list_response.rbs +7 -0
  219. data/sig/e_invoice_api/models/webhook_response.rbs +42 -0
  220. data/sig/e_invoice_api/models/webhook_retrieve_params.rbs +15 -0
  221. data/sig/e_invoice_api/models/webhook_update_params.rbs +32 -0
  222. data/sig/e_invoice_api/models.rbs +65 -0
  223. data/sig/e_invoice_api/request_options.rbs +36 -0
  224. data/sig/e_invoice_api/resources/documents/attachments.rbs +32 -0
  225. data/sig/e_invoice_api/resources/documents/ubl.rbs +14 -0
  226. data/sig/e_invoice_api/resources/documents.rbs +76 -0
  227. data/sig/e_invoice_api/resources/inbox.rbs +31 -0
  228. data/sig/e_invoice_api/resources/lookup.rbs +12 -0
  229. data/sig/e_invoice_api/resources/outbox.rbs +25 -0
  230. data/sig/e_invoice_api/resources/validate.rbs +62 -0
  231. data/sig/e_invoice_api/resources/webhooks.rbs +36 -0
  232. data/sig/e_invoice_api/version.rbs +3 -0
  233. metadata +290 -0
@@ -0,0 +1,482 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EInvoiceAPI
4
+ module Internal
5
+ module Type
6
+ # @abstract
7
+ class BaseModel
8
+ extend EInvoiceAPI::Internal::Type::Converter
9
+ extend EInvoiceAPI::Internal::Util::SorbetRuntimeSupport
10
+
11
+ class << self
12
+ # @api private
13
+ #
14
+ # Assumes superclass fields are totally defined before fields are accessed /
15
+ # defined on subclasses.
16
+ #
17
+ # @param child [Class<EInvoiceAPI::Internal::Type::BaseModel>]
18
+ def inherited(child)
19
+ super
20
+ child.known_fields.replace(known_fields.dup)
21
+ end
22
+
23
+ # @api private
24
+ #
25
+ # @return [Hash{Symbol=>Hash{Symbol=>Object}}]
26
+ def known_fields = @known_fields ||= {}
27
+
28
+ # @api private
29
+ #
30
+ # @return [Hash{Symbol=>Hash{Symbol=>Object}}]
31
+ def fields
32
+ known_fields.transform_values do |field|
33
+ {**field.except(:type_fn), type: field.fetch(:type_fn).call}
34
+ end
35
+ end
36
+
37
+ # @api private
38
+ #
39
+ # @param name_sym [Symbol]
40
+ #
41
+ # @param required [Boolean]
42
+ #
43
+ # @param type_info [Hash{Symbol=>Object}, Proc, EInvoiceAPI::Internal::Type::Converter, Class]
44
+ #
45
+ # @param spec [Hash{Symbol=>Object}] .
46
+ #
47
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
48
+ #
49
+ # @option spec [Proc] :enum
50
+ #
51
+ # @option spec [Proc] :union
52
+ #
53
+ # @option spec [Boolean] :"nil?"
54
+ private def add_field(name_sym, required:, type_info:, spec:)
55
+ type_fn, info =
56
+ case type_info
57
+ in Proc | EInvoiceAPI::Internal::Type::Converter | Class
58
+ [EInvoiceAPI::Internal::Type::Converter.type_info({**spec, union: type_info}), spec]
59
+ in Hash
60
+ [EInvoiceAPI::Internal::Type::Converter.type_info(type_info), type_info]
61
+ end
62
+
63
+ setter = "#{name_sym}="
64
+ api_name = info.fetch(:api_name, name_sym)
65
+ nilable = info.fetch(:nil?, false)
66
+ const = if required && !nilable
67
+ info.fetch(
68
+ :const,
69
+ EInvoiceAPI::Internal::OMIT
70
+ )
71
+ else
72
+ EInvoiceAPI::Internal::OMIT
73
+ end
74
+
75
+ [name_sym, setter].each { undef_method(_1) } if known_fields.key?(name_sym)
76
+
77
+ known_fields[name_sym] =
78
+ {
79
+ mode: @mode,
80
+ api_name: api_name,
81
+ required: required,
82
+ nilable: nilable,
83
+ const: const,
84
+ type_fn: type_fn
85
+ }
86
+
87
+ define_method(setter) { @data.store(name_sym, _1) }
88
+
89
+ define_method(name_sym) do
90
+ target = type_fn.call
91
+ value = @data.fetch(name_sym) { const == EInvoiceAPI::Internal::OMIT ? nil : const }
92
+ state = {strictness: :strong, exactness: {yes: 0, no: 0, maybe: 0}, branched: 0}
93
+ if (nilable || !required) && value.nil?
94
+ nil
95
+ else
96
+ EInvoiceAPI::Internal::Type::Converter.coerce(
97
+ target, value, state: state
98
+ )
99
+ end
100
+ rescue StandardError => e
101
+ cls = self.class.name.split("::").last
102
+ message = [
103
+ "Failed to parse #{cls}.#{__method__} from #{value.class} to #{target.inspect}.",
104
+ "To get the unparsed API response, use #{cls}[#{__method__.inspect}].",
105
+ "Cause: #{e.message}"
106
+ ].join(" ")
107
+ raise EInvoiceAPI::Errors::ConversionError.new(message)
108
+ end
109
+ end
110
+
111
+ # @api private
112
+ #
113
+ # @param name_sym [Symbol]
114
+ #
115
+ # @param type_info [Hash{Symbol=>Object}, Proc, EInvoiceAPI::Internal::Type::Converter, Class]
116
+ #
117
+ # @param spec [Hash{Symbol=>Object}] .
118
+ #
119
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
120
+ #
121
+ # @option spec [Proc] :enum
122
+ #
123
+ # @option spec [Proc] :union
124
+ #
125
+ # @option spec [Boolean] :"nil?"
126
+ def required(name_sym, type_info, spec = {})
127
+ add_field(name_sym, required: true, type_info: type_info, spec: spec)
128
+ end
129
+
130
+ # @api private
131
+ #
132
+ # @param name_sym [Symbol]
133
+ #
134
+ # @param type_info [Hash{Symbol=>Object}, Proc, EInvoiceAPI::Internal::Type::Converter, Class]
135
+ #
136
+ # @param spec [Hash{Symbol=>Object}] .
137
+ #
138
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
139
+ #
140
+ # @option spec [Proc] :enum
141
+ #
142
+ # @option spec [Proc] :union
143
+ #
144
+ # @option spec [Boolean] :"nil?"
145
+ def optional(name_sym, type_info, spec = {})
146
+ add_field(name_sym, required: false, type_info: type_info, spec: spec)
147
+ end
148
+
149
+ # @api private
150
+ #
151
+ # `request_only` attributes not excluded from `.#coerce` when receiving responses
152
+ # even if well behaved servers should not send them
153
+ #
154
+ # @param blk [Proc]
155
+ private def request_only(&blk)
156
+ @mode = :dump
157
+ blk.call
158
+ ensure
159
+ @mode = nil
160
+ end
161
+
162
+ # @api private
163
+ #
164
+ # `response_only` attributes are omitted from `.#dump` when making requests
165
+ #
166
+ # @param blk [Proc]
167
+ private def response_only(&blk)
168
+ @mode = :coerce
169
+ blk.call
170
+ ensure
171
+ @mode = nil
172
+ end
173
+
174
+ # @api public
175
+ #
176
+ # @param other [Object]
177
+ #
178
+ # @return [Boolean]
179
+ def ==(other)
180
+ other.is_a?(Class) && other <= EInvoiceAPI::Internal::Type::BaseModel && other.fields == fields
181
+ end
182
+
183
+ # @api public
184
+ #
185
+ # @return [Integer]
186
+ def hash = fields.hash
187
+ end
188
+
189
+ # @api public
190
+ #
191
+ # @param other [Object]
192
+ #
193
+ # @return [Boolean]
194
+ def ==(other) = self.class == other.class && @data == other.to_h
195
+
196
+ # @api public
197
+ #
198
+ # @return [Integer]
199
+ def hash = [self.class, @data].hash
200
+
201
+ class << self
202
+ # @api private
203
+ #
204
+ # @param value [EInvoiceAPI::Internal::Type::BaseModel, Hash{Object=>Object}, Object]
205
+ #
206
+ # @param state [Hash{Symbol=>Object}] .
207
+ #
208
+ # @option state [Boolean, :strong] :strictness
209
+ #
210
+ # @option state [Hash{Symbol=>Object}] :exactness
211
+ #
212
+ # @option state [Integer] :branched
213
+ #
214
+ # @return [self, Object]
215
+ def coerce(value, state:)
216
+ exactness = state.fetch(:exactness)
217
+
218
+ if value.is_a?(self.class)
219
+ exactness[:yes] += 1
220
+ return value
221
+ end
222
+
223
+ unless (val = EInvoiceAPI::Internal::Util.coerce_hash(value)).is_a?(Hash)
224
+ exactness[:no] += 1
225
+ return value
226
+ end
227
+ exactness[:yes] += 1
228
+
229
+ keys = val.keys.to_set
230
+ instance = new
231
+ data = instance.to_h
232
+
233
+ # rubocop:disable Metrics/BlockLength
234
+ fields.each do |name, field|
235
+ mode, required, target = field.fetch_values(:mode, :required, :type)
236
+ api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
237
+
238
+ unless val.key?(api_name)
239
+ if required && mode != :dump && const == EInvoiceAPI::Internal::OMIT
240
+ exactness[nilable ? :maybe : :no] += 1
241
+ else
242
+ exactness[:yes] += 1
243
+ end
244
+ next
245
+ end
246
+
247
+ item = val.fetch(api_name)
248
+ keys.delete(api_name)
249
+
250
+ converted =
251
+ if item.nil? && (nilable || !required)
252
+ exactness[nilable ? :yes : :maybe] += 1
253
+ nil
254
+ else
255
+ coerced = EInvoiceAPI::Internal::Type::Converter.coerce(target, item, state: state)
256
+ case target
257
+ in EInvoiceAPI::Internal::Type::Converter | Symbol
258
+ coerced
259
+ else
260
+ item
261
+ end
262
+ end
263
+ data.store(name, converted)
264
+ end
265
+ # rubocop:enable Metrics/BlockLength
266
+
267
+ keys.each { data.store(_1, val.fetch(_1)) }
268
+ instance
269
+ end
270
+
271
+ # @api private
272
+ #
273
+ # @param value [self, Object]
274
+ #
275
+ # @param state [Hash{Symbol=>Object}] .
276
+ #
277
+ # @option state [Boolean] :can_retry
278
+ #
279
+ # @return [Hash{Object=>Object}, Object]
280
+ def dump(value, state:)
281
+ unless (coerced = EInvoiceAPI::Internal::Util.coerce_hash(value)).is_a?(Hash)
282
+ return super
283
+ end
284
+
285
+ acc = {}
286
+
287
+ coerced.each do |key, val|
288
+ name = key.is_a?(String) ? key.to_sym : key
289
+ case (field = known_fields[name])
290
+ in nil
291
+ acc.store(name, super(val, state: state))
292
+ else
293
+ api_name, mode, type_fn = field.fetch_values(:api_name, :mode, :type_fn)
294
+ case mode
295
+ in :coerce
296
+ next
297
+ else
298
+ target = type_fn.call
299
+ acc.store(api_name, EInvoiceAPI::Internal::Type::Converter.dump(target, val, state: state))
300
+ end
301
+ end
302
+ end
303
+
304
+ known_fields.each_value do |field|
305
+ api_name, mode, const = field.fetch_values(:api_name, :mode, :const)
306
+ next if mode == :coerce || acc.key?(api_name) || const == EInvoiceAPI::Internal::OMIT
307
+ acc.store(api_name, const)
308
+ end
309
+
310
+ acc
311
+ end
312
+
313
+ # @api private
314
+ #
315
+ # @return [Object]
316
+ def to_sorbet_type
317
+ self
318
+ end
319
+ end
320
+
321
+ class << self
322
+ # @api private
323
+ #
324
+ # @param model [EInvoiceAPI::Internal::Type::BaseModel]
325
+ # @param convert [Boolean]
326
+ #
327
+ # @return [Hash{Symbol=>Object}]
328
+ def recursively_to_h(model, convert:)
329
+ rec = ->(x) do
330
+ case x
331
+ in EInvoiceAPI::Internal::Type::BaseModel
332
+ if convert
333
+ fields = x.class.known_fields
334
+ x.to_h.to_h do |key, val|
335
+ [key, rec.call(fields.key?(key) ? x.public_send(key) : val)]
336
+ rescue EInvoiceAPI::Errors::ConversionError
337
+ [key, rec.call(val)]
338
+ end
339
+ else
340
+ rec.call(x.to_h)
341
+ end
342
+ in Hash
343
+ x.transform_values(&rec)
344
+ in Array
345
+ x.map(&rec)
346
+ else
347
+ x
348
+ end
349
+ end
350
+ rec.call(model)
351
+ end
352
+ end
353
+
354
+ # @api public
355
+ #
356
+ # Returns the raw value associated with the given key, if found. Otherwise, nil is
357
+ # returned.
358
+ #
359
+ # It is valid to lookup keys that are not in the API spec, for example to access
360
+ # undocumented features. This method does not parse response data into
361
+ # higher-level types. Lookup by anything other than a Symbol is an ArgumentError.
362
+ #
363
+ # @param key [Symbol]
364
+ #
365
+ # @return [Object, nil]
366
+ def [](key)
367
+ unless key.instance_of?(Symbol)
368
+ raise ArgumentError.new("Expected symbol key for lookup, got #{key.inspect}")
369
+ end
370
+
371
+ @data[key]
372
+ end
373
+
374
+ # @api public
375
+ #
376
+ # Returns a Hash of the data underlying this object. O(1)
377
+ #
378
+ # Keys are Symbols and values are the raw values from the response. The return
379
+ # value indicates which values were ever set on the object. i.e. there will be a
380
+ # key in this hash if they ever were, even if the set value was nil.
381
+ #
382
+ # This method is not recursive. The returned value is shared by the object, so it
383
+ # should not be mutated.
384
+ #
385
+ # @return [Hash{Symbol=>Object}]
386
+ def to_h = @data
387
+
388
+ alias_method :to_hash, :to_h
389
+
390
+ # @api public
391
+ #
392
+ # In addition to the behaviour of `#to_h`, this method will recursively call
393
+ # `#to_h` on nested models.
394
+ #
395
+ # @return [Hash{Symbol=>Object}]
396
+ def deep_to_h = self.class.recursively_to_h(@data, convert: false)
397
+
398
+ # @param keys [Array<Symbol>, nil]
399
+ #
400
+ # @return [Hash{Symbol=>Object}]
401
+ #
402
+ # @example
403
+ # # `document_attachment_create` is a `EInvoiceAPI::DocumentAttachmentCreate`
404
+ # document_attachment_create => {
405
+ # file_name: file_name,
406
+ # file_data: file_data,
407
+ # file_size: file_size
408
+ # }
409
+ def deconstruct_keys(keys)
410
+ (keys || self.class.known_fields.keys)
411
+ .filter_map do |k|
412
+ unless self.class.known_fields.key?(k)
413
+ next
414
+ end
415
+
416
+ [k, public_send(k)]
417
+ end
418
+ .to_h
419
+ end
420
+
421
+ # @api public
422
+ #
423
+ # @param a [Object]
424
+ #
425
+ # @return [String]
426
+ def to_json(*a) = EInvoiceAPI::Internal::Type::Converter.dump(self.class, self).to_json(*a)
427
+
428
+ # @api public
429
+ #
430
+ # @param a [Object]
431
+ #
432
+ # @return [String]
433
+ def to_yaml(*a) = EInvoiceAPI::Internal::Type::Converter.dump(self.class, self).to_yaml(*a)
434
+
435
+ # Create a new instance of a model.
436
+ #
437
+ # @param data [Hash{Symbol=>Object}, self]
438
+ def initialize(data = {}) = (@data = EInvoiceAPI::Internal::Util.coerce_hash!(data).to_h)
439
+
440
+ class << self
441
+ # @api private
442
+ #
443
+ # @param depth [Integer]
444
+ #
445
+ # @return [String]
446
+ def inspect(depth: 0)
447
+ return super() if depth.positive?
448
+
449
+ depth = depth.succ
450
+ deferred = fields.transform_values do |field|
451
+ type, required, nilable = field.fetch_values(:type, :required, :nilable)
452
+ inspected = [
453
+ EInvoiceAPI::Internal::Type::Converter.inspect(type, depth: depth),
454
+ !required || nilable ? "nil" : nil
455
+ ].compact.join(" | ")
456
+ -> { inspected }.tap { _1.define_singleton_method(:inspect) { call } }
457
+ end
458
+
459
+ "#{name}[#{deferred.inspect}]"
460
+ end
461
+ end
462
+
463
+ # @api public
464
+ #
465
+ # @return [String]
466
+ def to_s = deep_to_h.to_s
467
+
468
+ # @api private
469
+ #
470
+ # @return [String]
471
+ def inspect
472
+ converted = self.class.recursively_to_h(self, convert: true)
473
+ "#<#{self.class}:0x#{object_id.to_s(16)} #{converted}>"
474
+ end
475
+
476
+ define_sorbet_constant!(:KnownField) do
477
+ T.type_alias { {mode: T.nilable(Symbol), required: T::Boolean, nilable: T::Boolean} }
478
+ end
479
+ end
480
+ end
481
+ end
482
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EInvoiceAPI
4
+ module Internal
5
+ module Type
6
+ # @api private
7
+ #
8
+ # @generic Elem
9
+ #
10
+ # This module provides a base implementation for paginated responses in the SDK.
11
+ module BasePage
12
+ # rubocop:disable Lint/UnusedMethodArgument
13
+
14
+ # @api public
15
+ #
16
+ # @return [Boolean]
17
+ def next_page? = (raise NotImplementedError)
18
+
19
+ # @api public
20
+ #
21
+ # @raise [EInvoiceAPI::Errors::APIError]
22
+ # @return [self]
23
+ def next_page = (raise NotImplementedError)
24
+
25
+ # @api public
26
+ #
27
+ # @param blk [Proc]
28
+ #
29
+ # @yieldparam [generic<Elem>]
30
+ # @return [void]
31
+ def auto_paging_each(&blk) = (raise NotImplementedError)
32
+
33
+ # @return [Enumerable<generic<Elem>>]
34
+ def to_enum = super(:auto_paging_each)
35
+
36
+ alias_method :enum_for, :to_enum
37
+
38
+ # @api private
39
+ #
40
+ # @param client [EInvoiceAPI::Internal::Transport::BaseClient]
41
+ # @param req [Hash{Symbol=>Object}]
42
+ # @param headers [Hash{String=>String}, Net::HTTPHeader]
43
+ # @param page_data [Object]
44
+ def initialize(client:, req:, headers:, page_data:)
45
+ @client = client
46
+ @req = req
47
+ @model = req.fetch(:model)
48
+ super()
49
+ end
50
+
51
+ # rubocop:enable Lint/UnusedMethodArgument
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EInvoiceAPI
4
+ module Internal
5
+ module Type
6
+ # @api private
7
+ #
8
+ # @abstract
9
+ #
10
+ # Ruby has no Boolean class; this is something for models to refer to.
11
+ class Boolean
12
+ extend EInvoiceAPI::Internal::Type::Converter
13
+ extend EInvoiceAPI::Internal::Util::SorbetRuntimeSupport
14
+
15
+ private_class_method :new
16
+
17
+ # @api public
18
+ #
19
+ # @param other [Object]
20
+ #
21
+ # @return [Boolean]
22
+ def self.===(other) = other == true || other == false
23
+
24
+ # @api public
25
+ #
26
+ # @param other [Object]
27
+ #
28
+ # @return [Boolean]
29
+ def self.==(other) = other.is_a?(Class) && other <= EInvoiceAPI::Internal::Type::Boolean
30
+
31
+ class << self
32
+ # @api private
33
+ #
34
+ # @param value [Boolean, Object]
35
+ #
36
+ # @param state [Hash{Symbol=>Object}] .
37
+ #
38
+ # @option state [Boolean, :strong] :strictness
39
+ #
40
+ # @option state [Hash{Symbol=>Object}] :exactness
41
+ #
42
+ # @option state [Integer] :branched
43
+ #
44
+ # @return [Boolean, Object]
45
+ def coerce(value, state:)
46
+ state.fetch(:exactness)[value == true || value == false ? :yes : :no] += 1
47
+ value
48
+ end
49
+
50
+ # @!method dump(value, state:)
51
+ # @api private
52
+ #
53
+ # @param value [Boolean, Object]
54
+ #
55
+ # @param state [Hash{Symbol=>Object}] .
56
+ #
57
+ # @option state [Boolean] :can_retry
58
+ #
59
+ # @return [Boolean, Object]
60
+
61
+ # @api private
62
+ #
63
+ # @return [Object]
64
+ def to_sorbet_type
65
+ T::Boolean
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end