qube_sync 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (268) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +84 -0
  3. data/README.md +291 -75
  4. data/Rakefile +12 -0
  5. data/exe/qube_sync +11 -0
  6. data/lib/qube_sync/client.rb +614 -0
  7. data/lib/qube_sync/operations/account.rb +338 -0
  8. data/lib/qube_sync/operations/account_tax_line_info.rb +30 -0
  9. data/lib/qube_sync/operations/aging_report.rb +160 -0
  10. data/lib/qube_sync/operations/alert.rb +91 -0
  11. data/lib/qube_sync/operations/ar_refund_credit_card.rb +402 -0
  12. data/lib/qube_sync/operations/bar_code.rb +37 -0
  13. data/lib/qube_sync/operations/bill.rb +1117 -0
  14. data/lib/qube_sync/operations/bill_payment_check.rb +420 -0
  15. data/lib/qube_sync/operations/bill_payment_credit_card.rb +320 -0
  16. data/lib/qube_sync/operations/bill_to_pay.rb +67 -0
  17. data/lib/qube_sync/operations/billing_rate.rb +173 -0
  18. data/lib/qube_sync/operations/budget_summary_report.rb +80 -0
  19. data/lib/qube_sync/operations/build_assembly.rb +319 -0
  20. data/lib/qube_sync/operations/charge.rb +454 -0
  21. data/lib/qube_sync/operations/check.rb +1099 -0
  22. data/lib/qube_sync/operations/class.rb +176 -0
  23. data/lib/qube_sync/operations/cleared_status.rb +39 -0
  24. data/lib/qube_sync/operations/company.rb +39 -0
  25. data/lib/qube_sync/operations/company_activity.rb +31 -0
  26. data/lib/qube_sync/operations/credit_card_charge.rb +1044 -0
  27. data/lib/qube_sync/operations/credit_card_credit.rb +1044 -0
  28. data/lib/qube_sync/operations/credit_memo.rb +1277 -0
  29. data/lib/qube_sync/operations/currency.rb +195 -0
  30. data/lib/qube_sync/operations/custom_detail_report.rb +176 -0
  31. data/lib/qube_sync/operations/custom_summary_report.rb +180 -0
  32. data/lib/qube_sync/operations/customer.rb +745 -0
  33. data/lib/qube_sync/operations/customer_msg.rb +117 -0
  34. data/lib/qube_sync/operations/customer_type.rb +133 -0
  35. data/lib/qube_sync/operations/data_event_recovery_info.rb +54 -0
  36. data/lib/qube_sync/operations/data_ext.rb +202 -0
  37. data/lib/qube_sync/operations/data_ext_def.rb +179 -0
  38. data/lib/qube_sync/operations/date_driven_terms.rb +131 -0
  39. data/lib/qube_sync/operations/deposit.rb +570 -0
  40. data/lib/qube_sync/operations/employee.rb +581 -0
  41. data/lib/qube_sync/operations/employee_default.rb +100 -0
  42. data/lib/qube_sync/operations/entity.rb +118 -0
  43. data/lib/qube_sync/operations/estimate.rb +1084 -0
  44. data/lib/qube_sync/operations/fields.rb +4058 -0
  45. data/lib/qube_sync/operations/form1099_category_account_mapping.rb +91 -0
  46. data/lib/qube_sync/operations/general_detail_report.rb +185 -0
  47. data/lib/qube_sync/operations/general_summary_report.rb +180 -0
  48. data/lib/qube_sync/operations/host.rb +36 -0
  49. data/lib/qube_sync/operations/inventory_adjustment.rb +491 -0
  50. data/lib/qube_sync/operations/inventory_site.rb +214 -0
  51. data/lib/qube_sync/operations/invoice.rb +1242 -0
  52. data/lib/qube_sync/operations/item.rb +115 -0
  53. data/lib/qube_sync/operations/item_assemblies_can_build.rb +44 -0
  54. data/lib/qube_sync/operations/item_discount.rb +320 -0
  55. data/lib/qube_sync/operations/item_fixed_asset.rb +318 -0
  56. data/lib/qube_sync/operations/item_group.rb +255 -0
  57. data/lib/qube_sync/operations/item_inventory.rb +447 -0
  58. data/lib/qube_sync/operations/item_inventory_assembly.rb +486 -0
  59. data/lib/qube_sync/operations/item_non_inventory.rb +472 -0
  60. data/lib/qube_sync/operations/item_other_charge.rb +445 -0
  61. data/lib/qube_sync/operations/item_payment.rb +269 -0
  62. data/lib/qube_sync/operations/item_receipt.rb +1055 -0
  63. data/lib/qube_sync/operations/item_sales_tax.rb +271 -0
  64. data/lib/qube_sync/operations/item_sales_tax_group.rb +200 -0
  65. data/lib/qube_sync/operations/item_service.rb +466 -0
  66. data/lib/qube_sync/operations/item_sites.rb +98 -0
  67. data/lib/qube_sync/operations/item_subtotal.rb +182 -0
  68. data/lib/qube_sync/operations/job_report.rb +156 -0
  69. data/lib/qube_sync/operations/job_type.rb +131 -0
  70. data/lib/qube_sync/operations/journal_entry.rb +533 -0
  71. data/lib/qube_sync/operations/lead.rb +216 -0
  72. data/lib/qube_sync/operations/list.rb +42 -0
  73. data/lib/qube_sync/operations/list_deleted.rb +57 -0
  74. data/lib/qube_sync/operations/list_display.rb +59 -0
  75. data/lib/qube_sync/operations/list_merge.rb +57 -0
  76. data/lib/qube_sync/operations/other_name.rb +245 -0
  77. data/lib/qube_sync/operations/paycheck.rb +139 -0
  78. data/lib/qube_sync/operations/payment_method.rb +129 -0
  79. data/lib/qube_sync/operations/payroll_detail_report.rb +161 -0
  80. data/lib/qube_sync/operations/payroll_item.rb +75 -0
  81. data/lib/qube_sync/operations/payroll_item_addition.rb +98 -0
  82. data/lib/qube_sync/operations/payroll_item_company_contribution.rb +134 -0
  83. data/lib/qube_sync/operations/payroll_item_deduction.rb +124 -0
  84. data/lib/qube_sync/operations/payroll_item_direct_deposit.rb +70 -0
  85. data/lib/qube_sync/operations/payroll_item_non_wage.rb +93 -0
  86. data/lib/qube_sync/operations/payroll_item_tax.rb +123 -0
  87. data/lib/qube_sync/operations/payroll_item_wage.rb +168 -0
  88. data/lib/qube_sync/operations/payroll_item_workers_compensation.rb +108 -0
  89. data/lib/qube_sync/operations/payroll_last_period.rb +30 -0
  90. data/lib/qube_sync/operations/payroll_liability_adjustment.rb +204 -0
  91. data/lib/qube_sync/operations/payroll_liability_check.rb +137 -0
  92. data/lib/qube_sync/operations/payroll_prior_payment.rb +178 -0
  93. data/lib/qube_sync/operations/payroll_summary_report.rb +157 -0
  94. data/lib/qube_sync/operations/payroll_year_to_date_adjustment.rb +211 -0
  95. data/lib/qube_sync/operations/preferences.rb +61 -0
  96. data/lib/qube_sync/operations/price_level.rb +309 -0
  97. data/lib/qube_sync/operations/purchase_order.rb +1065 -0
  98. data/lib/qube_sync/operations/qb_command_exec.rb +37 -0
  99. data/lib/qube_sync/operations/receive_payment.rb +634 -0
  100. data/lib/qube_sync/operations/receive_payment_to_deposit.rb +38 -0
  101. data/lib/qube_sync/operations/sales_order.rb +1072 -0
  102. data/lib/qube_sync/operations/sales_receipt.rb +1371 -0
  103. data/lib/qube_sync/operations/sales_rep.rb +172 -0
  104. data/lib/qube_sync/operations/sales_tax_code.rb +205 -0
  105. data/lib/qube_sync/operations/sales_tax_payable.rb +37 -0
  106. data/lib/qube_sync/operations/sales_tax_payment_check.rb +379 -0
  107. data/lib/qube_sync/operations/sales_tax_return.rb +35 -0
  108. data/lib/qube_sync/operations/sales_tax_return_line.rb +43 -0
  109. data/lib/qube_sync/operations/ship_method.rb +116 -0
  110. data/lib/qube_sync/operations/special_account.rb +55 -0
  111. data/lib/qube_sync/operations/special_item.rb +44 -0
  112. data/lib/qube_sync/operations/standard_terms.rb +125 -0
  113. data/lib/qube_sync/operations/template.rb +36 -0
  114. data/lib/qube_sync/operations/terms.rb +91 -0
  115. data/lib/qube_sync/operations/time_report.rb +127 -0
  116. data/lib/qube_sync/operations/time_tracking.rb +304 -0
  117. data/lib/qube_sync/operations/to_do.rb +260 -0
  118. data/lib/qube_sync/operations/transaction.rb +361 -0
  119. data/lib/qube_sync/operations/transfer.rb +213 -0
  120. data/lib/qube_sync/operations/transfer_inventory.rb +393 -0
  121. data/lib/qube_sync/operations/txn.rb +38 -0
  122. data/lib/qube_sync/operations/txn_deleted.rb +54 -0
  123. data/lib/qube_sync/operations/txn_display.rb +76 -0
  124. data/lib/qube_sync/operations/txn_void.rb +36 -0
  125. data/lib/qube_sync/operations/unit_of_measure_set.rb +149 -0
  126. data/lib/qube_sync/operations/vehicle.rb +152 -0
  127. data/lib/qube_sync/operations/vehicle_mileage.rb +191 -0
  128. data/lib/qube_sync/operations/vendor.rb +663 -0
  129. data/lib/qube_sync/operations/vendor_credit.rb +1052 -0
  130. data/lib/qube_sync/operations/vendor_type.rb +131 -0
  131. data/lib/qube_sync/operations/workers_comp_code.rb +184 -0
  132. data/lib/qube_sync/operations.rb +1169 -0
  133. data/lib/qube_sync/version.rb +1 -1
  134. data/lib/qube_sync/workflow_types/cli.rb +120 -0
  135. data/lib/qube_sync/workflow_types/rbs_renderer.rb +290 -0
  136. data/lib/qube_sync/workflow_types/ruby_renderer.rb +391 -0
  137. data/lib/qube_sync/workflow_types/workflow.rb +153 -0
  138. data/lib/qube_sync/workflow_types.rb +219 -0
  139. data/lib/qube_sync.rb +1 -0
  140. data/sig/qube_sync/client.rbs +85 -0
  141. data/sig/qube_sync/operations/account.rbs +65 -0
  142. data/sig/qube_sync/operations/account_tax_line_info.rbs +13 -0
  143. data/sig/qube_sync/operations/aging_report.rbs +29 -0
  144. data/sig/qube_sync/operations/alert.rbs +53 -0
  145. data/sig/qube_sync/operations/ar_refund_credit_card.rbs +53 -0
  146. data/sig/qube_sync/operations/bar_code.rbs +16 -0
  147. data/sig/qube_sync/operations/bill.rbs +78 -0
  148. data/sig/qube_sync/operations/bill_payment_check.rbs +62 -0
  149. data/sig/qube_sync/operations/bill_payment_credit_card.rbs +45 -0
  150. data/sig/qube_sync/operations/bill_to_pay.rbs +19 -0
  151. data/sig/qube_sync/operations/billing_rate.rbs +40 -0
  152. data/sig/qube_sync/operations/budget_summary_report.rbs +22 -0
  153. data/sig/qube_sync/operations/build_assembly.rbs +64 -0
  154. data/sig/qube_sync/operations/charge.rbs +80 -0
  155. data/sig/qube_sync/operations/check.rbs +81 -0
  156. data/sig/qube_sync/operations/class.rbs +43 -0
  157. data/sig/qube_sync/operations/cleared_status.rbs +16 -0
  158. data/sig/qube_sync/operations/company.rbs +15 -0
  159. data/sig/qube_sync/operations/company_activity.rbs +14 -0
  160. data/sig/qube_sync/operations/credit_card_charge.rbs +69 -0
  161. data/sig/qube_sync/operations/credit_card_credit.rbs +69 -0
  162. data/sig/qube_sync/operations/credit_memo.rbs +162 -0
  163. data/sig/qube_sync/operations/currency.rbs +52 -0
  164. data/sig/qube_sync/operations/custom_detail_report.rbs +31 -0
  165. data/sig/qube_sync/operations/custom_summary_report.rbs +33 -0
  166. data/sig/qube_sync/operations/customer.rbs +174 -0
  167. data/sig/qube_sync/operations/customer_msg.rbs +31 -0
  168. data/sig/qube_sync/operations/customer_type.rbs +32 -0
  169. data/sig/qube_sync/operations/data_event_recovery_info.rbs +20 -0
  170. data/sig/qube_sync/operations/data_ext.rbs +53 -0
  171. data/sig/qube_sync/operations/data_ext_def.rbs +51 -0
  172. data/sig/qube_sync/operations/date_driven_terms.rbs +35 -0
  173. data/sig/qube_sync/operations/deposit.rbs +100 -0
  174. data/sig/qube_sync/operations/employee.rbs +173 -0
  175. data/sig/qube_sync/operations/employee_default.rbs +68 -0
  176. data/sig/qube_sync/operations/entity.rbs +24 -0
  177. data/sig/qube_sync/operations/estimate.rbs +148 -0
  178. data/sig/qube_sync/operations/form1099_category_account_mapping.rbs +33 -0
  179. data/sig/qube_sync/operations/general_detail_report.rbs +31 -0
  180. data/sig/qube_sync/operations/general_summary_report.rbs +32 -0
  181. data/sig/qube_sync/operations/host.rbs +19 -0
  182. data/sig/qube_sync/operations/inventory_adjustment.rbs +105 -0
  183. data/sig/qube_sync/operations/inventory_site.rbs +66 -0
  184. data/sig/qube_sync/operations/invoice.rbs +144 -0
  185. data/sig/qube_sync/operations/item.rbs +25 -0
  186. data/sig/qube_sync/operations/item_assemblies_can_build.rbs +15 -0
  187. data/sig/qube_sync/operations/item_discount.rbs +62 -0
  188. data/sig/qube_sync/operations/item_fixed_asset.rbs +97 -0
  189. data/sig/qube_sync/operations/item_group.rbs +62 -0
  190. data/sig/qube_sync/operations/item_inventory.rbs +87 -0
  191. data/sig/qube_sync/operations/item_inventory_assembly.rbs +94 -0
  192. data/sig/qube_sync/operations/item_non_inventory.rbs +64 -0
  193. data/sig/qube_sync/operations/item_other_charge.rbs +59 -0
  194. data/sig/qube_sync/operations/item_payment.rbs +55 -0
  195. data/sig/qube_sync/operations/item_receipt.rbs +71 -0
  196. data/sig/qube_sync/operations/item_sales_tax.rbs +57 -0
  197. data/sig/qube_sync/operations/item_sales_tax_group.rbs +50 -0
  198. data/sig/qube_sync/operations/item_service.rbs +62 -0
  199. data/sig/qube_sync/operations/item_sites.rbs +26 -0
  200. data/sig/qube_sync/operations/item_subtotal.rbs +48 -0
  201. data/sig/qube_sync/operations/job_report.rbs +28 -0
  202. data/sig/qube_sync/operations/job_type.rbs +32 -0
  203. data/sig/qube_sync/operations/journal_entry.rbs +88 -0
  204. data/sig/qube_sync/operations/lead.rbs +91 -0
  205. data/sig/qube_sync/operations/list.rbs +15 -0
  206. data/sig/qube_sync/operations/list_deleted.rbs +17 -0
  207. data/sig/qube_sync/operations/list_display.rbs +21 -0
  208. data/sig/qube_sync/operations/list_merge.rbs +22 -0
  209. data/sig/qube_sync/operations/other_name.rbs +71 -0
  210. data/sig/qube_sync/operations/paycheck.rbs +27 -0
  211. data/sig/qube_sync/operations/payment_method.rbs +33 -0
  212. data/sig/qube_sync/operations/payroll_detail_report.rbs +29 -0
  213. data/sig/qube_sync/operations/payroll_item.rbs +23 -0
  214. data/sig/qube_sync/operations/payroll_item_addition.rbs +48 -0
  215. data/sig/qube_sync/operations/payroll_item_company_contribution.rbs +58 -0
  216. data/sig/qube_sync/operations/payroll_item_deduction.rbs +54 -0
  217. data/sig/qube_sync/operations/payroll_item_direct_deposit.rbs +30 -0
  218. data/sig/qube_sync/operations/payroll_item_non_wage.rbs +23 -0
  219. data/sig/qube_sync/operations/payroll_item_tax.rbs +58 -0
  220. data/sig/qube_sync/operations/payroll_item_wage.rbs +48 -0
  221. data/sig/qube_sync/operations/payroll_item_workers_compensation.rbs +42 -0
  222. data/sig/qube_sync/operations/payroll_last_period.rbs +13 -0
  223. data/sig/qube_sync/operations/payroll_liability_adjustment.rbs +59 -0
  224. data/sig/qube_sync/operations/payroll_liability_check.rbs +27 -0
  225. data/sig/qube_sync/operations/payroll_prior_payment.rbs +48 -0
  226. data/sig/qube_sync/operations/payroll_summary_report.rbs +30 -0
  227. data/sig/qube_sync/operations/payroll_year_to_date_adjustment.rbs +66 -0
  228. data/sig/qube_sync/operations/preferences.rbs +26 -0
  229. data/sig/qube_sync/operations/price_level.rbs +57 -0
  230. data/sig/qube_sync/operations/purchase_order.rbs +159 -0
  231. data/sig/qube_sync/operations/qb_command_exec.rbs +22 -0
  232. data/sig/qube_sync/operations/receive_payment.rbs +102 -0
  233. data/sig/qube_sync/operations/receive_payment_to_deposit.rbs +15 -0
  234. data/sig/qube_sync/operations/sales_order.rbs +153 -0
  235. data/sig/qube_sync/operations/sales_receipt.rbs +139 -0
  236. data/sig/qube_sync/operations/sales_rep.rbs +43 -0
  237. data/sig/qube_sync/operations/sales_tax_code.rbs +49 -0
  238. data/sig/qube_sync/operations/sales_tax_payable.rbs +16 -0
  239. data/sig/qube_sync/operations/sales_tax_payment_check.rbs +64 -0
  240. data/sig/qube_sync/operations/sales_tax_return.rbs +14 -0
  241. data/sig/qube_sync/operations/sales_tax_return_line.rbs +15 -0
  242. data/sig/qube_sync/operations/ship_method.rbs +31 -0
  243. data/sig/qube_sync/operations/special_account.rbs +16 -0
  244. data/sig/qube_sync/operations/special_item.rbs +17 -0
  245. data/sig/qube_sync/operations/standard_terms.rbs +34 -0
  246. data/sig/qube_sync/operations/template.rbs +15 -0
  247. data/sig/qube_sync/operations/terms.rbs +23 -0
  248. data/sig/qube_sync/operations/time_report.rbs +26 -0
  249. data/sig/qube_sync/operations/time_tracking.rbs +57 -0
  250. data/sig/qube_sync/operations/to_do.rbs +59 -0
  251. data/sig/qube_sync/operations/transaction.rbs +39 -0
  252. data/sig/qube_sync/operations/transfer.rbs +47 -0
  253. data/sig/qube_sync/operations/transfer_inventory.rbs +76 -0
  254. data/sig/qube_sync/operations/txn.rbs +15 -0
  255. data/sig/qube_sync/operations/txn_deleted.rbs +17 -0
  256. data/sig/qube_sync/operations/txn_display.rbs +22 -0
  257. data/sig/qube_sync/operations/txn_void.rbs +15 -0
  258. data/sig/qube_sync/operations/types.rbs +434 -0
  259. data/sig/qube_sync/operations/unit_of_measure_set.rbs +51 -0
  260. data/sig/qube_sync/operations/vehicle.rbs +43 -0
  261. data/sig/qube_sync/operations/vehicle_mileage.rbs +38 -0
  262. data/sig/qube_sync/operations/vendor.rbs +142 -0
  263. data/sig/qube_sync/operations/vendor_credit.rbs +70 -0
  264. data/sig/qube_sync/operations/vendor_type.rbs +32 -0
  265. data/sig/qube_sync/operations/workers_comp_code.rbs +52 -0
  266. data/sig/qube_sync/operations.rbs +148 -0
  267. data/sig/qube_sync/workflow_types.rbs +59 -0
  268. metadata +265 -3
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module QubeSync
4
- VERSION = "0.3.0"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "optparse"
5
+
6
+ module QubeSync
7
+ module WorkflowTypes
8
+ # `qube_sync workflows [--drafts] [--check] [--output lib/qube_workflows.rb] [--sig sig/qube_workflows.rbs]`
9
+ #
10
+ # Fetches the app's workflow types with QUBE_API_KEY (from QUBE_BASE_URL, default
11
+ # https://qubesync.com) and writes the typed Ruby file and its RBS. With --check it writes
12
+ # nothing: it exits 1 when the file is out of date (or QuBe Sync refuses the key), and 0 when
13
+ # it's current or QuBe Sync can't be reached, with a warning, so CI isn't held up by an
14
+ # outage. Returns the exit status.
15
+ class CLI
16
+ USAGE = "Usage: qube_sync workflows [--drafts] [--check] [--output PATH] [--sig PATH]"
17
+
18
+ # env, out and err are for tests; client_factory takes (api_key:, base_url:).
19
+ def initialize(env: ENV, out: $stdout, err: $stderr, client_factory: nil)
20
+ @env = env
21
+ @out = out
22
+ @err = err
23
+ @client_factory = client_factory || ->(api_key:, base_url:) { Client.new(api_key: api_key, base_url: base_url) }
24
+ end
25
+
26
+ def run(argv)
27
+ command, *rest = argv
28
+ unless command == "workflows"
29
+ @err.puts USAGE
30
+ return %w[-h --help help].include?(command) ? 0 : 1
31
+ end
32
+
33
+ options = {drafts: false, check: false, output: "lib/qube_workflows.rb", sig: "sig/qube_workflows.rbs"}
34
+ parser = OptionParser.new do |opts|
35
+ opts.banner = USAGE
36
+ opts.on("--drafts", "Type each workflow's draft where it has one") { options[:drafts] = true }
37
+ opts.on("--check", "Write nothing; exit 1 when the files are out of date") { options[:check] = true }
38
+ opts.on("--output PATH", "The Ruby file (lib/qube_workflows.rb)") { |path| options[:output] = path }
39
+ opts.on("--sig PATH", "The RBS file (sig/qube_workflows.rbs)") { |path| options[:sig] = path }
40
+ end
41
+ begin
42
+ parser.parse!(rest)
43
+ rescue OptionParser::ParseError => e
44
+ @err.puts "qube_sync: #{e.message}"
45
+ @err.puts parser.help
46
+ return 1
47
+ end
48
+ unless rest.empty?
49
+ @err.puts parser.help
50
+ return 1
51
+ end
52
+
53
+ api_key = @env["QUBE_API_KEY"].to_s
54
+ if api_key.strip.empty?
55
+ @err.puts "qube_sync: set QUBE_API_KEY to your app's API key"
56
+ return 1
57
+ end
58
+
59
+ base_url = @env["QUBE_BASE_URL"].to_s.strip
60
+ base_url = Client::DEFAULT_BASE_URL if base_url.empty?
61
+ # The site, or its API: QUBE_BASE_URL may name either.
62
+ base_url = base_url.sub(%r{/+\z}, "").delete_suffix(Client::API_PATH)
63
+ client = @client_factory.call(api_key: api_key, base_url: base_url)
64
+ source = "#{client.base_url}#{Client::API_PATH}/workflows/types"
65
+
66
+ options[:check] ? check(client, source, options) : generate(client, source, options)
67
+ rescue Error, ArgumentError => e
68
+ @err.puts "qube_sync: #{e.message}"
69
+ 1
70
+ end
71
+
72
+ private
73
+
74
+ def generate(client, source, options)
75
+ document = client.workflow_types(include_drafts: options[:drafts])
76
+ files = WorkflowTypes.render(document, source: source, drafts: options[:drafts])
77
+ files.warnings.each { |warning| @err.puts "warning: #{warning}" }
78
+ write(options[:output], files.ruby)
79
+ write(options[:sig], files.rbs)
80
+ count = WorkflowTypes.select(document, options[:drafts]).size
81
+ @out.puts "qube_sync: #{count} workflow#{"s" unless count == 1} from #{source}#{" (with drafts)" if options[:drafts]}"
82
+ 0
83
+ rescue APIError => e
84
+ @err.puts "qube_sync: #{e.message}"
85
+ 1
86
+ rescue *Client::NETWORK_ERRORS, SystemCallError => e
87
+ @err.puts "qube_sync: couldn't reach QuBe Sync (#{e.class}: #{e.message}); nothing was written"
88
+ 1
89
+ end
90
+
91
+ def check(client, source, options)
92
+ path = options[:output]
93
+ unless File.exist?(path)
94
+ @err.puts "qube_sync: #{path} isn't there: run `#{COMMAND}` to write it"
95
+ return 1
96
+ end
97
+
98
+ result = WorkflowTypes.check(File.read(path, encoding: "UTF-8"), client: client, source: source, path: path)
99
+ result.warnings.each { |warning| @err.puts "warning: #{warning}" }
100
+ case result.status
101
+ when :current then @out.puts "qube_sync: #{result.message}"
102
+ when :unreachable then @err.puts "warning: #{result.message}"
103
+ else @err.puts "qube_sync: #{result.message}"
104
+ end
105
+ result.exit_status
106
+ end
107
+
108
+ def write(path, content)
109
+ dir = File.dirname(path)
110
+ FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
111
+ if File.exist?(path) && File.read(path, encoding: "UTF-8") == content
112
+ @out.puts "identical #{path}"
113
+ else
114
+ File.write(path, content)
115
+ @out.puts "wrote #{path}"
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,290 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module QubeSync
6
+ module WorkflowTypes
7
+ # Writes the RBS half of a render: a type alias per input (`invoice_input`, or `input` for a
8
+ # workflow without branches), each operation body as that operation's params record
9
+ # (::QubeSync::Operations::Invoice::create_params), a nested discriminated object (an `adds`
10
+ # entry) as a union of a record per branch, and the start and input methods' signatures. JSON
11
+ # Schema maps to RBS as bin/generate-operations maps it.
12
+ class RBSRenderer
13
+ # Words RBS doesn't take as a type alias's name: one gets a trailing underscore.
14
+ RBS_KEYWORDS = %w[
15
+ alias as attr_accessor attr_reader attr_writer bool bot class def end extend false in include instance
16
+ interface module nil out prepend private public self singleton super top true type unchecked untyped
17
+ use void
18
+ ].freeze
19
+ # A string RBS can take as a literal type as it is.
20
+ PLAIN_STRING = %r{\A[\w .:/@+\-]*\z}
21
+ DATA = "::QubeSync::Client::data"
22
+
23
+ def initialize(workflows, header:, client:)
24
+ @workflows = workflows
25
+ @header = header
26
+ @setter = client == DEFAULT_CLIENT
27
+ end
28
+
29
+ def render
30
+ lines = @header + ["", "module QubeWorkflows"]
31
+ lines << " # Anything with a QuBe Sync connection id, such as a QubeRails::Connection."
32
+ lines << " interface _Connection"
33
+ lines << " def qube_id: () -> String?"
34
+ lines << " end"
35
+ lines << ""
36
+ lines << " type connection = String | _Connection"
37
+ lines << ""
38
+ if @setter
39
+ lines << " def self.client: () -> ::QubeSync::Client"
40
+ lines << " def self.client=: (::QubeSync::Client client) -> ::QubeSync::Client"
41
+ end
42
+ lines << " def self.connection_id: (connection connection) -> String?"
43
+ @workflows.each do |workflow|
44
+ lines << " def self.#{workflow.accessor}: () -> singleton(#{workflow.const})"
45
+ end
46
+ @workflows.each do |workflow|
47
+ lines << ""
48
+ lines.concat(workflow_rbs(workflow))
49
+ end
50
+ lines.concat(["end", ""])
51
+ lines.join("\n")
52
+ end
53
+
54
+ private
55
+
56
+ def workflow_rbs(workflow)
57
+ @aliases = {}
58
+ @memo = {}
59
+ @prefix = nil
60
+ @taken = ["input"] + workflow.branches.map { |branch| "#{branch.method}_input" }
61
+ inputs = {}
62
+ methods = []
63
+ if workflow.discriminated?
64
+ workflow.branches.each do |branch|
65
+ # A body's own record is named for its branch.
66
+ @prefix = branch.method
67
+ fields = [[workflow.discriminator, literal(branch.value), true]] +
68
+ branch.inputs.map { |input| [input.name, type(input.schema, input.name, " "), input.required] }
69
+ inputs["#{branch.method}_input"] = record(fields, " ")
70
+ params = RubyRenderer.params_for(branch.inputs, branch.body)
71
+ methods << " def self.#{branch.method}: (#{(connection + signature(params) + run).join(", ")}) -> #{DATA}"
72
+ methods << " def self.#{branch.method}_input: (#{signature(params).join(", ")}) -> #{branch.method}_input"
73
+ end
74
+ @prefix = nil
75
+ inputs["input"] = workflow.branches.map { |branch| "#{branch.method}_input" }.join(" | ")
76
+ methods << " def self.start: (#{(connection + ["input input"] + run).join(", ")}) -> #{DATA}"
77
+ else
78
+ fields = workflow.inputs.map { |input| [input.name, type(input.schema, input.name, " "), input.required] }
79
+ inputs["input"] = record(fields, " ")
80
+ params = RubyRenderer.params_for(workflow.inputs, nil)
81
+ methods << " def self.start: (#{(connection + signature(params) + run).join(", ")}) -> #{DATA}"
82
+ methods << " def self.input: (#{signature(params).join(", ")}) -> input"
83
+ end
84
+
85
+ lines = [" module #{workflow.const}", " WORKFLOW: String", " VERSION: #{workflow.version.nil? ? "nil" : "Integer"}", ""]
86
+ @aliases.sort.each do |name, rbs|
87
+ lines << " type #{name} = #{rbs}"
88
+ lines << ""
89
+ end
90
+ inputs.each do |name, rbs|
91
+ lines << " type #{name} = #{rbs}"
92
+ lines << ""
93
+ end
94
+ lines.concat(methods)
95
+ lines << " end"
96
+ end
97
+
98
+ def connection
99
+ ["::QubeWorkflows::connection connection"]
100
+ end
101
+
102
+ def run
103
+ ["reference: String", "webhook_url: String", "?version: Integer?", "?client: ::QubeSync::Client"]
104
+ end
105
+
106
+ # A method's inputs, named as the Ruby names them: required ones as `name: T`, the others as
107
+ # `?name: T?` (a nil is left out of the input).
108
+ def signature(params)
109
+ params.map do |param|
110
+ rbs = type(param.input.schema, param.input.name, nil)
111
+ if param.positional
112
+ RBS_KEYWORDS.include?(param.name) ? rbs : "#{rbs} #{param.name}"
113
+ elsif param.input.required
114
+ "#{param.name}: #{rbs}"
115
+ else
116
+ "?#{param.name}: #{optional(rbs)}"
117
+ end
118
+ end
119
+ end
120
+
121
+ # -- Types ------------------------------------------------------------------------------
122
+
123
+ def type(schema, path, indent)
124
+ union(members(schema, path, indent))
125
+ end
126
+
127
+ def members(schema, path, indent)
128
+ id = Workflow.operation(schema)
129
+ return [body_type(id, schema, path)] if id
130
+ return [named(schema, path)] if Workflow.discriminated?(schema)
131
+ return [literal(schema["const"])] if schema.key?("const")
132
+ return schema["enum"].map { |value| literal(value) } if schema["enum"].is_a?(Array)
133
+
134
+ types = Array(schema["type"])
135
+ if types.empty?
136
+ types << "object" if schema.key?("properties")
137
+ types << "array" if schema.key?("items")
138
+ end
139
+ return ["untyped"] if types.empty?
140
+
141
+ types.map do |kind|
142
+ case kind
143
+ when "string" then "String"
144
+ when "integer" then "Integer"
145
+ when "number" then "Numeric"
146
+ when "boolean" then "bool"
147
+ when "null" then "nil"
148
+ when "array" then "Array[#{type(schema["items"].is_a?(Hash) ? schema["items"] : {}, "#{path}_item", indent)}]"
149
+ when "object" then object_type(schema, path, indent)
150
+ else "untyped"
151
+ end
152
+ end
153
+ end
154
+
155
+ def union(members)
156
+ members = members.uniq
157
+ return "untyped" if members.include?("untyped")
158
+
159
+ others = members - ["nil"]
160
+ return members.join(" | ") if others.size == members.size
161
+ return "nil" if others.empty?
162
+
163
+ optional(others.join(" | "))
164
+ end
165
+
166
+ def optional(rbs)
167
+ return rbs if rbs == "untyped" || rbs == "nil" || rbs.end_with?("?")
168
+
169
+ top_level_union?(rbs) ? "(#{rbs})?" : "#{rbs}?"
170
+ end
171
+
172
+ # A `|` outside any brackets: `"a" | "b"`, not `Hash[String, "a" | "b"]`.
173
+ def top_level_union?(rbs)
174
+ depth = 0
175
+ rbs.each_char.each_cons(3) do |before, char, after|
176
+ depth += 1 if "[{(".include?(before)
177
+ depth -= 1 if "]})".include?(before)
178
+ return true if depth.zero? && char == "|" && before == " " && after == " "
179
+ end
180
+ false
181
+ end
182
+
183
+ def object_type(schema, path, indent)
184
+ properties = schema["properties"].is_a?(Hash) ? schema["properties"] : {}
185
+ extra = schema["additionalProperties"]
186
+ if properties.empty?
187
+ return "Hash[String, #{type(extra, "#{path}_value", indent)}]" if extra.is_a?(Hash)
188
+
189
+ return "Hash[untyped, untyped]"
190
+ end
191
+ # A field name a record can't hold types the object as a plain Hash.
192
+ return "Hash[untyped, untyped]" unless properties.keys.all? { |name| name.match?(Workflow::NAME) }
193
+
194
+ required = Array(schema["required"])
195
+ fields = properties.sort.map do |name, sub|
196
+ [name, type(sub.is_a?(Hash) ? sub : {}, "#{path}_#{name}", indent && "#{indent} "), required.include?(name)]
197
+ end
198
+ record(fields, indent)
199
+ end
200
+
201
+ # A record over lines at indent, or on one line when indent is nil (in a signature).
202
+ def record(fields, indent)
203
+ return "Hash[Symbol, untyped]" if fields.empty?
204
+ return "{ #{fields.map { |name, rbs, required| "#{required ? "" : "?"}#{name}: #{rbs}" }.join(", ")} }" if indent.nil?
205
+
206
+ lines = fields.map { |name, rbs, required| "#{indent} #{required ? "" : "?"}#{name}: #{rbs}" }
207
+ "{\n#{lines.join(",\n")}\n#{indent}}"
208
+ end
209
+
210
+ # A nested discriminated object's alias: a union of a record per branch, each named
211
+ # <alias>_<title>. The same schema at the same place shares one.
212
+ def named(schema, path)
213
+ key = [path, JSON.generate(schema)]
214
+ return @memo[key] if @memo.key?(key)
215
+
216
+ name = claim(path)
217
+ @memo[key] = name
218
+ # Shared by every branch of the workflow, so not named for one.
219
+ prefix = @prefix
220
+ @prefix = nil
221
+ discriminator = schema.dig("discriminator", "propertyName")
222
+ branches = Workflow.branch_schemas(schema).map do |title, properties, required|
223
+ branch = claim("#{name}_#{title}")
224
+ fields = properties.sort_by { |field, _| [field == discriminator ? 0 : 1, field] }.map do |field, sub|
225
+ sub = sub.is_a?(Hash) ? sub : {}
226
+ [field, type(sub, "#{name}_#{field}", " "), field == discriminator || required.include?(field)]
227
+ end
228
+ if fields.any? { |field, _, _| !field.match?(Workflow::NAME) }
229
+ @aliases[branch] = "Hash[untyped, untyped]"
230
+ else
231
+ @aliases[branch] = record(fields, " ")
232
+ end
233
+ branch
234
+ end
235
+ @aliases[name] = branches.empty? ? "Hash[untyped, untyped]" : branches.join(" | ")
236
+ @prefix = prefix
237
+ name
238
+ end
239
+
240
+ def claim(path)
241
+ base = RBS_KEYWORDS.include?(path) ? "#{path}_" : path
242
+ name = base
243
+ n = 2
244
+ while @taken.include?(name)
245
+ name = "#{base}_#{n}"
246
+ n += 1
247
+ end
248
+ @taken << name
249
+ name
250
+ end
251
+
252
+ # An operation body: the operation's params alias, or, when the workflow sets some of its
253
+ # required fields itself (set_by_workflow), a record of its own with those made optional,
254
+ # named <branch>_<input>_body (<input>_body without branches). A field or an operation this
255
+ # gem doesn't know is ignored.
256
+ def body_type(id, schema, path)
257
+ fields = Workflow.operation_fields(id)
258
+ return operation_type(id) unless fields
259
+
260
+ relaxed = Workflow.set_by_workflow(schema).select { |field| fields.key?(field) && fields[field][1] }
261
+ return operation_type(id) if relaxed.empty?
262
+
263
+ base = [@prefix, path].compact.join("_")
264
+ key = [:body, base, id, relaxed]
265
+ return @memo[key] if @memo.key?(key)
266
+
267
+ name = claim("#{base}_body")
268
+ @memo[key] = name
269
+ @aliases[name] = record(fields.sort.map { |field, (rbs, required)| [field, rbs, required && !relaxed.include?(field)] }, " ")
270
+ name
271
+ end
272
+
273
+ def operation_type(id)
274
+ known = Workflow.known_operation(id)
275
+ known ? "::QubeSync::Operations::#{known[0]}::#{known[1]}_params" : "Hash[untyped, untyped]"
276
+ end
277
+
278
+ def literal(value)
279
+ case value
280
+ when String then value.match?(PLAIN_STRING) ? JSON.generate(value) : "String"
281
+ when Integer then value.to_s
282
+ when Float then "Float"
283
+ when true, false then value.to_s
284
+ when nil then "nil"
285
+ else "untyped"
286
+ end
287
+ end
288
+ end
289
+ end
290
+ end