mindee 4.6.0 → 4.7.0.pre.rc1

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 (309) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +21 -222
  4. data/docs/code_samples/default_v2.txt +28 -0
  5. data/lib/mindee/client.rb +11 -30
  6. data/lib/mindee/client_v2.rb +109 -0
  7. data/lib/mindee/errors/mindee_http_error_v2.rb +26 -0
  8. data/lib/mindee/errors.rb +1 -0
  9. data/lib/mindee/geometry/point.rb +2 -2
  10. data/lib/mindee/geometry/quadrilateral.rb +4 -4
  11. data/lib/mindee/geometry/utils.rb +1 -0
  12. data/lib/mindee/http/api_settings_v2.rb +61 -0
  13. data/lib/mindee/http/endpoint.rb +12 -6
  14. data/lib/mindee/http/http_error_handler.rb +17 -4
  15. data/lib/mindee/http/mindee_api_v2.rb +148 -0
  16. data/lib/mindee/http/response_validation.rb +20 -1
  17. data/lib/mindee/http/workflow_endpoint.rb +2 -2
  18. data/lib/mindee/http.rb +2 -0
  19. data/lib/mindee/image/extracted_image.rb +1 -1
  20. data/lib/mindee/image/image_extractor.rb +10 -10
  21. data/lib/mindee/input/inference_parameters.rb +116 -0
  22. data/lib/mindee/input/local_response.rb +10 -1
  23. data/lib/mindee/input/polling_options.rb +26 -0
  24. data/lib/mindee/input/sources/base64_input_source.rb +2 -2
  25. data/lib/mindee/input/sources/local_input_source.rb +19 -12
  26. data/lib/mindee/input.rb +2 -0
  27. data/lib/mindee/page_options.rb +24 -0
  28. data/lib/mindee/parsing/common/api_request.rb +2 -2
  29. data/lib/mindee/parsing/common/api_response.rb +4 -0
  30. data/lib/mindee/parsing/common/execution.rb +1 -1
  31. data/lib/mindee/parsing/common/extras/extras.rb +2 -2
  32. data/lib/mindee/parsing/common/extras/rag_extra.rb +2 -0
  33. data/lib/mindee/parsing/common/ocr/ocr.rb +4 -6
  34. data/lib/mindee/parsing/common/orientation.rb +2 -2
  35. data/lib/mindee/parsing/common/product.rb +6 -1
  36. data/lib/mindee/parsing/standard/amount_field.rb +2 -2
  37. data/lib/mindee/parsing/standard/feature_field.rb +1 -1
  38. data/lib/mindee/parsing/standard/tax_field.rb +1 -1
  39. data/lib/mindee/parsing/universal/universal_list_field.rb +2 -2
  40. data/lib/mindee/parsing/universal/universal_object_field.rb +6 -3
  41. data/lib/mindee/parsing/v2/common_response.rb +18 -0
  42. data/lib/mindee/parsing/v2/error_response.rb +36 -0
  43. data/lib/mindee/parsing/v2/field/base_field.rb +60 -0
  44. data/lib/mindee/parsing/v2/field/field_confidence.rb +91 -0
  45. data/lib/mindee/parsing/v2/field/field_location.rb +36 -0
  46. data/lib/mindee/parsing/v2/field/inference_fields.rb +96 -0
  47. data/lib/mindee/parsing/v2/field/list_field.rb +88 -0
  48. data/lib/mindee/parsing/v2/field/object_field.rb +102 -0
  49. data/lib/mindee/parsing/v2/field/simple_field.rb +56 -0
  50. data/lib/mindee/parsing/v2/field.rb +9 -0
  51. data/lib/mindee/parsing/v2/inference.rb +50 -0
  52. data/lib/mindee/parsing/v2/inference_file.rb +38 -0
  53. data/lib/mindee/parsing/v2/inference_model.rb +18 -0
  54. data/lib/mindee/parsing/v2/inference_response.rb +30 -0
  55. data/lib/mindee/parsing/v2/inference_result.rb +49 -0
  56. data/lib/mindee/parsing/v2/inference_result_options.rb +21 -0
  57. data/lib/mindee/parsing/v2/job.rb +86 -0
  58. data/lib/mindee/parsing/v2/job_response.rb +30 -0
  59. data/lib/mindee/parsing/v2/job_webhook.rb +60 -0
  60. data/lib/mindee/parsing/v2/raw_text.rb +21 -0
  61. data/lib/mindee/parsing/v2.rb +15 -0
  62. data/lib/mindee/pdf/extracted_pdf.rb +5 -3
  63. data/lib/mindee/pdf/pdf_extractor.rb +11 -7
  64. data/lib/mindee/pdf/pdf_processor.rb +6 -6
  65. data/lib/mindee/pdf/pdf_tools.rb +4 -2
  66. data/lib/mindee/product/universal/universal_prediction.rb +4 -4
  67. data/lib/mindee/version.rb +2 -2
  68. data/lib/mindee.rb +6 -0
  69. data/mindee.gemspec +1 -1
  70. data/sig/custom/mini_magick.rbs +14 -7
  71. data/sig/custom/net_http.rbs +4 -4
  72. data/sig/custom/origami.rbs +9 -4
  73. data/sig/mindee/client.rbs +14 -19
  74. data/sig/mindee/client_v2.rbs +17 -0
  75. data/sig/mindee/errors/mindee_http_error.rbs +6 -5
  76. data/sig/mindee/errors/mindee_http_error_v2.rbs +12 -0
  77. data/sig/mindee/errors/mindee_input_error.rbs +3 -2
  78. data/sig/mindee/geometry/min_max.rbs +4 -3
  79. data/sig/mindee/geometry/point.rbs +5 -6
  80. data/sig/mindee/geometry/polygon.rbs +3 -3
  81. data/sig/mindee/geometry/quadrilateral.rbs +5 -8
  82. data/sig/mindee/geometry/utils.rbs +8 -8
  83. data/sig/mindee/http/api_settings_v2.rbs +23 -0
  84. data/sig/mindee/http/endpoint.rbs +12 -11
  85. data/sig/mindee/http/http_error_handler.rbs +8 -3
  86. data/sig/mindee/http/mindee_api_v2.rbs +18 -0
  87. data/sig/mindee/http/response_validation.rbs +1 -0
  88. data/sig/mindee/http/workflow_endpoint.rbs +1 -1
  89. data/sig/mindee/image/extracted_image.rbs +2 -2
  90. data/sig/mindee/image/image_compressor.rbs +1 -1
  91. data/sig/mindee/image/image_extractor.rbs +5 -5
  92. data/sig/mindee/image/image_utils.rbs +10 -10
  93. data/sig/mindee/input/inference_parameters.rbs +30 -0
  94. data/sig/mindee/input/local_response.rbs +6 -5
  95. data/sig/mindee/input/polling_options.rbs +12 -0
  96. data/sig/mindee/input/sources/base64_input_source.rbs +1 -1
  97. data/sig/mindee/input/sources/local_input_source.rbs +10 -7
  98. data/sig/mindee/input/sources/url_input_source.rbs +1 -1
  99. data/sig/mindee/logging/logger.rbs +2 -0
  100. data/sig/mindee/page_options.rbs +11 -0
  101. data/sig/mindee/parsing/common/api_request.rbs +6 -6
  102. data/sig/mindee/parsing/common/api_response.rbs +2 -2
  103. data/sig/mindee/parsing/common/document.rbs +10 -10
  104. data/sig/mindee/parsing/common/execution.rbs +15 -14
  105. data/sig/mindee/parsing/common/execution_file.rbs +3 -3
  106. data/sig/mindee/parsing/common/extras/cropper_extra.rbs +3 -2
  107. data/sig/mindee/parsing/common/extras/extras.rbs +6 -5
  108. data/sig/mindee/parsing/common/extras/full_text_ocr_extra.rbs +2 -1
  109. data/sig/mindee/parsing/common/inference.rbs +16 -9
  110. data/sig/mindee/parsing/common/job.rbs +2 -2
  111. data/sig/mindee/parsing/common/ocr/mvision_v1.rbs +2 -2
  112. data/sig/mindee/parsing/common/ocr/ocr.rbs +22 -20
  113. data/sig/mindee/parsing/common/orientation.rbs +4 -3
  114. data/sig/mindee/parsing/common/page.rbs +6 -5
  115. data/sig/mindee/parsing/common/product.rbs +4 -4
  116. data/sig/mindee/parsing/common/workflow_response.rbs +8 -4
  117. data/sig/mindee/parsing/standard/abstract_field.rbs +1 -1
  118. data/sig/mindee/parsing/standard/address_field.rbs +1 -1
  119. data/sig/mindee/parsing/standard/amount_field.rbs +6 -4
  120. data/sig/mindee/parsing/standard/base_field.rbs +1 -1
  121. data/sig/mindee/parsing/standard/boolean_field.rbs +1 -1
  122. data/sig/mindee/parsing/standard/company_registration_field.rbs +3 -2
  123. data/sig/mindee/parsing/standard/date_field.rbs +6 -6
  124. data/sig/mindee/parsing/standard/locale_field.rbs +1 -1
  125. data/sig/mindee/parsing/standard/payment_details_field.rbs +7 -6
  126. data/sig/mindee/parsing/standard/position_field.rbs +2 -2
  127. data/sig/mindee/parsing/standard/string_field.rbs +1 -1
  128. data/sig/mindee/parsing/standard/tax_field.rbs +8 -7
  129. data/sig/mindee/parsing/universal/universal_list_field.rbs +4 -5
  130. data/sig/mindee/parsing/universal/universal_object_field.rbs +17 -11
  131. data/sig/mindee/parsing/v2/common_response.rbs +11 -0
  132. data/sig/mindee/parsing/v2/error_response.rbs +16 -0
  133. data/sig/mindee/parsing/v2/field/base_field.rbs +17 -0
  134. data/sig/mindee/parsing/v2/field/field_confidence.rbs +27 -0
  135. data/sig/mindee/parsing/v2/field/field_location.rbs +16 -0
  136. data/sig/mindee/parsing/v2/field/inference_fields.rbs +19 -0
  137. data/sig/mindee/parsing/v2/field/list_field.rbs +22 -0
  138. data/sig/mindee/parsing/v2/field/object_field.rbs +21 -0
  139. data/sig/mindee/parsing/v2/field/simple_field.rbs +16 -0
  140. data/sig/mindee/parsing/v2/inference.rbs +16 -0
  141. data/sig/mindee/parsing/v2/inference_file.rbs +15 -0
  142. data/sig/mindee/parsing/v2/inference_model.rbs +11 -0
  143. data/sig/mindee/parsing/v2/inference_response.rbs +12 -0
  144. data/sig/mindee/parsing/v2/inference_result.rbs +13 -0
  145. data/sig/mindee/parsing/v2/inference_result_options.rbs +11 -0
  146. data/sig/mindee/parsing/v2/job.rbs +22 -0
  147. data/sig/mindee/parsing/v2/job_response.rbs +12 -0
  148. data/sig/mindee/parsing/v2/job_webhook.rbs +17 -0
  149. data/sig/mindee/parsing/v2/raw_text.rbs +12 -0
  150. data/sig/mindee/pdf/extracted_pdf.rbs +6 -6
  151. data/sig/mindee/pdf/pdf_compressor.rbs +4 -4
  152. data/sig/mindee/pdf/pdf_extractor.rbs +8 -7
  153. data/sig/mindee/pdf/pdf_processor.rbs +4 -4
  154. data/sig/mindee/pdf/pdf_tools.rbs +22 -16
  155. data/sig/mindee/product/barcode_reader/barcode_reader_v1.rbs +1 -1
  156. data/sig/mindee/product/barcode_reader/barcode_reader_v1_document.rbs +1 -1
  157. data/sig/mindee/product/barcode_reader/barcode_reader_v1_page.rbs +2 -2
  158. data/sig/mindee/product/bill_of_lading/bill_of_lading_v1.rbs +1 -1
  159. data/sig/mindee/product/bill_of_lading/bill_of_lading_v1_carrier.rbs +2 -2
  160. data/sig/mindee/product/bill_of_lading/bill_of_lading_v1_carrier_item.rbs +3 -3
  161. data/sig/mindee/product/bill_of_lading/bill_of_lading_v1_consignee.rbs +2 -2
  162. data/sig/mindee/product/bill_of_lading/bill_of_lading_v1_document.rbs +1 -1
  163. data/sig/mindee/product/bill_of_lading/bill_of_lading_v1_notify_party.rbs +2 -2
  164. data/sig/mindee/product/bill_of_lading/bill_of_lading_v1_page.rbs +2 -2
  165. data/sig/mindee/product/bill_of_lading/bill_of_lading_v1_shipper.rbs +2 -2
  166. data/sig/mindee/product/business_card/business_card_v1.rbs +1 -1
  167. data/sig/mindee/product/business_card/business_card_v1_document.rbs +1 -1
  168. data/sig/mindee/product/business_card/business_card_v1_page.rbs +2 -2
  169. data/sig/mindee/product/cropper/cropper_v1.rbs +1 -1
  170. data/sig/mindee/product/cropper/cropper_v1_document.rbs +1 -1
  171. data/sig/mindee/product/cropper/cropper_v1_page.rbs +2 -2
  172. data/sig/mindee/product/delivery_note/delivery_note_v1.rbs +1 -1
  173. data/sig/mindee/product/delivery_note/delivery_note_v1_document.rbs +1 -1
  174. data/sig/mindee/product/delivery_note/delivery_note_v1_page.rbs +2 -2
  175. data/sig/mindee/product/driver_license/driver_license_v1.rbs +1 -1
  176. data/sig/mindee/product/driver_license/driver_license_v1_document.rbs +1 -1
  177. data/sig/mindee/product/driver_license/driver_license_v1_page.rbs +2 -2
  178. data/sig/mindee/product/financial_document/financial_document_v1.rbs +1 -1
  179. data/sig/mindee/product/financial_document/financial_document_v1_document.rbs +1 -1
  180. data/sig/mindee/product/financial_document/financial_document_v1_line_item.rbs +3 -3
  181. data/sig/mindee/product/financial_document/financial_document_v1_page.rbs +2 -2
  182. data/sig/mindee/product/fr/bank_account_details/bank_account_details_v1.rbs +1 -1
  183. data/sig/mindee/product/fr/bank_account_details/bank_account_details_v1_document.rbs +1 -1
  184. data/sig/mindee/product/fr/bank_account_details/bank_account_details_v1_page.rbs +2 -2
  185. data/sig/mindee/product/fr/bank_account_details/bank_account_details_v2.rbs +1 -1
  186. data/sig/mindee/product/fr/bank_account_details/bank_account_details_v2_bban.rbs +2 -2
  187. data/sig/mindee/product/fr/bank_account_details/bank_account_details_v2_document.rbs +1 -1
  188. data/sig/mindee/product/fr/bank_account_details/bank_account_details_v2_page.rbs +2 -2
  189. data/sig/mindee/product/fr/bank_statement/bank_statement_v2.rbs +1 -1
  190. data/sig/mindee/product/fr/bank_statement/bank_statement_v2_document.rbs +1 -1
  191. data/sig/mindee/product/fr/bank_statement/bank_statement_v2_page.rbs +2 -2
  192. data/sig/mindee/product/fr/bank_statement/bank_statement_v2_transaction.rbs +3 -3
  193. data/sig/mindee/product/fr/carte_grise/carte_grise_v1.rbs +1 -1
  194. data/sig/mindee/product/fr/carte_grise/carte_grise_v1_document.rbs +1 -1
  195. data/sig/mindee/product/fr/carte_grise/carte_grise_v1_page.rbs +2 -2
  196. data/sig/mindee/product/fr/energy_bill/energy_bill_v1.rbs +1 -1
  197. data/sig/mindee/product/fr/energy_bill/energy_bill_v1_document.rbs +1 -1
  198. data/sig/mindee/product/fr/energy_bill/energy_bill_v1_energy_consumer.rbs +2 -2
  199. data/sig/mindee/product/fr/energy_bill/energy_bill_v1_energy_supplier.rbs +2 -2
  200. data/sig/mindee/product/fr/energy_bill/energy_bill_v1_energy_usage.rbs +3 -3
  201. data/sig/mindee/product/fr/energy_bill/energy_bill_v1_meter_detail.rbs +2 -2
  202. data/sig/mindee/product/fr/energy_bill/energy_bill_v1_page.rbs +2 -2
  203. data/sig/mindee/product/fr/energy_bill/energy_bill_v1_subscription.rbs +3 -3
  204. data/sig/mindee/product/fr/energy_bill/energy_bill_v1_taxes_and_contribution.rbs +3 -3
  205. data/sig/mindee/product/fr/health_card/health_card_v1.rbs +1 -1
  206. data/sig/mindee/product/fr/health_card/health_card_v1_document.rbs +1 -1
  207. data/sig/mindee/product/fr/health_card/health_card_v1_page.rbs +2 -2
  208. data/sig/mindee/product/fr/id_card/id_card_v1.rbs +1 -1
  209. data/sig/mindee/product/fr/id_card/id_card_v1_document.rbs +1 -1
  210. data/sig/mindee/product/fr/id_card/id_card_v1_page.rbs +2 -2
  211. data/sig/mindee/product/fr/id_card/id_card_v2.rbs +1 -1
  212. data/sig/mindee/product/fr/id_card/id_card_v2_document.rbs +1 -1
  213. data/sig/mindee/product/fr/id_card/id_card_v2_page.rbs +2 -2
  214. data/sig/mindee/product/fr/payslip/payslip_v2.rbs +1 -1
  215. data/sig/mindee/product/fr/payslip/payslip_v2_bank_account_detail.rbs +2 -2
  216. data/sig/mindee/product/fr/payslip/payslip_v2_document.rbs +1 -1
  217. data/sig/mindee/product/fr/payslip/payslip_v2_employee.rbs +2 -2
  218. data/sig/mindee/product/fr/payslip/payslip_v2_employer.rbs +2 -2
  219. data/sig/mindee/product/fr/payslip/payslip_v2_employment.rbs +2 -2
  220. data/sig/mindee/product/fr/payslip/payslip_v2_page.rbs +2 -2
  221. data/sig/mindee/product/fr/payslip/payslip_v2_pay_detail.rbs +2 -2
  222. data/sig/mindee/product/fr/payslip/payslip_v2_pay_period.rbs +2 -2
  223. data/sig/mindee/product/fr/payslip/payslip_v2_pto.rbs +2 -2
  224. data/sig/mindee/product/fr/payslip/payslip_v2_salary_detail.rbs +3 -3
  225. data/sig/mindee/product/fr/payslip/payslip_v3.rbs +1 -1
  226. data/sig/mindee/product/fr/payslip/payslip_v3_bank_account_detail.rbs +2 -2
  227. data/sig/mindee/product/fr/payslip/payslip_v3_document.rbs +1 -1
  228. data/sig/mindee/product/fr/payslip/payslip_v3_employee.rbs +2 -2
  229. data/sig/mindee/product/fr/payslip/payslip_v3_employer.rbs +2 -2
  230. data/sig/mindee/product/fr/payslip/payslip_v3_employment.rbs +2 -2
  231. data/sig/mindee/product/fr/payslip/payslip_v3_page.rbs +2 -2
  232. data/sig/mindee/product/fr/payslip/payslip_v3_paid_time_off.rbs +3 -3
  233. data/sig/mindee/product/fr/payslip/payslip_v3_pay_detail.rbs +2 -2
  234. data/sig/mindee/product/fr/payslip/payslip_v3_pay_period.rbs +2 -2
  235. data/sig/mindee/product/fr/payslip/payslip_v3_salary_detail.rbs +3 -3
  236. data/sig/mindee/product/ind/indian_passport/indian_passport_v1.rbs +1 -1
  237. data/sig/mindee/product/ind/indian_passport/indian_passport_v1_document.rbs +1 -1
  238. data/sig/mindee/product/ind/indian_passport/indian_passport_v1_page.rbs +2 -2
  239. data/sig/mindee/product/international_id/international_id_v2.rbs +1 -1
  240. data/sig/mindee/product/international_id/international_id_v2_document.rbs +1 -1
  241. data/sig/mindee/product/international_id/international_id_v2_page.rbs +2 -2
  242. data/sig/mindee/product/invoice/invoice_v4.rbs +1 -1
  243. data/sig/mindee/product/invoice/invoice_v4_document.rbs +1 -1
  244. data/sig/mindee/product/invoice/invoice_v4_line_item.rbs +3 -3
  245. data/sig/mindee/product/invoice/invoice_v4_page.rbs +2 -2
  246. data/sig/mindee/product/invoice_splitter/invoice_splitter_v1.rbs +1 -1
  247. data/sig/mindee/product/invoice_splitter/invoice_splitter_v1_document.rbs +1 -1
  248. data/sig/mindee/product/invoice_splitter/invoice_splitter_v1_invoice_page_group.rbs +3 -3
  249. data/sig/mindee/product/invoice_splitter/invoice_splitter_v1_page.rbs +2 -2
  250. data/sig/mindee/product/multi_receipts_detector/multi_receipts_detector_v1.rbs +1 -1
  251. data/sig/mindee/product/multi_receipts_detector/multi_receipts_detector_v1_document.rbs +1 -1
  252. data/sig/mindee/product/multi_receipts_detector/multi_receipts_detector_v1_page.rbs +2 -2
  253. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1.rbs +1 -1
  254. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_added_sugar.rbs +2 -2
  255. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_calorie.rbs +2 -2
  256. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_cholesterol.rbs +2 -2
  257. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_dietary_fiber.rbs +2 -2
  258. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_document.rbs +1 -1
  259. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_nutrient.rbs +3 -3
  260. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_page.rbs +2 -2
  261. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_protein.rbs +2 -2
  262. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_saturated_fat.rbs +2 -2
  263. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_serving_size.rbs +2 -2
  264. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_sodium.rbs +2 -2
  265. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_total_carbohydrate.rbs +2 -2
  266. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_total_fat.rbs +2 -2
  267. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_total_sugar.rbs +2 -2
  268. data/sig/mindee/product/nutrition_facts_label/nutrition_facts_label_v1_trans_fat.rbs +2 -2
  269. data/sig/mindee/product/passport/passport_v1.rbs +1 -1
  270. data/sig/mindee/product/passport/passport_v1_document.rbs +1 -1
  271. data/sig/mindee/product/passport/passport_v1_page.rbs +2 -2
  272. data/sig/mindee/product/receipt/receipt_v5.rbs +1 -1
  273. data/sig/mindee/product/receipt/receipt_v5_document.rbs +1 -1
  274. data/sig/mindee/product/receipt/receipt_v5_line_item.rbs +3 -3
  275. data/sig/mindee/product/receipt/receipt_v5_page.rbs +2 -2
  276. data/sig/mindee/product/resume/resume_v1.rbs +1 -1
  277. data/sig/mindee/product/resume/resume_v1_certificate.rbs +3 -3
  278. data/sig/mindee/product/resume/resume_v1_document.rbs +1 -1
  279. data/sig/mindee/product/resume/resume_v1_education.rbs +3 -3
  280. data/sig/mindee/product/resume/resume_v1_language.rbs +3 -3
  281. data/sig/mindee/product/resume/resume_v1_page.rbs +2 -2
  282. data/sig/mindee/product/resume/resume_v1_professional_experience.rbs +3 -3
  283. data/sig/mindee/product/resume/resume_v1_social_networks_url.rbs +3 -3
  284. data/sig/mindee/product/universal/universal.rbs +5 -5
  285. data/sig/mindee/product/universal/universal_document.rbs +1 -1
  286. data/sig/mindee/product/universal/universal_page.rbs +2 -2
  287. data/sig/mindee/product/universal/universal_prediction.rbs +5 -5
  288. data/sig/mindee/product/us/bank_check/bank_check_v1.rbs +1 -1
  289. data/sig/mindee/product/us/bank_check/bank_check_v1_document.rbs +1 -1
  290. data/sig/mindee/product/us/bank_check/bank_check_v1_page.rbs +2 -2
  291. data/sig/mindee/product/us/healthcare_card/healthcare_card_v1.rbs +1 -1
  292. data/sig/mindee/product/us/healthcare_card/healthcare_card_v1_copay.rbs +3 -3
  293. data/sig/mindee/product/us/healthcare_card/healthcare_card_v1_document.rbs +1 -1
  294. data/sig/mindee/product/us/healthcare_card/healthcare_card_v1_page.rbs +2 -2
  295. data/sig/mindee/product/us/us_mail/us_mail_v2.rbs +1 -1
  296. data/sig/mindee/product/us/us_mail/us_mail_v2_document.rbs +1 -1
  297. data/sig/mindee/product/us/us_mail/us_mail_v2_page.rbs +2 -2
  298. data/sig/mindee/product/us/us_mail/us_mail_v2_recipient_address.rbs +3 -3
  299. data/sig/mindee/product/us/us_mail/us_mail_v2_sender_address.rbs +2 -2
  300. data/sig/mindee/product/us/us_mail/us_mail_v3.rbs +1 -1
  301. data/sig/mindee/product/us/us_mail/us_mail_v3_document.rbs +1 -1
  302. data/sig/mindee/product/us/us_mail/us_mail_v3_page.rbs +2 -2
  303. data/sig/mindee/product/us/us_mail/us_mail_v3_recipient_address.rbs +3 -3
  304. data/sig/mindee/product/us/us_mail/us_mail_v3_sender_address.rbs +2 -2
  305. data/sig/mindee/product/us/w9/w9_v1.rbs +1 -1
  306. data/sig/mindee/product/us/w9/w9_v1_document.rbs +1 -1
  307. data/sig/mindee/product/us/w9/w9_v1_page.rbs +2 -2
  308. data/sig/mindee/version.rbs +2 -2
  309. metadata +61 -6
