orb-billing 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38b3bf74b4e8dd32bd70721c88a432e1700d34742b5435fb1f623124f9d69aae
4
- data.tar.gz: 67304b3d1242e85d48a26dd2a108cdac385698fba02c7d6109d850836bb71b91
3
+ metadata.gz: 4ce1529e168d4f47f75f48505cf8e528afc2056e95f075f4611382b97374d1a2
4
+ data.tar.gz: c4f6bd8086298f6950ddeed3480ee75a60836463355acd7f0bb5d70fff40b359
5
5
  SHA512:
6
- metadata.gz: 4d6cdb145141d9b8c1cc43175bb784b1b07288414147c0e8c9eaf36a4928e5c13e0f610c8a16282774c42c5706272b52a78babd80e4ae973dc873c47709be06c
7
- data.tar.gz: 3fa6dd7dd0f921f286993942bb61621d646c8462110ec2a02628aae0eb8a1db9ff3f1fbefd818b4612c3827eaf240b732989a9be008270830388704d5a6bdd30
6
+ metadata.gz: d6a7ff2c8e6ec173a053c6ad9635cd97f372b08baf61bee105e1c736706fa265ebcc90b1a2dbeb05c0d67f5adfb56d157ccc5d355e06b3ce2340c78cdb056a20
7
+ data.tar.gz: 90bc92e99e52b0cb51a208337ccd2d4a6d6cb39cad3990db8518b886c3b471188e0a2981cbb769058cef19f2e391ef78b80eaa721befe6180cc531bcc4bd68aa
data/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.0 (2025-05-14)
4
+
5
+ Full Changelog: [v0.4.0...v0.5.0](https://github.com/orbcorp/orb-ruby/compare/v0.4.0...v0.5.0)
6
+
7
+ ### Features
8
+
9
+ * bump default connection pool size limit to minimum of 99 ([79f9994](https://github.com/orbcorp/orb-ruby/commit/79f9994e2a77d786a7e1ffaa52c56916835b9b5f))
10
+
11
+
12
+ ### Chores
13
+
14
+ * **internal:** version bump ([cfc1793](https://github.com/orbcorp/orb-ruby/commit/cfc17939f09ece0217ea7683991e509c30491e96))
15
+
16
+
17
+ ### Documentation
18
+
19
+ * rewrite much of README.md for readability ([ac8a45d](https://github.com/orbcorp/orb-ruby/commit/ac8a45d8765183d41fbeaf23d1bd03c372908b45))
20
+
3
21
  ## 0.4.0 (2025-05-13)
4
22
 
5
23
  Full Changelog: [v0.3.2...v0.4.0](https://github.com/orbcorp/orb-ruby/compare/v0.3.2...v0.4.0)
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Orb Ruby API library
2
2
 
3
- The Orb Ruby library provides convenient access to the Orb REST API from any Ruby 3.2.0+ application.
3
+ The Orb Ruby library provides convenient access to the Orb REST API from any Ruby 3.2.0+ application. It ships with comprehensive types & docstrings in Yard, RBS, and RBI – [see below](https://github.com/orbcorp/orb-ruby#Sorbet) for usage with Sorbet. The standard library's `net/http` is used as the HTTP transport, with connection pooling via the `connection_pool` gem.
4
4
 
5
5
  ## Documentation
6
6
 
@@ -15,7 +15,7 @@ To use this gem, install via Bundler by adding the following to your application
15
15
  <!-- x-release-please-start-version -->
16
16
 
17
17
  ```ruby
18
- gem "orb-billing", "~> 0.4.0"
18
+ gem "orb-billing", "~> 0.5.0"
19
19
  ```
20
20
 
21
21
  <!-- x-release-please-end -->
@@ -35,16 +35,6 @@ customer = orb.customers.create(email: "example-customer@withorb.com", name: "My
35
35
  puts(customer.id)
36
36
  ```
37
37
 
38
- ## Sorbet
39
-
40
- This library is written with [Sorbet type definitions](https://sorbet.org/docs/rbi). However, there is no runtime dependency on the `sorbet-runtime`.
41
-
42
- When using sorbet, it is recommended to use model classes as below. This provides stronger type checking and tooling integration.
43
-
44
- ```ruby
45
- orb.customers.create(email: "example-customer@withorb.com", name: "My Customer")
46
- ```
47
-
48
38
  ### Pagination
49
39
 
50
40
  List methods in the Orb API are paginated.
@@ -64,15 +54,30 @@ page.auto_paging_each do |coupon|
64
54
  end
65
55
  ```
66
56
 
67
- ### Errors
57
+ Alternatively, you can use the `#next_page?` and `#next_page` methods for more granular control working with pages.
58
+
59
+ ```ruby
60
+ if page.next_page?
61
+ new_page = page.next_page
62
+ puts(new_page.data[0].id)
63
+ end
64
+ ```
65
+
66
+ ### Handling errors
68
67
 
69
68
  When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of `Orb::Errors::APIError` will be thrown:
70
69
 
71
70
  ```ruby
72
71
  begin
73
72
  customer = orb.customers.create(email: "example-customer@withorb.com", name: "My Customer")
74
- rescue Orb::Errors::APIError => e
75
- puts(e.status) # 400
73
+ rescue Orb::Errors::APIConnectionError => e
74
+ puts("The server could not be reached")
75
+ puts(e.cause) # an underlying Exception, likely raised within `net/http`
76
+ rescue Orb::Errors::RateLimitError => e
77
+ puts("A 429 status code was received; we should back off a bit.")
78
+ rescue Orb::Errors::APIStatusError => e
79
+ puts("Another non-200-range status code was received")
80
+ puts(e.status)
76
81
  end
77
82
  ```
78
83
 
@@ -116,11 +121,7 @@ orb.customers.create(
116
121
 
117
122
  ### Timeouts
118
123
 
119
- By default, requests will time out after 60 seconds.
120
-
121
- Timeouts are applied separately to the initial connection and the overall request time, so in some cases a request could wait 2\*timeout seconds before it fails.
122
-
123
- You can use the `timeout` option to configure or disable this:
124
+ By default, requests will time out after 60 seconds. You can use the timeout option to configure or disable this:
124
125
 
125
126
  ```ruby
126
127
  # Configure the default for all requests:
@@ -136,40 +137,54 @@ orb.customers.create(
136
137
  )
137
138
  ```
138
139
 
139
- ## Model DSL
140
+ On timeout, `Orb::Errors::APITimeoutError` is raised.
140
141
 
141
- This library uses a simple DSL to represent request parameters and response shapes in `lib/orb/models`.
142
+ Note that requests that time out are retried by default.
142
143
 
143
- With the right [editor plugins](https://shopify.github.io/ruby-lsp), you can ctrl-click on elements of the DSL to navigate around and explore the library.
144
+ ## Advanced concepts
144
145
 
145
- In all places where a `BaseModel` type is specified, vanilla Ruby `Hash` can also be used. For example, the following are interchangeable as arguments:
146
+ ### BaseModel
146
147
 
147
- ```ruby
148
- # This has tooling readability, for auto-completion, static analysis, and goto definition with supported language services
149
- params = Orb::Models::CustomerCreateParams.new(email: "example-customer@withorb.com", name: "My Customer")
148
+ All parameter and response objects inherit from `Orb::Internal::Type::BaseModel`, which provides several conveniences, including:
150
149
 
151
- # This also works
152
- params = {
153
- email: "example-customer@withorb.com",
154
- name: "My Customer"
155
- }
156
- ```
150
+ 1. All fields, including unknown ones, are accessible with `obj[:prop]` syntax, and can be destructured with `obj => {prop: prop}` or pattern-matching syntax.
157
151
 
158
- ## Editor support
152
+ 2. Structural equivalence for equality; if two API calls return the same values, comparing the responses with == will return true.
159
153
 
160
- A combination of [Shopify LSP](https://shopify.github.io/ruby-lsp) and [Solargraph](https://solargraph.org/) is recommended for non-[Sorbet](https://sorbet.org) users. The former is especially good at go to definition, while the latter has much better auto-completion support.
154
+ 3. Both instances and the classes themselves can be pretty-printed.
161
155
 
162
- ## Advanced concepts
156
+ 4. Helpers such as `#to_h`, `#deep_to_h`, `#to_json`, and `#to_yaml`.
157
+
158
+ ### Making custom or undocumented requests
159
+
160
+ #### Undocumented properties
163
161
 
164
- ### Making custom/undocumented requests
162
+ You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:
163
+
164
+ Note: the `extra_` parameters of the same name overrides the documented parameters.
165
+
166
+ ```ruby
167
+ customer =
168
+ orb.customers.create(
169
+ email: "example-customer@withorb.com",
170
+ name: "My Customer",
171
+ request_options: {
172
+ extra_query: {my_query_parameter: value},
173
+ extra_body: {my_body_parameter: value},
174
+ extra_headers: {"my-header": value}
175
+ }
176
+ )
177
+
178
+ puts(customer[:my_undocumented_property])
179
+ ```
165
180
 
166
181
  #### Undocumented request params
167
182
 
168
- If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` under the `request_options:` parameter when making a requests as seen in examples above.
183
+ If you want to explicitly send an extra param, you can do so with the `extra_query`, `extra_body`, and `extra_headers` under the `request_options:` parameter when making a request as seen in examples above.
169
184
 
170
185
  #### Undocumented endpoints
171
186
 
172
- To make requests to undocumented endpoints, you can make requests using `client.request`. Options on the client will be respected (such as retries) when making this request.
187
+ To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using `client.request`, like so:
173
188
 
174
189
  ```ruby
175
190
  response = client.request(
@@ -177,42 +192,67 @@ response = client.request(
177
192
  path: '/undocumented/endpoint',
178
193
  query: {"dog": "woof"},
179
194
  headers: {"useful-header": "interesting-value"},
180
- body: {"he": "llo"},
195
+ body: {"hello": "world"}
181
196
  )
182
197
  ```
183
198
 
184
199
  ### Concurrency & connection pooling
185
200
 
186
- The `Orb::Client` instances are thread-safe, and should be re-used across multiple threads. By default, each `Client` have their own HTTP connection pool, with a maximum number of connections equal to thread count.
201
+ The `Orb::Client` instances are threadsafe, but only are fork-safe when there are no in-flight HTTP requests.
202
+
203
+ Each instance of `Orb::Client` has its own HTTP connection pool with a default size of 99. As such, we recommend instantiating the client once per application in most settings.
187
204
 
188
- When the maximum number of connections has been checked out from the connection pool, the `Client` will wait for an in use connection to become available. The queue time for this mechanism is accounted for by the per-request timeout.
205
+ When all available connections from the pool are checked out, requests wait for a new connection to become available, with queue time counting towards the request timeout.
189
206
 
190
207
  Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
191
208
 
192
- Currently, `Orb::Client` instances are only fork-safe if there are no in-flight HTTP requests.
209
+ ## Sorbet
193
210
 
194
- ### Sorbet
211
+ This library provides comprehensive [RBI](https://sorbet.org/docs/rbi) definitions, and has no dependency on sorbet-runtime.
195
212
 
196
- #### Enums
213
+ You can provide typesafe request parameters like so:
197
214
 
198
- Sorbet's typed enums require sub-classing of the [`T::Enum` class](https://sorbet.org/docs/tenum) from the `sorbet-runtime` gem.
215
+ ```ruby
216
+ orb.customers.create(email: "example-customer@withorb.com", name: "My Customer")
217
+ ```
199
218
 
200
- Since this library does not depend on `sorbet-runtime`, it uses a [`T.all` intersection type](https://sorbet.org/docs/intersection-types) with a ruby primitive type to construct a "tagged alias" instead.
219
+ Or, equivalently:
201
220
 
202
221
  ```ruby
203
- module Orb::BillingCycleRelativeDate
204
- # This alias aids language service driven navigation.
205
- TaggedSymbol = T.type_alias { T.all(Symbol, Orb::BillingCycleRelativeDate) }
206
- end
222
+ # Hashes work, but are not typesafe:
223
+ orb.customers.create(email: "example-customer@withorb.com", name: "My Customer")
224
+
225
+ # You can also splat a full Params class:
226
+ params = Orb::CustomerCreateParams.new(email: "example-customer@withorb.com", name: "My Customer")
227
+ orb.customers.create(**params)
207
228
  ```
208
229
 
209
- #### Argument passing trick
230
+ ### Enums
210
231
 
211
- It is possible to pass a compatible model / parameter class to a method that expects keyword arguments by using the `**` splat operator.
232
+ Since this library does not depend on `sorbet-runtime`, it cannot provide [`T::Enum`](https://sorbet.org/docs/tenum) instances. Instead, we provide "tagged symbols" instead, which is always a primitive at runtime:
212
233
 
213
234
  ```ruby
214
- params = Orb::Models::CustomerCreateParams.new(email: "example-customer@withorb.com", name: "My Customer")
215
- orb.customers.create(**params)
235
+ # :duplicate
236
+ puts(Orb::CreditNoteCreateParams::Reason::DUPLICATE)
237
+
238
+ # Revealed type: `T.all(Orb::CreditNoteCreateParams::Reason, Symbol)`
239
+ T.reveal_type(Orb::CreditNoteCreateParams::Reason::DUPLICATE)
240
+ ```
241
+
242
+ Enum parameters have a "relaxed" type, so you can either pass in enum constants or their literal value:
243
+
244
+ ```ruby
245
+ # Using the enum constants preserves the tagged type information:
246
+ orb.credit_notes.create(
247
+ reason: Orb::CreditNoteCreateParams::Reason::DUPLICATE,
248
+ # …
249
+ )
250
+
251
+ # Literal values is also permissible:
252
+ orb.credit_notes.create(
253
+ reason: :duplicate,
254
+ # …
255
+ )
216
256
  ```
217
257
 
218
258
  ## Versioning
data/lib/orb/client.rb CHANGED
@@ -91,10 +91,10 @@ module Orb
91
91
  def initialize(
92
92
  api_key: ENV["ORB_API_KEY"],
93
93
  base_url: ENV["ORB_BASE_URL"],
94
- max_retries: Orb::Client::DEFAULT_MAX_RETRIES,
95
- timeout: Orb::Client::DEFAULT_TIMEOUT_IN_SECONDS,
96
- initial_retry_delay: Orb::Client::DEFAULT_INITIAL_RETRY_DELAY,
97
- max_retry_delay: Orb::Client::DEFAULT_MAX_RETRY_DELAY,
94
+ max_retries: self.class::DEFAULT_MAX_RETRIES,
95
+ timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
96
+ initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
97
+ max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY,
98
98
  idempotency_header: "Idempotency-Key"
99
99
  )
100
100
  base_url ||= "https://api.withorb.com/v1"
@@ -11,6 +11,8 @@ module Orb
11
11
  # https://github.com/golang/go/blob/c8eced8580028328fde7c03cbfcb720ce15b2358/src/net/http/transport.go#L49
12
12
  KEEP_ALIVE_TIMEOUT = 30
13
13
 
14
+ DEFAULT_MAX_CONNECTIONS = [Etc.nprocessors, 99].max
15
+
14
16
  class << self
15
17
  # @api private
16
18
  #
@@ -184,7 +186,7 @@ module Orb
184
186
  # @api private
185
187
  #
186
188
  # @param size [Integer]
187
- def initialize(size: Etc.nprocessors)
189
+ def initialize(size: self.class::DEFAULT_MAX_CONNECTIONS)
188
190
  @mutex = Mutex.new
189
191
  @size = size
190
192
  @pools = {}
@@ -386,6 +386,14 @@ module Orb
386
386
  # @param keys [Array<Symbol>, nil]
387
387
  #
388
388
  # @return [Hash{Symbol=>Object}]
389
+ #
390
+ # @example
391
+ # # `amount_discount` is a `Orb::AmountDiscount`
392
+ # amount_discount => {
393
+ # amount_discount: amount_discount,
394
+ # applies_to_price_ids: applies_to_price_ids,
395
+ # discount_type: discount_type
396
+ # }
389
397
  def deconstruct_keys(keys)
390
398
  (keys || self.class.known_fields.keys)
391
399
  .filter_map do |k|
data/lib/orb/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Orb
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -22,6 +22,8 @@ module Orb
22
22
  # https://github.com/golang/go/blob/c8eced8580028328fde7c03cbfcb720ce15b2358/src/net/http/transport.go#L49
23
23
  KEEP_ALIVE_TIMEOUT = 30
24
24
 
25
+ DEFAULT_MAX_CONNECTIONS = T.let(T.unsafe(nil), Integer)
26
+
25
27
  class << self
26
28
  # @api private
27
29
  sig { params(url: URI::Generic).returns(Net::HTTP) }
@@ -66,7 +68,9 @@ module Orb
66
68
 
67
69
  # @api private
68
70
  sig { params(size: Integer).returns(T.attached_class) }
69
- def self.new(size: Etc.nprocessors)
71
+ def self.new(
72
+ size: Orb::Internal::Transport::PooledNetRequester::DEFAULT_MAX_CONNECTIONS
73
+ )
70
74
  end
71
75
  end
72
76
  end
@@ -15,6 +15,8 @@ module Orb
15
15
 
16
16
  KEEP_ALIVE_TIMEOUT: 30
17
17
 
18
+ DEFAULT_MAX_CONNECTIONS: Integer
19
+
18
20
  def self.connect: (URI::Generic url) -> top
19
21
 
20
22
  def self.calibrate_socket_timeout: (top conn, Float deadline) -> void
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orb-billing
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
  - Orb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-05-13 00:00:00.000000000 Z
11
+ date: 2025-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool