qube_sync 0.4.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 (97) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +38 -2
  3. data/README.md +58 -2
  4. data/exe/qube_sync +11 -0
  5. data/lib/qube_sync/client.rb +40 -5
  6. data/lib/qube_sync/operations/account.rb +21 -20
  7. data/lib/qube_sync/operations/ar_refund_credit_card.rb +17 -14
  8. data/lib/qube_sync/operations/bill.rb +148 -104
  9. data/lib/qube_sync/operations/bill_payment_check.rb +29 -22
  10. data/lib/qube_sync/operations/bill_payment_credit_card.rb +21 -16
  11. data/lib/qube_sync/operations/bill_to_pay.rb +7 -6
  12. data/lib/qube_sync/operations/billing_rate.rb +10 -9
  13. data/lib/qube_sync/operations/build_assembly.rb +13 -12
  14. data/lib/qube_sync/operations/charge.rb +36 -34
  15. data/lib/qube_sync/operations/check.rb +144 -100
  16. data/lib/qube_sync/operations/class.rb +11 -11
  17. data/lib/qube_sync/operations/credit_card_charge.rb +144 -100
  18. data/lib/qube_sync/operations/credit_card_credit.rb +144 -100
  19. data/lib/qube_sync/operations/credit_memo.rb +142 -108
  20. data/lib/qube_sync/operations/currency.rb +7 -7
  21. data/lib/qube_sync/operations/customer.rb +54 -53
  22. data/lib/qube_sync/operations/customer_msg.rb +5 -5
  23. data/lib/qube_sync/operations/customer_type.rb +7 -7
  24. data/lib/qube_sync/operations/data_ext.rb +7 -6
  25. data/lib/qube_sync/operations/date_driven_terms.rb +5 -5
  26. data/lib/qube_sync/operations/deposit.rb +53 -38
  27. data/lib/qube_sync/operations/employee.rb +27 -23
  28. data/lib/qube_sync/operations/entity.rb +5 -5
  29. data/lib/qube_sync/operations/estimate.rb +119 -90
  30. data/lib/qube_sync/operations/fields.rb +4058 -0
  31. data/lib/qube_sync/operations/form1099_category_account_mapping.rb +3 -2
  32. data/lib/qube_sync/operations/inventory_adjustment.rb +43 -34
  33. data/lib/qube_sync/operations/inventory_site.rb +11 -11
  34. data/lib/qube_sync/operations/invoice.rb +142 -108
  35. data/lib/qube_sync/operations/item.rb +5 -5
  36. data/lib/qube_sync/operations/item_assemblies_can_build.rb +2 -2
  37. data/lib/qube_sync/operations/item_discount.rb +25 -24
  38. data/lib/qube_sync/operations/item_fixed_asset.rb +17 -16
  39. data/lib/qube_sync/operations/item_group.rb +17 -15
  40. data/lib/qube_sync/operations/item_inventory.rb +45 -44
  41. data/lib/qube_sync/operations/item_inventory_assembly.rb +51 -48
  42. data/lib/qube_sync/operations/item_non_inventory.rb +55 -44
  43. data/lib/qube_sync/operations/item_other_charge.rb +51 -40
  44. data/lib/qube_sync/operations/item_payment.rb +21 -20
  45. data/lib/qube_sync/operations/item_receipt.rb +144 -100
  46. data/lib/qube_sync/operations/item_sales_tax.rb +21 -20
  47. data/lib/qube_sync/operations/item_sales_tax_group.rb +11 -11
  48. data/lib/qube_sync/operations/item_service.rb +55 -44
  49. data/lib/qube_sync/operations/item_sites.rb +10 -8
  50. data/lib/qube_sync/operations/item_subtotal.rb +7 -7
  51. data/lib/qube_sync/operations/job_type.rb +7 -7
  52. data/lib/qube_sync/operations/journal_entry.rb +49 -34
  53. data/lib/qube_sync/operations/lead.rb +7 -7
  54. data/lib/qube_sync/operations/list.rb +2 -2
  55. data/lib/qube_sync/operations/list_display.rb +2 -2
  56. data/lib/qube_sync/operations/list_merge.rb +6 -4
  57. data/lib/qube_sync/operations/other_name.rb +7 -7
  58. data/lib/qube_sync/operations/payment_method.rb +6 -5
  59. data/lib/qube_sync/operations/payroll_item_non_wage.rb +5 -5
  60. data/lib/qube_sync/operations/payroll_item_wage.rb +7 -7
  61. data/lib/qube_sync/operations/price_level.rb +23 -19
  62. data/lib/qube_sync/operations/purchase_order.rb +113 -86
  63. data/lib/qube_sync/operations/receive_payment.rb +37 -30
  64. data/lib/qube_sync/operations/sales_order.rb +117 -90
  65. data/lib/qube_sync/operations/sales_receipt.rb +142 -108
  66. data/lib/qube_sync/operations/sales_rep.rb +11 -11
  67. data/lib/qube_sync/operations/sales_tax_code.rb +15 -15
  68. data/lib/qube_sync/operations/sales_tax_payment_check.rb +18 -14
  69. data/lib/qube_sync/operations/sales_tax_return_line.rb +2 -2
  70. data/lib/qube_sync/operations/ship_method.rb +5 -5
  71. data/lib/qube_sync/operations/special_account.rb +2 -2
  72. data/lib/qube_sync/operations/standard_terms.rb +5 -5
  73. data/lib/qube_sync/operations/terms.rb +5 -5
  74. data/lib/qube_sync/operations/time_tracking.rb +23 -22
  75. data/lib/qube_sync/operations/to_do.rb +23 -23
  76. data/lib/qube_sync/operations/transaction.rb +15 -10
  77. data/lib/qube_sync/operations/transfer.rb +12 -12
  78. data/lib/qube_sync/operations/transfer_inventory.rb +35 -26
  79. data/lib/qube_sync/operations/txn_display.rb +2 -2
  80. data/lib/qube_sync/operations/unit_of_measure_set.rb +5 -5
  81. data/lib/qube_sync/operations/vehicle.rb +7 -7
  82. data/lib/qube_sync/operations/vehicle_mileage.rb +8 -8
  83. data/lib/qube_sync/operations/vendor.rb +54 -52
  84. data/lib/qube_sync/operations/vendor_credit.rb +144 -100
  85. data/lib/qube_sync/operations/vendor_type.rb +7 -7
  86. data/lib/qube_sync/operations/workers_comp_code.rb +8 -8
  87. data/lib/qube_sync/operations.rb +4 -0
  88. data/lib/qube_sync/version.rb +1 -1
  89. data/lib/qube_sync/workflow_types/cli.rb +120 -0
  90. data/lib/qube_sync/workflow_types/rbs_renderer.rb +290 -0
  91. data/lib/qube_sync/workflow_types/ruby_renderer.rb +391 -0
  92. data/lib/qube_sync/workflow_types/workflow.rb +153 -0
  93. data/lib/qube_sync/workflow_types.rb +219 -0
  94. data/sig/qube_sync/client.rbs +4 -1
  95. data/sig/qube_sync/operations.rbs +1 -0
  96. data/sig/qube_sync/workflow_types.rbs +59 -0
  97. metadata +12 -3