@@ -66,6 +66,9 @@ module Mindee
66
66
 
67
67
  ResponseValidation.clean_request!(response)
68
68
  end
69
+
70
+ raise Errors::MindeeError, 'Could not resolve server response.' if response.nil?
71
+
69
72
  error = ErrorHandler.handle_error(@url_name, response)
70
73
  raise error
71
74
  end
@@ -84,8 +87,9 @@ module Mindee
84
87
  ResponseValidation.clean_request!(response)
85
88
  end
86
89
 
87
- error = ErrorHandler.handle_error(@url_name, response)
88
- raise error
90
+ raise Errors::MindeeError, 'Could not resolve server response.' if response.nil?
91
+
92
+ raise ErrorHandler.handle_error(@url_name, response)
89
93
  end
90
94
 
91
95
  # Calls the parsed async doc.
@@ -110,7 +114,7 @@ module Mindee
110
114
  def predict_req_post(input_source, opts)
111
115
  uri = URI("#{@url_root}/predict")
112
116
 
113
- params = {} # : Hash[Symbol | String, untyped]
117
+ params = {} # : Hash[String | Symbol, untyped]
114
118
  params[:cropper] = 'true' if opts.cropper
115
119
  params[:full_text_ocr] = 'true' if opts.full_text
116
120
  uri.query = URI.encode_www_form(params)
