Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ workspaces = seam.workspaces.list

### Webhooks

The Seam API implements webhooks using [Svix](https://www.svix.com).This SDK exports a thin wrapper `Seam::Webhook` around the svix package.
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.
Known event types load as `Seam::Resources::SeamEvent` subclasses with only that event's accessors; unknown types remain generic `SeamEvent` instances for forward compatibility.

> [!TIP]
> This example is for [Sinatra](https://sinatrarb.com/), see the [Svix docs for more examples in specific frameworks](https://docs.svix.com/receiving/verifying-payloads/how).
Expand All @@ -402,23 +403,26 @@ post "/webhook" do
"svix-signature" => request.env["HTTP_SVIX_SIGNATURE"],
"svix-timestamp" => request.env["HTTP_SVIX_TIMESTAMP"]
}
data = webhook.verify(request.body.read, headers)
event = webhook.verify(request.body.read, headers)
rescue Seam::WebhookVerificationError
halt 400, "Bad Request"
end

begin
store_event(data)
case event.event_type
when "access_code.created"
puts "Access code created: #{event.access_code_id}"
when "device.connected"
puts "Device connected: #{event.device_id}"
else
puts event
end
rescue
halt 500, "Internal Server Error"
end

204
end

def store_event(data)
puts data
end
```

### Advanced Usage
Expand Down
32 changes: 25 additions & 7 deletions codegen/layouts/partials/resource-class.hbs
Original file line number Diff line number Diff line change
@@ -1,40 +1,58 @@
{{indent}}class {{className}} < BaseResource
{{#if description}}
{{{rubyDoc description indentation}}}
{{/if}}
{{indent}}class {{className}} < {{superclass}}
{{#each nestedClasses}}
{{> resource-class}}
{{/each}}
{{#each variants}}
{{> resource-class}}
{{/each}}
{{#each resourceAccessors}}
{{#if description}}
{{{rubyDoc description ../docIndent}}}
{{/if}}
{{../bodyIndent}}# @return [{{rubyResourceType this}}]
{{../bodyIndent}}# @return [{{rubyResourceType this}}]{{{rubyEnumValuesDoc this ../docIndent}}}
{{../bodyIndent}}resource_accessor :{{name}}, {{className}}
{{/each}}
{{#each resourceListAccessors}}
{{#if description}}
{{{rubyDoc description ../docIndent}}}
{{/if}}
{{../bodyIndent}}# @return [{{rubyResourceType this}}]
{{../bodyIndent}}# @return [{{rubyResourceType this}}]{{{rubyEnumValuesDoc this ../docIndent}}}
{{../bodyIndent}}resource_list_accessor :{{name}}, {{className}}
{{/each}}
{{#each accessors}}
{{#if description}}
{{{rubyDoc description ../docIndent}}}
{{/if}}
{{../bodyIndent}}# @return [{{rubyPropertyType this}}]
{{../bodyIndent}}# @return [{{rubyPropertyType this}}]{{{rubyEnumValuesDoc this ../docIndent}}}
{{#if isDeprecated}}
{{{rubyDeprecatedDoc this ../docIndent}}}
{{/if}}
{{../bodyIndent}}attr_accessor :{{name}}
{{#if isAliased}}
{{../bodyIndent}}aliased_accessor :{{accessorName}}, from: :{{name}}
{{else}}
{{../bodyIndent}}attr_accessor :{{accessorName}}
{{/if}}
{{/each}}
{{#each dateAccessors}}
{{#if description}}
{{{rubyDoc description ../docIndent}}}
{{/if}}
{{../bodyIndent}}# @return [{{rubyPropertyType this}}]
{{../bodyIndent}}# @return [{{rubyPropertyType this}}]{{{rubyEnumValuesDoc this ../docIndent}}}
{{#if isDeprecated}}
{{{rubyDeprecatedDoc this ../docIndent}}}
{{/if}}
{{../bodyIndent}}date_accessor :{{name}}
{{../bodyIndent}}date_accessor :{{accessorName}}
{{/each}}
{{#if discriminator}}

{{bodyIndent}}discriminated_by :{{discriminator}}, {
{{#each variants}}
{{../bodyIndent}} {{{rubyString discriminatorValue}}} => {{className}},
{{/each}}
{{bodyIndent}}{{identity "}"}}.freeze
{{/if}}
{{indent}}end

27 changes: 21 additions & 6 deletions codegen/layouts/resource.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,56 @@ module Seam
{{#each nestedClasses}}
{{> resource-class}}
{{/each}}
{{#each variants}}
{{> resource-class}}
{{/each}}
{{#each resourceAccessors}}
{{#if description}}
{{{rubyDoc description 6}}}
{{/if}}
# @return [{{rubyResourceType this}}]
# @return [{{rubyResourceType this}}]{{{rubyEnumValuesDoc this 6}}}
resource_accessor :{{name}}, {{className}}
{{/each}}
{{#each resourceListAccessors}}
{{#if description}}
{{{rubyDoc description 6}}}
{{/if}}
# @return [{{rubyResourceType this}}]
# @return [{{rubyResourceType this}}]{{{rubyEnumValuesDoc this 6}}}
resource_list_accessor :{{name}}, {{className}}
{{/each}}
{{#each accessors}}
{{#if description}}
{{{rubyDoc description 6}}}
{{/if}}
# @return [{{rubyPropertyType this}}]
# @return [{{rubyPropertyType this}}]{{{rubyEnumValuesDoc this 6}}}
{{#if isDeprecated}}
{{{rubyDeprecatedDoc this 6}}}
{{/if}}
attr_accessor :{{name}}
{{#if isAliased}}
aliased_accessor :{{accessorName}}, from: :{{name}}
{{else}}
attr_accessor :{{accessorName}}
{{/if}}
{{/each}}
{{#each dateAccessors}}

{{#if description}}
{{{rubyDoc description 6}}}
{{/if}}
# @return [{{rubyPropertyType this}}]
# @return [{{rubyPropertyType this}}]{{{rubyEnumValuesDoc this 6}}}
{{#if isDeprecated}}
{{{rubyDeprecatedDoc this 6}}}
{{/if}}
date_accessor :{{name}}
date_accessor :{{accessorName}}
{{/each}}
{{#if discriminator}}

discriminated_by :{{discriminator}}, {
{{#each variants}}
{{{rubyString discriminatorValue}}} => {{className}},
{{/each}}
}.freeze
{{/if}}
end
end
end
20 changes: 20 additions & 0 deletions codegen/lib/handlebars-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ export const rubyDeprecatedDoc = (
)
: ''

export const rubyString = (value: string): string => JSON.stringify(value)

export const rubyEnumValuesDoc = (
property: Property,
indentation: number,
): string => {
const values =
property.format === 'enum'
? property.values
: property.format === 'list' && property.itemFormat === 'enum'
? property.itemEnumValues
: []
return values.length === 0
? ''
: `\n${comment(
['Known values:', ...values.map(({ name }) => `- \`${name}\``)],
indentation,
)}`
}

const nullable = (
type: string,
value: { isOptional: boolean; isNullable: boolean },
Expand Down
Loading
Loading