Skip to content

Commit e6161d9

Browse files
Update readme
1 parent 09515b1 commit e6161d9

File tree

1 file changed

+222
-4
lines changed

1 file changed

+222
-4
lines changed

README.md

Lines changed: 222 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,235 @@ SDK for the Seam API written in Ruby.
77

88
## Description
99

10-
TODO
10+
[Seam](https://seam.co) makes it easy to integrate IoT devices with your applications.
11+
This is an official SDK for the Seam API.
12+
Please refer to the official [Seam Docs](https://docs.seam.co/latest/) to get started.
13+
14+
Parts of this SDK are generated from always up-to-date type information
15+
provided by [@seamapi/types](https://github.com/seamapi/types/) node package.
16+
This ensures all API methods, request shapes, and response shapes are
17+
accurate and fully typed.
1118

1219
## Installation
1320

14-
Add this as a dependency to your project using [Bundler] with
21+
Add this as a dependency to your project using [Bundler](https://bundler.io/) with:
1522

16-
```
23+
```bash
1724
$ bundle add seam
1825
```
1926

20-
[bundler]: https://bundler.io/
27+
## Usage
28+
29+
### Examples
30+
31+
**Note:** _These examples assume `SEAM_API_KEY` is set in your environment._
32+
33+
#### List devices
34+
35+
```ruby
36+
require 'seam'
37+
38+
seam = Seam.new
39+
devices = seam.devices.list
40+
```
41+
42+
#### Unlock a door
43+
44+
```ruby
45+
require 'seam'
46+
47+
seam = Seam.new
48+
lock = seam.locks.get(name: "Front Door")
49+
seam.locks.unlock_door(device_id: lock.device_id)
50+
```
51+
52+
### Authentication Method
53+
54+
The SDK supports API key and personal access token authentication mechanisms.
55+
Authentication may be configured by passing the corresponding options directly to the `Seam` constructor, or with the more ergonomic static factory methods.
56+
57+
#### API Key
58+
59+
An API key is scoped to a single workspace and should only be used on the server.
60+
Obtain one from the Seam Console.
61+
62+
```ruby
63+
# Set the `SEAM_API_KEY` environment variable
64+
seam = Seam.new
65+
66+
# Pass as a keyword argument to the constructor
67+
seam = Seam.new(api_key: "your-api-key")
68+
69+
# Use the factory method
70+
seam = Seam.from_api_key("your-api-key")
71+
```
72+
73+
#### Personal Access Token
74+
75+
A Personal Access Token is scoped to a Seam Console user.
76+
Obtain one from the Seam Console.
77+
A workspace ID must be provided when using this method and all requests will be scoped to that workspace.
78+
79+
```ruby
80+
# Pass as an option to the constructor
81+
seam = Seam.new(
82+
personal_access_token: "your-personal-access-token",
83+
workspace_id: "your-workspace-id"
84+
)
85+
86+
# Use the factory method
87+
seam = Seam.from_personal_access_token(
88+
"your-personal-access-token",
89+
"your-workspace-id"
90+
)
91+
```
92+
93+
### Action Attempts
94+
95+
Some asynchronous operations, e.g., unlocking a door, return an
96+
[action attempt](https://docs.seam.co/latest/core-concepts/action-attempts).
97+
Seam tracks the progress of the requested operation and updates the action attempt
98+
when it succeeds or fails.
99+
100+
To make working with action attempts more convenient for applications,
101+
this library provides the `wait_for_action_attempt` option and enables it by default.
102+
103+
When the `wait_for_action_attempt` option is enabled, the SDK:
104+
105+
- Polls the action attempt up to the `timeout`
106+
at the `polling_interval` (both in seconds).
107+
- Resolves with a fresh copy of the successful action attempt.
108+
- Raises a `Seam::ActionAttemptFailedError` if the action attempt is unsuccessful.
109+
- Raises a `Seam::ActionAttemptTimeoutError` if the action attempt is still pending when the `timeout` is reached.
110+
- Both errors expose an `action_attempt` property.
111+
112+
If you already have an action attempt ID
113+
and want to wait for it to resolve, simply use:
114+
115+
```ruby
116+
seam.action_attempts.get(action_attempt_id: action_attempt_id)
117+
```
118+
119+
Or, to get the current state of an action attempt by ID without waiting:
120+
121+
```ruby
122+
seam.action_attempts.get(
123+
action_attempt_id: action_attempt_id,
124+
wait_for_action_attempt: false
125+
)
126+
```
127+
128+
To disable this behavior, set the default option for the client:
129+
130+
```ruby
131+
seam = Seam.new(
132+
api_key: "your-api-key",
133+
wait_for_action_attempt: false
134+
)
135+
136+
seam.locks.unlock_door(device_id: device_id)
137+
```
138+
139+
or the behavior may be configured per-request:
140+
141+
```ruby
142+
seam.locks.unlock_door(
143+
device_id: device_id,
144+
wait_for_action_attempt: false
145+
)
146+
```
147+
148+
The `polling_interval` and `timeout` may be configured for the client or per-request.
149+
For example:
150+
151+
```ruby
152+
require 'seam'
153+
154+
seam = Seam.new("your-api-key")
155+
156+
locks = seam.locks.list
157+
158+
if locks.empty?
159+
raise "No locks in this workspace"
160+
end
161+
162+
lock = locks.first
163+
164+
begin
165+
seam.locks.unlock_door(
166+
device_id: lock.device_id,
167+
wait_for_action_attempt: {
168+
timeout: 5.0,
169+
polling_interval: 1.0
170+
}
171+
)
172+
173+
puts "Door unlocked"
174+
rescue Seam::ActionAttemptFailedError
175+
puts "Could not unlock the door"
176+
rescue Seam::ActionAttemptTimeoutError
177+
puts "Door took too long to unlock"
178+
end
179+
```
180+
181+
### Interacting with Multiple Workspaces
182+
183+
Some Seam API endpoints interact with multiple workspaces. The `Seam::Http::SeamMultiWorkspace` client is not bound to a specific workspace and may use those endpoints with a personal access token authentication method.
184+
185+
A Personal Access Token is scoped to a Seam Console user. Obtain one from the Seam Console.
186+
187+
```ruby
188+
# Pass as an option to the constructor
189+
seam = Seam::Http::SeamMultiWorkspace.new(personal_access_token: "your-personal-access-token")
190+
191+
# Use the factory method
192+
seam = Seam::Http::SeamMultiWorkspace.from_personal_access_token("your-personal-access-token")
193+
194+
# List workspaces authorized for this Personal Access Token
195+
workspaces = seam.workspaces.list
196+
```
197+
198+
### Webhooks
199+
200+
The Seam API implements webhooks using [Svix](https://www.svix.com). This SDK exports a thin wrapper `Seam::Webhook` around the svix package. Use it to parse and validate Seam webhook events.
201+
202+
Refer to the [Svix docs on Consuming Webhooks](https://docs.svix.com/receiving/introduction) for an in-depth guide on best-practices for handling webhooks in your application.
203+
204+
```ruby
205+
require 'sinatra'
206+
require 'seam'
207+
208+
webhook = Seam::Webhook.new(ENV['SEAM_WEBHOOK_SECRET'])
209+
210+
post '/webhook' do
211+
begin
212+
data = webhook.verify(request.body.read, request.env)
213+
rescue StandardError
214+
halt 400, 'Bad Request'
215+
end
216+
217+
begin
218+
store_event(data)
219+
rescue StandardError
220+
halt 500, 'Internal Server Error'
221+
end
222+
223+
204
224+
end
225+
226+
def store_event(data)
227+
puts data
228+
end
229+
```
230+
231+
### Advanced Usage
232+
233+
#### Setting the endpoint
234+
235+
Some contexts may need to override the API endpoint,
236+
e.g., testing or proxy setups.
237+
238+
Either pass the `endpoint` option to the constructor, or set the `SEAM_ENDPOINT` environment variable.
21239

22240
## Development and Testing
23241

0 commit comments

Comments
 (0)