@@ -123,7 +127,7 @@ module Mindee
123
127
  form_data = if input_source.is_a?(Mindee::Input::Source::URLInputSource)
124
128
  [['document', input_source.url]] # : Array[untyped]
125
129
  else
126
- [input_source.read_contents(close: opts.close_file)] # : Array[untyped]
130
+ [['document', *input_source.read_contents(close: opts.close_file)]] # : Array[untyped]
127
131
  end
128
132
  form_data.push ['include_mvision', 'true'] if opts.all_words
129
133
 
@@ -146,7 +150,7 @@ module Mindee
146
150
  URI("#{@url_root}/predict_async")
147
151
  end
148
152
 
149
- params = {} # : Hash[Symbol | String, untyped]
153
+ params = {} # : Hash[String | Symbol, untyped]
150
154
  params[:cropper] = 'true' if opts.cropper
151
155
  params[:full_text_ocr] = 'true' if opts.full_text
152
156
  params[:rag] = 'true' if opts.rag
@@ -160,7 +164,7 @@ module Mindee
160
164
  form_data = if input_source.is_a?(Mindee::Input::Source::URLInputSource)
161
165
  [['document', input_source.url]] # : Array[untyped]
162
166
  else
163
- [input_source.read_contents(close: opts.close_file)] # : Array[untyped]
167
+ [['document', *input_source.read_contents(close: opts.close_file)]] # : Array[untyped]
164
168
  end
165
169
  form_data.push ['include_mvision', 'true'] if opts.all_words
166
170
 
@@ -189,6 +193,8 @@ module Mindee
189
193
  http.request(req)
190
194
  end
191
195
 
196
+ raise Errors::MindeeError, 'Could not resolve server response.' if response.nil?
197
+
192
198
  if response.code.to_i > 299 && response.code.to_i < 400
193
199
  req = Net::HTTP::Get.new(response['location'], headers)
194
200
  Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @request_timeout) do |http|
@@ -65,7 +65,7 @@ module Mindee
65
65
  end
66
66
 
67
67
  end
68
- error_obj.nil? ? {} : error_obj
68
+ error_obj
69
69
  end
70
70
 
71
71
  # Creates an appropriate HTTP error exception, based on retrieved http error code
@@ -81,11 +81,24 @@ module Mindee
81
81
  error_obj = create_error_obj(parsed_hash)
82
82
  case code
83
83
  when 400..499
84
- Errors::MindeeHTTPClientError.new(error_obj, url, code)
84
+ Errors::MindeeHTTPClientError.new(error_obj || {}, url, code)
85
85
  when 500..599
86
- Errors::MindeeHTTPServerError.new(error_obj, url, code)
86
+ Errors::MindeeHTTPServerError.new(error_obj || {}, url, code)
87
87
  else
88
- Errors::MindeeHTTPError.new(error_obj, url, code)
88
+ Errors::MindeeHTTPError.new(error_obj || {}, url, code)
89
+ end
90
+ end
91
+
92
+ # Creates an appropriate HTTP error exception for a V2 API response, based on retrieved http error code.
93
+ # @param hashed_response [Hash] dictionary response retrieved by the server
94
+ def generate_v2_error(hashed_response)
95
+ code = hashed_response[:code].to_i
96
+ if hashed_response.key?(:status)
97
+ Errors::MindeeHTTPErrorV2.new(hashed_response.transform_keys(&:to_s))
98
+ elsif code < 200 || code > 399
99
+ Errors::MindeeHTTPErrorV2.new({ 'status' => code, 'detail' => 'No details available.' })
100
+ else
101
+ Errors::MindeeHTTPErrorV2.new({ 'status' => -1, 'detail' => 'Unknown Error.' })
89
102
  end
90
103
  end
91
104
  end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_settings_v2'
4
+ require_relative '../input'
5
+ require_relative '../errors'
6
+ require_relative '../parsing/v2'
7
+
8
+ module Mindee
9
+ module HTTP
10
+ # API client for version 2.
11
+ class MindeeApiV2
12
+ # @return [ApiSettingsV2]
13
+ attr_reader :settings
14
+
15
+ # @param api_key [String, nil]
16
+ def initialize(api_key: nil)
17
+ @settings = ApiSettingsV2.new(api_key: api_key)
18
+ end
19
+
20
+ # Sends a file to the inference queue.
21
+ #
22
+ # @param input_source [Input::Source::LocalInputSource, Input::Source::URLInputSource]
23
+ # @param params [Input::InferenceParameters]
24
+ # @return [Mindee::Parsing::V2::JobResponse]
25
+ # @raise [Mindee::Errors::MindeeHttpErrorV2]
26
+ def req_post_inference_enqueue(input_source, params)
27
+ @settings.check_api_key
28
+ response = enqueue(
29
+ input_source,
30
+ params
31
+ )
32
+ Parsing::V2::JobResponse.new(process_response(response))
33
+ end
34
+
35
+ # Retrieves a queued inference.
36
+ #
37
+ # @param inference_id [String]
38
+ # @return [Mindee::Parsing::V2::InferenceResponse]
39
+ def req_get_inference(inference_id)
40
+ @settings.check_api_key
41
+ response = inference_result_req_get(
42
+ inference_id
43
+ )
44
+ Parsing::V2::InferenceResponse.new(process_response(response))
45
+ end
46
+
47
+ # Retrieves a queued job.
48
+ #
49
+ # @param job_id [String]
50
+ # @return [Mindee::Parsing::V2::JobResponse]
51
+ def req_get_job(job_id)
52
+ @settings.check_api_key
53
+ response = inference_job_req_get(
54
+ job_id
55
+ )
56
+ Parsing::V2::JobResponse.new(process_response(response))
57
+ end
58
+
59
+ private
60
+
61
+ # Converts an HTTP response to a parsed response object.
62
+ #
63
+ # @param response [Net::HTTPResponse, nil]
64
+ # @return [Hash]
65
+ # @raise Throws if the server returned an error.
66
+ def process_response(response)
67
+ if !response.nil? && response.respond_to?(:body) && ResponseValidation.valid_v2_response?(response)
68
+ return JSON.parse(response.body, object_class: Hash)
69
+ end
70
+
71
+ response_body = if response.nil? || !response.respond_to?(:body)
72
+ '{ "status": -1,
73
+ "detail": "Empty server response." }'
74
+ else
75
+ response.body
76
+ end
77
+ raise ErrorHandler.generate_v2_error(JSON.parse(response_body).transform_keys(&:to_sym))
78
+ end
79
+
80
+ # Polls a queue for either a result or a job.
81
+ # @param url [String] URL, passed as a string.
82
+ # @return [Net::HTTPResponse]
83
+ def poll(url)
84
+ uri = URI(url)
85
+ headers = {
86
+ 'Authorization' => @settings.api_key,
87
+ 'User-Agent' => @settings.user_agent,
88
+ }
89
+ req = Net::HTTP::Get.new(uri, headers)
90
+ req['Transfer-Encoding'] = 'chunked'
91
+
92
+ Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @settings.request_timeout) do |http|
93
+ return http.request(req)
94
+ end
95
+ raise Mindee::Errors::MindeeError, 'Could not resolve server response.'
96
+ end
97
+
98
+ # Polls the API for the status of a job.
99
+ #
100
+ # @param job_id [String] ID of the job.
101
+ # @return [Net::HTTPResponse]
102
+ def inference_job_req_get(job_id)
103
+ poll("#{@settings.base_url}/jobs/#{job_id}")
104
+ end
105
+
106
+ # Polls the API for the result of an inference.
107
+ #
108
+ # @param queue_id [String] ID of the queue.
109
+ # @return [Net::HTTPResponse]
110
+ def inference_result_req_get(queue_id)
111
+ poll("#{@settings.base_url}/inferences/#{queue_id}")
112
+ end
113
+
114
+ # @param input_source [Mindee::Input::Source::LocalInputSource, Mindee::Input::Source::URLInputSource]
115
+ # @param params [Input::InferenceParameters] Parse options.
116
+ # @return [Net::HTTPResponse, nil]
117
+ def enqueue(input_source, params)
118
+ uri = URI("#{@settings.base_url}/inferences/enqueue")
119
+
120
+ form_data = if input_source.is_a?(Mindee::Input::Source::URLInputSource)
121
+ [['url', input_source.url]] # : Array[untyped]
122
+ else
123
+ file_data, file_metadata = input_source.read_contents(close: params.close_file)
124
+ [['file', file_data, file_metadata]] # : Array[untyped]
125
+ end
126
+ form_data.push ['model_id', params.model_id]
127
+ form_data.push ['rag', 'true'] if params.rag
128
+ form_data.push ['file_alias', params.file_alias] if params.file_alias
129
+ unless params.webhook_ids.nil? || params.webhook_ids.empty?
130
+ form_data.push ['webhook_ids', params.webhook_ids.join(',')]
131
+ end
132
+ headers = {
133
+ 'Authorization' => @settings.api_key,
134
+ 'User-Agent' => @settings.user_agent,
135
+ }
136
+ req = Net::HTTP::Post.new(uri, headers)
137
+
138
+ req.set_form(form_data, 'multipart/form-data')
139
+ req['Transfer-Encoding'] = 'chunked'
140
+
141
+ Net::HTTP.start(uri.hostname, uri.port, use_ssl: true, read_timeout: @settings.request_timeout) do |http|
142
+ return http.request(req)
143
+ end
144
+ raise Mindee::Errors::MindeeError, 'Could not resolve server response.'
145
+ end
146
+ end
147
+ end
148
+ end
@@ -21,6 +21,25 @@ module Mindee
21
21
  true
22
22
  end
23
23
 
24
+ # Checks if a V2 response is valid.
25
+ # @param [Net::HTTPResponse] response
26
+ # @return [bool]
27
+ def self.valid_v2_response?(response)
28
+ return false unless valid_sync_response?(response)
29
+
30
+ hashed_response = JSON.parse(response.body, object_class: Hash)
31
+
32
+ return false if hashed_response.dig('job', 'status').to_s == 'Failed'
33
+
34
+ return false if hashed_response.dig('job',
35
+ 'error') && !(hashed_response.dig('job',
36
+ 'error').empty? || hashed_response.dig(
37
+ 'job', 'error'
38
+ ).nil?)
39
+
40
+ true
41
+ end
42
+
24
43
  # Checks if the asynchronous response is valid. Also checks if it is a valid synchronous response.
25
44
  # Returns true if the response is valid.
26
45
  # @param [Net::HTTPResponse] response
@@ -52,7 +71,7 @@ module Mindee
52
71
  end
53
72
 
54
73
  return if !hashed_response.dig('job', 'error').empty? &&
55
- (hashed_response.dig('job', 'status') != Mindee::Parsing::Common::JobStatus::FAILURE.to_s)
74
+ (hashed_response.dig('job', 'status').downcase != Mindee::Parsing::Common::JobStatus::FAILURE.to_s)
56
75
 
57
76
  response.instance_variable_set(:@code, '500')
58
77
  end
@@ -48,7 +48,7 @@ module Mindee
48
48
  # @return [Net::HTTPResponse, nil]
49
49
  def workflow_execution_req_post(input_source, opts)
50
50
  uri = URI(@url)
51
- params = {} # : Hash[Symbol | String, untyped]
51
+ params = {} # : Hash[String | Symbol, untyped]
52
52
  params[:full_text_ocr] = 'true' if opts.full_text
53
53
  params[:rag] = 'true' if opts.rag
54
54
  uri.query = URI.encode_www_form(params) if params.any?
@@ -62,7 +62,7 @@ module Mindee
62
62
  if input_source.is_a?(Mindee::Input::Source::URLInputSource)
63
63
  form_data.push ['document', input_source.url]
64
64
  else
65
- form_data.push input_source.read_contents
65
+ form_data.push ['document', *input_source.read_contents(close: opts.close_file)]
66
66
  end
67
67
  form_data.push ['alias', opts.document_alias] if opts.document_alias
68
68
  form_data.push ['public_url', opts.public_url] if opts.public_url
data/lib/mindee/http.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'http/api_settings_v2'
3
4
  require_relative 'http/endpoint'
4
5
  require_relative 'http/http_error_handler'
6
+ require_relative 'http/mindee_api_v2'
5
7
  require_relative 'http/workflow_endpoint'
@@ -69,7 +69,7 @@ module Mindee
69
69
  # @return [FileInputSource] A BufferInput source.
70
70
  def as_source
71
71
  @buffer.rewind
72
- Mindee::Input::Source::BytesInputSource.new(@buffer.read, @internal_file_name)
72
+ Mindee::Input::Source::BytesInputSource.new(@buffer.read || '', @internal_file_name)
73
73
  end
74
74
  end
75
75
  end
@@ -30,11 +30,11 @@ module Mindee
30
30
 
31
31
  # Extracts multiple images from a given local input source.
32
32
  #
33
- # @param [Mindee::Input::Source::LocalInputSource] input_source
33
+ # @param [Input::Source::LocalInputSource] input_source
34
34
  # @param [Integer] page_id ID of the Page to extract from.
35
- # @param [Array<Array<Mindee::Geometry::Point>>, Array<Mindee::Geometry::Quadrangle>] polygons List of coordinates
35
+ # @param [Array<Array<Geometry::Point>>, Array<Geometry::Quadrilateral>] polygons List of coordinates.
36
36
  # to extract.
37
- # @return [Array<Mindee::Image::ExtractedImage>] Extracted Images.
37
+ # @return [Array<Image::ExtractedImage>] Extracted Images.
38
38
  def self.extract_multiple_images_from_source(input_source, page_id, polygons)
39
39
  new_stream = load_input_source_pdf_page_as_stringio(input_source, page_id)
40
40
  new_stream.seek(0)
@@ -44,11 +44,11 @@ module Mindee
44
44
 
45
45
  # Extracts images from their positions on a file (as polygons).
46
46
  #
47
- # @param [Mindee::Input::Source::LocalInputSource] input_source Local input source.
47
+ # @param [Input::Source::LocalInputSource] input_source Local input source.
48
48
  # @param [StringIO] pdf_stream Buffer of the PDF.
49
49
  # @param [Integer] page_id Page ID.
50
- # @param [Array<Mindee::Geometry::Point, Mindee::Geometry::Polygon, Mindee::Geometry::Quadrangle>] polygons
51
- # @return [Array<Mindee::Image::ExtractedImage>] Extracted Images.
50
+ # @param [Array<Geometry::Point, Geometry::Polygon, Geometry::Quadrilateral>] polygons
51
+ # @return [Array<Image::ExtractedImage>] Extracted Images.
52
52
  def self.extract_images_from_polygons(input_source, pdf_stream, page_id, polygons)
53
53
  extracted_elements = []
54
54
 
@@ -90,12 +90,12 @@ module Mindee
90
90
  #
91
91
  # @param [StringIO] buffer Buffer containing the image.
92
92
  # @param [String] file_name Name for the file.
93
- # @param [Object] page_id ID of the page the file was universal from.
94
- # @param [Object] element_id ID of the element of a given page.
93
+ # @param [Integer] page_id ID of the page the file was universal from.
94
+ # @param [Integer] element_id ID of the element of a given page.
95
95
  def self.create_extracted_image(buffer, file_name, page_id, element_id)
96
96
  buffer.rewind
97
97
  ExtractedImage.new(
98
- Mindee::Input::Source::BytesInputSource.new(buffer.read.to_s, file_name),
98
+ Input::Source::BytesInputSource.new(buffer.read.to_s, file_name),
99
99
  page_id,
100
100
  element_id
101
101
  )
@@ -109,7 +109,7 @@ module Mindee
109
109
  def self.load_input_source_pdf_page_as_stringio(input_file, page_id)
110
110
  input_file.io_stream.rewind
111
111
  if input_file.pdf?
112
- Mindee::PDF::PDFProcessor.get_page(Origami::PDF.read(input_file.io_stream), page_id)
112
+ PDF::PDFProcessor.get_page(Origami::PDF.read(input_file.io_stream), page_id)
113
113
  else
114
114
  input_file.io_stream
115
115
  end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mindee
4
+ module Input
5
+ # Parameters to set when sending a file for inference.
6
+ class InferenceParameters
7
+ # @return [String] ID of the model (required).
8
+ attr_reader :model_id
9
+
10
+ # @return [Boolean, nil] Enable Retrieval-Augmented Generation.
11
+ attr_reader :rag
12
+
13
+ # @return [String, nil] Optional alias for the file.
14
+ attr_reader :file_alias
15
+
16
+ # @return [Array<String>, nil] Optional list of Webhooks IDs to propagate the API response to.
17
+ attr_reader :webhook_ids
18
+
19
+ # @return [PollingOptions] Options for polling. Set only if having timeout issues.
20
+ attr_reader :polling_options
21
+
22
+ # @return [Boolean, nil] Whether to close the file after parsing.
23
+ attr_reader :close_file
24
+
25
+ # @param [String] model_id ID of the model
26
+ # @param [FalseClass] rag Whether to enable rag.
27
+ # @param [nil] file_alias File alias, if applicable.
28
+ # @param [nil] webhook_ids
29
+ # @param [nil] polling_options
30
+ # @param [TrueClass] close_file
31
+ def initialize(model_id, rag: false, file_alias: nil, webhook_ids: nil, polling_options: nil, close_file: true)
32
+ raise Errors::MindeeInputError, 'Model ID is required.' if model_id.empty? || model_id.nil?
33
+
34
+ @model_id = model_id
35
+ @rag = rag || false
36
+ @file_alias = file_alias
37
+ @webhook_ids = webhook_ids || []
38
+ @polling_options = get_clean_polling_options(polling_options)
39
+ @close_file = close_file.nil? || close_file
40
+ end
41
+
42
+ # Validates the parameters for async auto-polling
43
+ def validate_async_params
44
+ min_delay_sec = 1
45
+ min_initial_delay_sec = 1
46
+ min_retries = 2
47
+
48
+ if @polling_options.delay_sec < min_delay_sec
49
+ raise ArgumentError,
50
+ "Cannot set auto-poll delay to less than #{min_delay_sec} second(s)"
51
+ end
52
+ if @polling_options.initial_delay_sec < min_initial_delay_sec
53
+ raise ArgumentError,
54
+ "Cannot set initial parsing delay to less than #{min_initial_delay_sec} second(s)"
55
+ end
56
+ return unless @polling_options.max_retries < min_retries
57
+
58
+ raise ArgumentError,
59
+ "Cannot set auto-poll retries to less than #{min_retries}"
60
+ end
61
+
62
+ # Loads a prediction from a Hash.
63
+ # @param [Hash] params Parameters to provide as a hash.
64
+ # @return [InferenceParameters]
65
+ def self.from_hash(params: {})
66
+ params.transform_keys!(&:to_sym)
67
+
68
+ if params.empty? || params[:model_id].nil? || params[:model_id].empty?
69
+ raise Errors::MindeeInputError, 'Model ID is required.'
70
+ end
71
+
72
+ model_id = params.fetch(:model_id)
73
+ rag = params.fetch(:rag, false)
74
+ file_alias = params.fetch(:file_alias, nil)
75
+ webhook_ids = params.fetch(:webhook_ids, [])
76
+ polling_options_input = params.fetch(:page_options, PollingOptions.new)
77
+ if polling_options_input.is_a?(Hash)
78
+ polling_options_input = polling_options_input.transform_keys(&:to_sym)
79
+ PollingOptions.new(
80
+ initial_delay_sec: polling_options_input.fetch(:initial_delay_sec, 2.0),
81
+ delay_sec: polling_options_input.fetch(:delay_sec, 1.5),
82
+ max_retries: polling_options_input.fetch(:max_retries, 80)
83
+ )
84
+ end
85
+ close_file = params.fetch(:close_file, true)
86
+ InferenceParameters.new(model_id, rag: rag, file_alias: file_alias, webhook_ids: webhook_ids,
87
+ close_file: close_file)
88
+ end
89
+
90
+ private
91
+
92
+ # Cleans a proper polling options object potentially from a hash.
93
+ # @param [Hash, PollingOptions, nil] polling_options Polling options.
94
+ # @return [PollingOptions] Valid polling options object.
95
+ def get_clean_polling_options(polling_options)
96
+ return PollingOptions.new if polling_options.is_a?(PollingOptions)
97
+
98
+ if polling_options.is_a?(Hash)
99
+ polling_options = polling_options.transform_keys(&:to_sym)
100
+ output_polling_options = PollingOptions.new(
101
+ initial_delay_sec: polling_options.fetch(:initial_delay_sec, 2.0),
102
+ delay_sec: polling_options.fetch(:delay_sec, 1.5),
103
+ max_retries: polling_options.fetch(:max_retries, 80)
104
+ )
105
+ else
106
+ output_polling_options = if polling_options.is_a?(PollingOptions)
107
+ polling_options || PollingOptions.new
108
+ else
109
+ PollingOptions.new
110
+ end
111
+ end
112
+ output_polling_options
113
+ end
114
+ end
115
+ end
116
+ end
@@ -62,11 +62,20 @@ module Mindee
62
62
  end
63
63
 
64
64
  # @param secret_key [String] Secret key, either a string or a byte/byte array.
65
- # @param signature [String]
65
+ # @param signature [String] Signature to match
66
66
  # @return [bool]
67
67
  def valid_hmac_signature?(secret_key, signature)
68
68
  signature == get_hmac_signature(secret_key)
69
69
  end
70
+
71
+ # Deserializes a loaded response
72
+ # @param response_class [Parsing::V2::JobResponse, Parsing::V2::InferenceResponse] class to return.
73
+ # @return [Parsing::V2::JobResponse, Parsing::V2::InferenceResponse]
74
+ def deserialize_response(response_class)
75
+ response_class.new(as_hash)
76
+ rescue StandardError
77
+ raise Errors::MindeeInputError, 'Invalid response provided.'
78
+ end
70
79
  end
71
80
  end
72
81
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mindee
4
+ module Input
5
+ # Options for asynchronous polling.
6
+ class PollingOptions
7
+ # @return [Integer, Float] Initial delay before the first polling attempt (in seconds).
8
+ attr_reader :initial_delay_sec
9
+
10
+ # @return [Integer, Float] Delay between each polling attempt (in seconds).
11
+ attr_reader :delay_sec
12
+
13
+ # @return [Integer] Total number of polling attempts.
14
+ attr_reader :max_retries
15
+
16
+ # @param initial_delay_sec [Float] Initial delay before the first attempt (default:2.0).
17
+ # @param delay_sec [Float] Delay between attempts (default: 1.5).
18
+ # @param max_retries [Integer] Maximum number of retries (default:80).
19
+ def initialize(initial_delay_sec: 2.0, delay_sec: 1.5, max_retries: 80)
20
+ @initial_delay_sec = initial_delay_sec
21
+ @delay_sec = delay_sec
22
+ @max_retries = max_retries
23
+ end
24
+ end
25
+ end
26
+ end
@@ -18,12 +18,12 @@ module Mindee
18
18
 
19
19
  # Overload of the same function to prevent a base64 from being re-encoded.
20
20
  # @param close [bool]
21
- # @return [Array<String, [String, aBinaryString ], [Hash, nil] >]
21
+ # @return [Array<[String, aBinaryString ], [Hash, nil] >]
22
22
  def read_contents(close: true)
23
23
  @io_stream.seek(0)
24
24
  data = @io_stream.read
25
25
  @io_stream.close if close
26
- ['document', [data].pack('m'), { filename: Source.convert_to_unicode_escape(@filename) }]
26
+ [[data].pack('m'), { filename: Source.convert_to_unicode_escape(@filename) }]
27
27
  end
28
28
  end
29
29
  end
@@ -27,7 +27,7 @@ module Mindee
27
27
  attr_reader :filename
28
28
  # @return [String]
29
29
  attr_reader :file_mimetype
30
- # @return [StringIO]
30
+ # @return [StringIO | File]
31
31
  attr_reader :io_stream
32
32
 
33
33
  # @param io_stream [StringIO, File]
@@ -60,7 +60,7 @@ module Mindee
60
60
  # Attempts to fix pdf files if mimetype is rejected.
61
61
  # "Broken PDFs" are often a result of third-party injecting invalid headers.
62
62
  # This attempts to remove them and send the file
63
- # @param stream [StringIO]
63
+ # @param stream [StringIO, File]
64
64
  def rescue_broken_pdf(stream)
65
65
  stream.gets('%PDF-')
66
66
  raise Errors::MindeePDFError if stream.eof? || stream.pos > 500
@@ -78,7 +78,7 @@ module Mindee
78
78
  @file_mimetype.to_s == 'application/pdf'
79
79
  end
80
80
 
81
- # Parses a PDF file according to provided options.
81
+ # Cuts a PDF file according to provided options.
82
82
  # @param options [PageOptions, nil] Page cutting/merge options:
83
83
  #
84
84
  # * `:page_indexes` Zero-based list of page indexes.
@@ -86,14 +86,20 @@ module Mindee
86
86
  # * `:KEEP_ONLY` - keep only the specified pages, and remove all others.
87
87
  # * `:REMOVE` - remove the specified pages, and keep all others.
88
88
  # * `:on_min_pages` Apply the operation only if document has at least this many pages.
89
- def process_pdf(options)
89
+ def apply_page_options(options)
90
90
  @io_stream.seek(0)
91
91
  @io_stream = PDF::PDFProcessor.parse(@io_stream, options)
92
92
  end
93
93
 
94
+ # @deprecated Use {#apply_page_options} instead.
95
+ # @see #apply_page_options
96
+ def process_pdf(options)
97
+ apply_page_options(options)
98
+ end
99
+
94
100
  # Reads a document.
95
101
  # @param close [bool]
96
- # @return [Array<String, [String, aBinaryString ], [Hash, nil] >]
102
+ # @return [Array<>]
97
103
  def read_contents(close: true)
98
104
  logger.debug("Reading data from: #{@filename}")
99
105
  @io_stream.seek(0)
@@ -101,20 +107,21 @@ module Mindee
101
107
  data = @io_stream.read
102
108
  @io_stream.rewind
103
109
  @io_stream.close if close
104
- ['document', data, { filename: Mindee::Input::Source.convert_to_unicode_escape(@filename) }]
110
+ [data, { filename: Mindee::Input::Source.convert_to_unicode_escape(@filename) }]
105
111
  end
106
112
 
107
113
  # Write the file to a given path. Uses the initial file name by default.
108
114
  # @param path [String] Path to write the file to.
109
115
  def write_to_file(path)
110
- full_path = if File.directory?(path) || path.end_with?('/')
111
- File.join(path, @filename)
112
- else
113
- path
114
- end
116
+ t_path = if File.directory?(path || '') || path.to_s.end_with?('/')
117
+ File.join(path || '', @filename)
118
+ else
119
+ path
120
+ end
121
+ full_path = File.expand_path(t_path || '')
115
122
  FileUtils.mkdir_p(File.dirname(full_path))
116
123
  @io_stream.rewind
117
- File.binwrite(full_path, @io_stream.read)
124
+ File.binwrite(full_path, @io_stream.read || '')
118
125
  logger.debug("Wrote file successfully to #{full_path}")
119
126
  @io_stream.rewind
120
127
  end
data/lib/mindee/input.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'input/inference_parameters'
4
+ require_relative 'input/polling_options'
3
5
  require_relative 'input/sources'
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mindee
4
+ # Class for page options in parse calls.
5
+ # @!attribute page_indexes [Array[Integer]] Zero-based list of page indexes.
6
+ # @!attribute operation [:KEEP_ONLY, :REMOVE] Operation to apply on the document, given the specified page indexes:
7
+ # * `:KEEP_ONLY` - keep only the specified pages, and remove all others.
8
+ # * `:REMOVE` - remove the specified pages, and keep all others.
9
+ # @!attribute on_min_pages [Integer, nil] Apply the operation only if the document has at least this many pages.
10
+ class PageOptions
11
+ attr_accessor :page_indexes, :operation, :on_min_pages
12
+
13
+ def initialize(params: {})
14
+ params ||= {}
15
+ params = params.transform_keys(&:to_sym)
16
+ @page_indexes = params.fetch(
17
+ :page_indexes,
18
+ [] # : Array[Integer]
19
+ )
20
+ @operation = params.fetch(:operation, :KEEP_ONLY)
21
+ @on_min_pages = params.fetch(:on_min_pages, nil)
22
+ end
23
+ end
24
+ end