@@ -0,0 +1,219 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "client"
5
+ require_relative "workflow_types/workflow"
6
+ require_relative "workflow_types/ruby_renderer"
7
+ require_relative "workflow_types/rbs_renderer"
8
+ require_relative "workflow_types/cli"
9
+
10
+ module QubeSync
11
+ # Turns an app's workflow input types (Client#workflow_types, GET /api/v2/workflows/types) into
12
+ # a typed Ruby file and its RBS: a QubeWorkflows module with one module per workflow, and a
13
+ # start method per branch of a workflow's input, whose operation bodies are typed as the
14
+ # operation's own params (QubeSync::Operations::<Entity>::<action>_params).
15
+ #
16
+ # files = QubeSync::WorkflowTypes.render(client.workflow_types, source: "https://qubesync.com/api/v2/workflows/types")
17
+ # File.write("lib/qube_workflows.rb", files.ruby)
18
+ # File.write("sig/qube_workflows.rbs", files.rbs)
19
+ #
20
+ # `qube_sync workflows` (exe/qube_sync, WorkflowTypes::CLI) does this, and `--check` compares a
21
+ # written file with what QuBe Sync serves; qube_rails' `bin/rails g qube_rails:workflows` does
22
+ # the same in a Rails app. The same document gives byte-identical files.
23
+ module WorkflowTypes
24
+ # A document this version can't render: not a workflow types document, or a name that isn't
25
+ # one QuBe Sync allows.
26
+ class Error < QubeSync::Error; end
27
+
28
+ COMMAND = "qube_sync workflows"
29
+ WIDTH = 100
30
+ # What a start method's client defaults to when none is given: set once, at boot.
31
+ DEFAULT_CLIENT = "::QubeWorkflows.client"
32
+
33
+ # The rendered files; digest and drafts are what their header records.
34
+ Files = Struct.new(:ruby, :rbs, :digest, :drafts, :warnings, keyword_init: true)
35
+
36
+ # What a generated file's header records: the document's digest, and whether it was made
37
+ # with drafts.
38
+ Header = Struct.new(:digest, :drafts, keyword_init: true)
39
+
40
+ # What a --check found. status is :current, :stale (changes lists the workflows, as
41
+ # "add_transaction (changed)", "resolve (new)", "upsert (removed)"), :unreachable (QuBe Sync
42
+ # didn't answer, or answered 5xx: nothing was checked) or :error (a 401, another 4xx, a file
43
+ # without a header). message is one line.
44
+ Check = Struct.new(:status, :message, :changes, :warnings, keyword_init: true) do
45
+ # 0 when the file is current, and when QuBe Sync couldn't be reached (CI warns, and the
46
+ # file keeps working); 1 otherwise.
47
+ def exit_status
48
+ (status == :current || status == :unreachable) ? 0 : 1
49
+ end
50
+ end
51
+
52
+ module_function
53
+
54
+ # Renders a document's workflows: each published one at its published version, or with
55
+ # `drafts: true` each workflow's draft where it has one. `source` is the URL the header
56
+ # names, `command` the command it says to run again, and `client` the Ruby expression a
57
+ # start method's client defaults to (DEFAULT_CLIENT, with a QubeWorkflows.client= setter; any
58
+ # other has none).
59
+ def render(document, source:, drafts: false, command: COMMAND, client: DEFAULT_CLIENT)
60
+ document = validate(document)
61
+ workflows = select(document, drafts).map { |key, entry| Workflow.new(key, entry) }
62
+ consts = workflows.group_by(&:const).select { |_const, same| same.size > 1 }
63
+ unless consts.empty?
64
+ raise Error, "workflows #{consts.values.flatten.map(&:key).join(", ")} would share a module name"
65
+ end
66
+
67
+ header = header_lines(source, drafts ? "#{command} --drafts" : command, document["digest"], drafts)
68
+ Files.new(
69
+ ruby: RubyRenderer.new(workflows, header: header, client: client).render,
70
+ rbs: RBSRenderer.new(workflows, header: header, client: client).render,
71
+ digest: document["digest"],
72
+ drafts: drafts,
73
+ warnings: warnings(document)
74
+ )
75
+ end
76
+
77
+ # The workflows a render types, by key, sorted: the published ones, each replaced by its
78
+ # draft when drafts is true, and the drafts of workflows never published.
79
+ def select(document, drafts)
80
+ chosen = (document["workflows"] || {}).dup
81
+ (document["drafts"] || {}).each { |key, entry| chosen[key] = entry } if drafts
82
+ chosen.sort.to_h
83
+ end
84
+
85
+ # The digest and drafts a generated file's header records, or nil when it has none.
86
+ def header(source)
87
+ lines = source.to_s.each_line.first(10)
88
+ digest = lines.filter_map { |line| line[/\A# digest: (\S+)\s*\z/, 1] }.first
89
+ drafts = lines.filter_map { |line| line[/\A# drafts: (true|false)\s*\z/, 1] }.first
90
+ return nil unless digest && drafts
91
+
92
+ Header.new(digest: digest, drafts: drafts == "true")
93
+ end
94
+
95
+ # The workflows whose generated module differs between two renders, by key, sorted.
96
+ def changes(before, after)
97
+ old = workflow_blocks(before)
98
+ new = workflow_blocks(after)
99
+ (old.keys | new.keys).sort.filter_map do |key|
100
+ if !old.key?(key)
101
+ "#{key} (new)"
102
+ elsif !new.key?(key)
103
+ "#{key} (removed)"
104
+ elsif old[key] != new[key]
105
+ "#{key} (changed)"
106
+ end
107
+ end
108
+ end
109
+
110
+ # Compares a generated file (its Ruby source) with what QuBe Sync serves now, fetched with the
111
+ # drafts flag the file records. client is a QubeSync::Client, or anything answering
112
+ # workflow_types as it does; path names the file in messages. Takes render's options, so the
113
+ # changes it lists are the ones running `command` again would make.
114
+ def check(existing, client:, source:, path:, command: COMMAND, client_expression: DEFAULT_CLIENT)
115
+ recorded = header(existing)
116
+ unless recorded
117
+ return Check.new(status: :error, changes: [], warnings: [],
118
+ message: "#{path} has no digest header: run `#{command}` to write it")
119
+ end
120
+
121
+ again = recorded.drafts ? "#{command} --drafts" : command
122
+ document =
123
+ begin
124
+ client.workflow_types(include_drafts: recorded.drafts, etag: recorded.digest)
125
+ rescue APIError => e
126
+ return unreachable("QuBe Sync answered #{e.status}") if e.status >= 500
127
+
128
+ return Check.new(status: :error, changes: [], warnings: [], message: e.message)
129
+ rescue *Client::NETWORK_ERRORS, SystemCallError => e
130
+ return unreachable("#{e.class}: #{e.message}")
131
+ end
132
+ return current(path, []) if document == :not_modified
133
+
134
+ files = render(document, source: source, drafts: recorded.drafts, command: command, client: client_expression)
135
+ return current(path, files.warnings) if files.digest == recorded.digest
136
+
137
+ changed = changes(existing, files.ruby)
138
+ what = changed.empty? ? "the served types changed" : changed.join(", ")
139
+ Check.new(status: :stale, changes: changed, warnings: files.warnings,
140
+ message: "#{path} is out of date (#{what}): run `#{again}` and commit the result")
141
+ end
142
+
143
+ def current(path, warnings)
144
+ Check.new(status: :current, changes: [], warnings: warnings, message: "#{path} is up to date")
145
+ end
146
+
147
+ def unreachable(why)
148
+ Check.new(status: :unreachable, changes: [], warnings: [],
149
+ message: "couldn't reach QuBe Sync (#{why}), so the workflow types weren't checked")
150
+ end
151
+
152
+ # A served operations_digest other than this gem's: the document's $operation ids may name
153
+ # operations this version doesn't type.
154
+ def warnings(document)
155
+ served = document["operations_digest"]
156
+ return [] if served.nil? || served == Operations::DIGEST
157
+
158
+ ["QuBe Sync's operations (#{served}) aren't the ones qube_sync #{VERSION} was generated from " \
159
+ "(#{Operations::DIGEST}): an operation it doesn't know is typed as a plain Hash. Update qube_sync to type it."]
160
+ end
161
+
162
+ def validate(document)
163
+ unless document.is_a?(Hash) && document["digest"].is_a?(String) && document["workflows"].is_a?(Hash)
164
+ raise Error, "not a workflow types document (expected digest and workflows)"
165
+ end
166
+ unless document["digest"].match?(/\A[\w:.\-]+\z/)
167
+ raise Error, "not a workflow types document: digest #{document["digest"].inspect}"
168
+ end
169
+
170
+ document
171
+ end
172
+
173
+ # Word-wraps text to WIDTH, as bin/generate-operations does: the first line starts with
174
+ # `first`, the others with `rest`. Splitting on whitespace keeps a newline in a description
175
+ # from ending a comment.
176
+ def wrap(text, first, rest)
177
+ lines = []
178
+ line = first.dup
179
+ fresh = true
180
+ text.to_s.split.each do |word|
181
+ if !fresh && line.length + 1 + word.length > WIDTH
182
+ lines << line
183
+ line = rest + word
184
+ else
185
+ line << (fresh ? "" : " ") << word
186
+ end
187
+ fresh = false
188
+ end
189
+ lines << line
190
+ lines.map(&:rstrip)
191
+ end
192
+
193
+ def header_lines(source, command, digest, drafts)
194
+ [
195
+ "# Generated by `#{command}` from #{source}: don't edit.",
196
+ "# Run it again when a workflow changes; `#{command} --check` fails while this file is out of date.",
197
+ "# digest: #{digest}",
198
+ "# drafts: #{drafts}"
199
+ ]
200
+ end
201
+
202
+ # Each workflow's module in a generated Ruby file, with its comment, by its WORKFLOW key.
203
+ def workflow_blocks(source)
204
+ lines = source.to_s.lines
205
+ blocks = {}
206
+ lines.each_with_index do |line, i|
207
+ next unless line.match?(/\A module [A-Z]\w*$/)
208
+
209
+ first = i
210
+ first -= 1 while first.positive? && lines[first - 1].start_with?(" #")
211
+ last = (i...lines.size).find { |j| lines[j] == " end\n" } || (lines.size - 1)
212
+ block = lines[first..last].join
213
+ key = block[/^ WORKFLOW = "([^"]+)"$/, 1]
214
+ blocks[key] = block if key
215
+ end
216
+ blocks
217
+ end
218
+ end
219
+ end
@@ -50,7 +50,7 @@ module QubeSync
50
50
  def inspect: () -> String
51
51
  def to_s: () -> String
52
52
 
53
- def create_connection: (webhook_url: String, ?name: String?) -> data
53
+ def create_connection: (webhook_url: String, ?name: String?, ?type: String?) -> data
54
54
  def get_connection: (String id) -> data
55
55
  def list_connections: () -> Array[data]
56
56
  def update_connection: (String id, ?webhook_url: String?, ?name: String?) -> data
@@ -61,10 +61,13 @@ module QubeSync
61
61
 
62
62
  def start_run: (String connection_id, String workflow, Hash[untyped, untyped]? input, reference: String, webhook_url: String, ?version: Integer?) -> data
63
63
  def get_run: (String run_url_or_connection_id, ?String? run_id) -> data
64
+ def redeliver_run: (String connection_id, String run_id) -> data
64
65
  def list_runs: (String connection_id, ?reference: String?, ?state: String?, ?outcome: String?) -> Array[data]
65
66
  def cancel_run: (String cancel_url, ?reason: String?) -> data
66
67
  def run_events: (String connection_id, String run_id, ?after: Integer?, ?limit: Integer?) -> data
67
68
 
69
+ def workflow_types: (?include_drafts: bool, ?etag: String?) -> (data | :not_modified)
70
+
68
71
  def answer: (String decisions_url, choice: String, ?data: Hash[untyped, untyped]?) -> data
69
72
  def search: (String search_url, field: String, q: String) -> Array[data]
70
73
 
@@ -4,6 +4,7 @@ module QubeSync
4
4
  module Operations
5
5
  type entry = { method: String, path: String, params_in: :body | :query, paged: bool, entity: String }
6
6
 
7
+ DIGEST: String
7
8
  INDEX: Hash[String, entry]
8
9
  ENTITIES: Hash[String, Symbol]
9
10
 
@@ -0,0 +1,59 @@
1
+ module QubeSync
2
+ module WorkflowTypes
3
+ class Error < QubeSync::Error
4
+ end
5
+
6
+ interface _TypesClient
7
+ def base_url: () -> String
8
+ def workflow_types: (?include_drafts: bool, ?etag: String?) -> (Hash[String, untyped] | :not_modified)
9
+ end
10
+
11
+ COMMAND: String
12
+ WIDTH: Integer
13
+ DEFAULT_CLIENT: String
14
+
15
+ class Files
16
+ attr_accessor ruby: String
17
+ attr_accessor rbs: String
18
+ attr_accessor digest: String
19
+ attr_accessor drafts: bool
20
+ attr_accessor warnings: Array[String]
21
+ end
22
+
23
+ class Header
24
+ attr_accessor digest: String
25
+ attr_accessor drafts: bool
26
+ end
27
+
28
+ class Check
29
+ attr_accessor status: :current | :stale | :unreachable | :error
30
+ attr_accessor message: String
31
+ attr_accessor changes: Array[String]
32
+ attr_accessor warnings: Array[String]
33
+
34
+ def exit_status: () -> Integer
35
+ end
36
+
37
+ def self.render: (Hash[String, untyped] document, source: String, ?drafts: bool, ?command: String, ?client: String) -> Files
38
+ def self.select: (Hash[String, untyped] document, bool drafts) -> Hash[String, Hash[String, untyped]]
39
+ def self.header: (String? source) -> Header?
40
+ def self.changes: (String before, String after) -> Array[String]
41
+ def self.check: (String existing, client: _TypesClient, source: String, path: String, ?command: String, ?client_expression: String) -> Check
42
+ def self.wrap: (String? text, String first, String rest) -> Array[String]
43
+
44
+ class CLI
45
+ interface _Env
46
+ def []: (String name) -> String?
47
+ end
48
+
49
+ interface _Puts
50
+ def puts: (*untyped) -> untyped
51
+ end
52
+
53
+ USAGE: String
54
+
55
+ def initialize: (?env: _Env, ?out: _Puts, ?err: _Puts, ?client_factory: (^(api_key: String, base_url: String) -> _TypesClient)?) -> void
56
+ def run: (Array[String] argv) -> Integer
57
+ end
58
+ end
59
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qube_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garrett Lancaster
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-10-25 00:00:00.000000000 Z
11
+ date: 2026-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -34,7 +34,8 @@ description: "\n Easily create and manage QUBE Sync API resources in Ruby.\n\n
34
34
  connections, queued requests, and more with the QUBE Sync API.\n "
35
35
  email:
36
36
  - support@qubesync.com
37
- executables: []
37
+ executables:
38
+ - qube_sync
38
39
  extensions: []
39
40
  extra_rdoc_files: []
40
41
  files:
@@ -43,6 +44,7 @@ files:
43
44
  - LICENSE
44
45
  - README.md
45
46
  - Rakefile
47
+ - exe/qube_sync
46
48
  - lib/qube_sync.rb
47
49
  - lib/qube_sync/client.rb
48
50
  - lib/qube_sync/operations.rb
@@ -83,6 +85,7 @@ files:
83
85
  - lib/qube_sync/operations/employee_default.rb
84
86
  - lib/qube_sync/operations/entity.rb
85
87
  - lib/qube_sync/operations/estimate.rb
88
+ - lib/qube_sync/operations/fields.rb
86
89
  - lib/qube_sync/operations/form1099_category_account_mapping.rb
87
90
  - lib/qube_sync/operations/general_detail_report.rb
88
91
  - lib/qube_sync/operations/general_summary_report.rb
@@ -174,6 +177,11 @@ files:
174
177
  - lib/qube_sync/version.rb
175
178
  - lib/qube_sync/webhooks.rb
176
179
  - lib/qube_sync/webhooks/event.rb
180
+ - lib/qube_sync/workflow_types.rb
181
+ - lib/qube_sync/workflow_types/cli.rb
182
+ - lib/qube_sync/workflow_types/rbs_renderer.rb
183
+ - lib/qube_sync/workflow_types/ruby_renderer.rb
184
+ - lib/qube_sync/workflow_types/workflow.rb
177
185
  - qube_sync.gemspec
178
186
  - sig/qube_sync.rbs
179
187
  - sig/qube_sync/client.rbs
@@ -303,6 +311,7 @@ files:
303
311
  - sig/qube_sync/operations/vendor_credit.rbs
304
312
  - sig/qube_sync/operations/vendor_type.rbs
305
313
  - sig/qube_sync/operations/workers_comp_code.rbs
314
+ - sig/qube_sync/workflow_types.rbs
306
315
  homepage: https://github.com/qubeintegrations/qube_sync_rb
307
316
  licenses: []
308
317
  metadata: