Skip to content

Commit da6e9ff

Browse files
authored
Merge pull request #258 from orbcorp/release-please--branches--main--changes--next
release: 0.5.0
2 parents aeeafbc + 1336d41 commit da6e9ff

File tree

10 files changed

+138
-64
lines changed

10 files changed

+138
-64
lines changed

.release-please-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "0.4.0"
2+
".": "0.5.0"
33
}

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# Changelog
22

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+
321
## 0.4.0 (2025-05-13)
422

523
Full Changelog: [v0.3.2...v0.4.0](https://github.com/orbcorp/orb-ruby/compare/v0.3.2...v0.4.0)

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ GIT
1111
PATH
1212
remote: .
1313
specs:
14-
orb-billing (0.3.2)
14+
orb-billing (0.4.0)
1515
connection_pool
1616

1717
GEM

README.md

Lines changed: 95 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Orb Ruby API library
22

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.
44

55
## Documentation
66

@@ -15,7 +15,7 @@ To use this gem, install via Bundler by adding the following to your application
1515
<!-- x-release-please-start-version -->
1616

1717
```ruby
18-
gem "orb-billing", "~> 0.4.0"
18+
gem "orb-billing", "~> 0.5.0"
1919
```
2020

2121
<!-- x-release-please-end -->
@@ -35,16 +35,6 @@ customer = orb.customers.create(email: "example-customer@withorb.com", name: "My
3535
puts(customer.id)
3636
```
3737

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-
4838
### Pagination
4939

5040
List methods in the Orb API are paginated.
@@ -64,15 +54,30 @@ page.auto_paging_each do |coupon|
6454
end
6555
```
6656

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
6867

6968
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:
7069

7170
```ruby
7271
begin
7372
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)
7681
end
7782
```
7883

@@ -116,11 +121,7 @@ orb.customers.create(
116121

117122
### Timeouts
118123

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:
124125

125126
```ruby
126127
# Configure the default for all requests:
@@ -136,83 +137,122 @@ orb.customers.create(
136137
)
137138
```
138139

139-
## Model DSL
140+
On timeout, `Orb::Errors::APITimeoutError` is raised.
140141

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.
142143

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
144145

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
146147

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:
150149

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.
157151

158-
## Editor support
152+
2. Structural equivalence for equality; if two API calls return the same values, comparing the responses with == will return true.
159153

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.
161155

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
163161

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+
```
165180

166181
#### Undocumented request params
167182

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.
169184

170185
#### Undocumented endpoints
171186

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:
173188

174189
```ruby
175190
response = client.request(
176191
method: :post,
177192
path: '/undocumented/endpoint',
178193
query: {"dog": "woof"},
179194
headers: {"useful-header": "interesting-value"},
180-
body: {"he": "llo"},
195+
body: {"hello": "world"}
181196
)
182197
```
183198

184199
### Concurrency & connection pooling
185200

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.
187204

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.
189206

190207
Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
191208

192-
Currently, `Orb::Client` instances are only fork-safe if there are no in-flight HTTP requests.
209+
## Sorbet
193210

194-
### Sorbet
211+
This library provides comprehensive [RBI](https://sorbet.org/docs/rbi) definitions, and has no dependency on sorbet-runtime.
195212

196-
#### Enums
213+
You can provide typesafe request parameters like so:
197214

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+
```
199218

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:
201220

202221
```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)
207228
```
208229

209-
#### Argument passing trick
230+
### Enums
210231

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:
212233

213234
```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+
)
216256
```
217257

218258
## Versioning

lib/orb/client.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ class Client < Orb::Internal::Transport::BaseClient
9191
def initialize(
9292
api_key: ENV["ORB_API_KEY"],
9393
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,
9898
idempotency_header: "Idempotency-Key"
9999
)
100100
base_url ||= "https://api.withorb.com/v1"

lib/orb/internal/transport/pooled_net_requester.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class PooledNetRequester
1111
# https://github.com/golang/go/blob/c8eced8580028328fde7c03cbfcb720ce15b2358/src/net/http/transport.go#L49
1212
KEEP_ALIVE_TIMEOUT = 30
1313

14+
DEFAULT_MAX_CONNECTIONS = [Etc.nprocessors, 99].max
15+
1416
class << self
1517
# @api private
1618
#
@@ -184,7 +186,7 @@ def execute(request)
184186
# @api private
185187
#
186188
# @param size [Integer]
187-
def initialize(size: Etc.nprocessors)
189+
def initialize(size: self.class::DEFAULT_MAX_CONNECTIONS)
188190
@mutex = Mutex.new
189191
@size = size
190192
@pools = {}

lib/orb/internal/type/base_model.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@ def deep_to_h = self.class.recursively_to_h(@data, convert: false)
386386
# @param keys [Array<Symbol>, nil]
387387
#
388388
# @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+
# }
389397
def deconstruct_keys(keys)
390398
(keys || self.class.known_fields.keys)
391399
.filter_map do |k|

lib/orb/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Orb
4-
VERSION = "0.4.0"
4+
VERSION = "0.5.0"
55
end

rbi/orb/internal/transport/pooled_net_requester.rbi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module Orb
2222
# https://github.com/golang/go/blob/c8eced8580028328fde7c03cbfcb720ce15b2358/src/net/http/transport.go#L49
2323
KEEP_ALIVE_TIMEOUT = 30
2424

25+
DEFAULT_MAX_CONNECTIONS = T.let(T.unsafe(nil), Integer)
26+
2527
class << self
2628
# @api private
2729
sig { params(url: URI::Generic).returns(Net::HTTP) }
@@ -66,7 +68,9 @@ module Orb
6668

6769
# @api private
6870
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+
)
7074
end
7175
end
7276
end

sig/orb/internal/transport/pooled_net_requester.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Orb
1515

1616
KEEP_ALIVE_TIMEOUT: 30
1717

18+
DEFAULT_MAX_CONNECTIONS: Integer
19+
1820
def self.connect: (URI::Generic url) -> top
1921

2022
def self.calibrate_socket_timeout: (top conn, Float deadline) -> void

0 commit comments

Comments
 (0)