Skip to content

Commit d4cb661

Browse files
docs: rewrite much of README.md for readability
1 parent 9b07067 commit d4cb661

2 files changed

Lines changed: 101 additions & 53 deletions

File tree

README.md

Lines changed: 93 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Lithic Ruby API library
22

3-
The Lithic Ruby library provides convenient access to the Lithic REST API from any Ruby 3.2.0+ application.
3+
The Lithic Ruby library provides convenient access to the Lithic 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/lithic-com/lithic-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

@@ -36,16 +36,6 @@ card = lithic.cards.create(type: "SINGLE_USE")
3636
puts(card.token)
3737
```
3838

39-
## Sorbet
40-
41-
This library is written with [Sorbet type definitions](https://sorbet.org/docs/rbi). However, there is no runtime dependency on the `sorbet-runtime`.
42-
43-
When using sorbet, it is recommended to use model classes as below. This provides stronger type checking and tooling integration.
44-
45-
```ruby
46-
lithic.cards.create(type: "SINGLE_USE")
47-
```
48-
4939
### Pagination
5040

5141
List methods in the Lithic API are paginated.
@@ -65,15 +55,30 @@ page.auto_paging_each do |card|
6555
end
6656
```
6757

68-
### Errors
58+
Alternatively, you can use the `#next_page?` and `#next_page` methods for more granular control working with pages.
59+
60+
```ruby
61+
if page.next_page?
62+
new_page = page.next_page
63+
puts(new_page.data[0].product_id)
64+
end
65+
```
66+
67+
### Handling errors
6968

7069
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 `Lithic::Errors::APIError` will be thrown:
7170

7271
```ruby
7372
begin
7473
card = lithic.cards.create(type: "MERCHANT_LOCKED")
75-
rescue Lithic::Errors::APIError => e
76-
puts(e.status) # 400
74+
rescue Lithic::Errors::APIConnectionError => e
75+
puts("The server could not be reached")
76+
puts(e.cause) # an underlying Exception, likely raised within `net/http`
77+
rescue Lithic::Errors::RateLimitError => e
78+
puts("A 429 status code was received; we should back off a bit.")
79+
rescue Lithic::Errors::APIStatusError => e
80+
puts("Another non-200-range status code was received")
81+
puts(e.status)
7782
end
7883
```
7984

@@ -113,11 +118,7 @@ lithic.cards.list(page_size: 10, request_options: {max_retries: 5})
113118

114119
### Timeouts
115120

116-
By default, requests will time out after 60 seconds.
117-
118-
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.
119-
120-
You can use the `timeout` option to configure or disable this:
121+
By default, requests will time out after 60 seconds. You can use the timeout option to configure or disable this:
121122

122123
```ruby
123124
# Configure the default for all requests:
@@ -129,82 +130,121 @@ lithic = Lithic::Client.new(
129130
lithic.cards.list(page_size: 10, request_options: {timeout: 5})
130131
```
131132

132-
## Model DSL
133+
On timeout, `Lithic::Errors::APITimeoutError` is raised.
133134

134-
This library uses a simple DSL to represent request parameters and response shapes in `lib/lithic/models`.
135+
Note that requests that time out are retried by default.
135136

136-
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.
137+
## Advanced concepts
137138

138-
In all places where a `BaseModel` type is specified, vanilla Ruby `Hash` can also be used. For example, the following are interchangeable as arguments:
139+
### BaseModel
139140

140-
```ruby
141-
# This has tooling readability, for auto-completion, static analysis, and goto definition with supported language services
142-
params = Lithic::Models::CardCreateParams.new(type: "SINGLE_USE")
141+
All parameter and response objects inherit from `Lithic::Internal::Type::BaseModel`, which provides several conveniences, including:
143142

144-
# This also works
145-
params = {
146-
type: "SINGLE_USE"
147-
}
148-
```
143+
1. All fields, including unknown ones, are accessible with `obj[:prop]` syntax, and can be destructured with `obj => {prop: prop}` or pattern-matching syntax.
149144

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

152-
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.
147+
3. Both instances and the classes themselves can be pretty-printed.
153148

154-
## Advanced concepts
149+
4. Helpers such as `#to_h`, `#deep_to_h`, `#to_json`, and `#to_yaml`.
150+
151+
### Making custom or undocumented requests
152+
153+
#### Undocumented properties
154+
155+
You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:
155156

156-
### Making custom/undocumented requests
157+
Note: the `extra_` parameters of the same name overrides the documented parameters.
158+
159+
```ruby
160+
page =
161+
lithic.cards.list(
162+
page_size: 10,
163+
request_options: {
164+
extra_query: {my_query_parameter: value},
165+
extra_body: {my_body_parameter: value},
166+
extra_headers: {"my-header": value}
167+
}
168+
)
169+
170+
puts(page[:my_undocumented_property])
171+
```
157172

158173
#### Undocumented request params
159174

160-
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.
175+
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.
161176

162177
#### Undocumented endpoints
163178

164-
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.
179+
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:
165180

166181
```ruby
167182
response = client.request(
168183
method: :post,
169184
path: '/undocumented/endpoint',
170185
query: {"dog": "woof"},
171186
headers: {"useful-header": "interesting-value"},
172-
body: {"he": "llo"},
187+
body: {"hello": "world"}
173188
)
174189
```
175190

176191
### Concurrency & connection pooling
177192

178-
The `Lithic::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.
193+
The `Lithic::Client` instances are threadsafe, but only are fork-safe when there are no in-flight HTTP requests.
179194

180-
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.
195+
Each instance of `Lithic::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.
196+
197+
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.
181198

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

184-
Currently, `Lithic::Client` instances are only fork-safe if there are no in-flight HTTP requests.
201+
## Sorbet
185202

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

188-
#### Enums
205+
You can provide typesafe request parameters like so:
189206

190-
Sorbet's typed enums require sub-classing of the [`T::Enum` class](https://sorbet.org/docs/tenum) from the `sorbet-runtime` gem.
207+
```ruby
208+
lithic.cards.create(type: "SINGLE_USE")
209+
```
191210

192-
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.
211+
Or, equivalently:
193212

194213
```ruby
195-
module Lithic::AccountFinancialAccountType
196-
# This alias aids language service driven navigation.
197-
TaggedSymbol = T.type_alias { T.all(Symbol, Lithic::AccountFinancialAccountType) }
198-
end
214+
# Hashes work, but are not typesafe:
215+
lithic.cards.create(type: "SINGLE_USE")
216+
217+
# You can also splat a full Params class:
218+
params = Lithic::CardCreateParams.new(type: "SINGLE_USE")
219+
lithic.cards.create(**params)
199220
```
200221

201-
#### Argument passing trick
222+
### Enums
202223

203-
It is possible to pass a compatible model / parameter class to a method that expects keyword arguments by using the `**` splat operator.
224+
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:
204225

205226
```ruby
206-
params = Lithic::Models::CardCreateParams.new(type: "SINGLE_USE")
207-
lithic.cards.create(**params)
227+
# :ACTIVE
228+
puts(Lithic::AccountUpdateParams::State::ACTIVE)
229+
230+
# Revealed type: `T.all(Lithic::AccountUpdateParams::State, Symbol)`
231+
T.reveal_type(Lithic::AccountUpdateParams::State::ACTIVE)
232+
```
233+
234+
Enum parameters have a "relaxed" type, so you can either pass in enum constants or their literal value:
235+
236+
```ruby
237+
# Using the enum constants preserves the tagged type information:
238+
lithic.accounts.update(
239+
state: Lithic::AccountUpdateParams::State::ACTIVE,
240+
#
241+
)
242+
243+
# Literal values is also permissible:
244+
lithic.accounts.update(
245+
state: :ACTIVE,
246+
#
247+
)
208248
```
209249

210250
## Versioning

lib/lithic/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+
# # `address` is a `Lithic::Address`
392+
# address => {
393+
# address1: address1,
394+
# city: city,
395+
# country: country
396+
# }
389397
def deconstruct_keys(keys)
390398
(keys || self.class.known_fields.keys)
391399
.filter_map do |k|

0 commit comments

Comments
 (0)