Skip to content

Commit

Permalink
feat: Add ruby request/response code samples (#42)
Browse files Browse the repository at this point in the history
* Add createRubyRequest and createRubyResponse

* Update test snapshots

* ci: Generate code

* ci: Format code

* Update default response object memory address

* ci: Generate code

* ci: Generate code

---------

Co-authored-by: Seam Bot <devops@getseam.com>
  • Loading branch information
andrii-balitskyi and seambot authored Aug 2, 2024
1 parent 914d381 commit 391a97d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
42 changes: 42 additions & 0 deletions src/lib/code-sample/ruby.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { pascalCase, snakeCase } from 'change-case'

import type { CodeSampleDefinition, Context } from './schema.js'

export const createRubyRequest = (
{ request }: CodeSampleDefinition,
_context: Context,
): string => {
const parts = request.path.split('/')
const params = Object.entries(request.parameters)
.map(([key, value]) => `${snakeCase(key)}: ${JSON.stringify(value)}`)
.join(', ')

return `seam${parts.map((p) => snakeCase(p)).join('.')}(${params})`
}

export const createRubyResponse = (
{ response, title }: CodeSampleDefinition,
context: Context,
): string => {
const { endpoint } = context

if (endpoint.response.responseType === 'void') return 'nil'

const { responseKey } = endpoint.response
const responseValue = response?.body?.[responseKey]

if (responseValue == null) {
throw new Error(`Missing ${responseKey} for '${title}'`)
}

const responseRubyClassName = pascalCase(responseKey)
const responseRubyParams = Object.entries(responseValue)
.map(([paramKey, paramValue]) => {
const formattedValue =
paramValue === null ? 'nil' : JSON.stringify(paramValue)
return ` ${snakeCase(paramKey)}=${formattedValue}`
})
.join('\n')

return `<Seam::${responseRubyClassName}:0x00000\n${responseRubyParams}>`
}
8 changes: 7 additions & 1 deletion src/lib/code-sample/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './javascript.js'
import { createPhpRequest, createPhpResponse } from './php.js'
import { createPythonRequest, createPythonResponse } from './python.js'
import { createRubyRequest, createRubyResponse } from './ruby.js'

export const CodeSampleDefinitionSchema = z.object({
title: z.string().trim().min(1),
Expand Down Expand Up @@ -36,7 +37,7 @@ export type CodeSampleDefinition = z.output<typeof CodeSampleDefinitionSchema>

const CodeSampleSchema = CodeSampleDefinitionSchema.extend({
code: z.record(
z.enum(['javascript', 'python', 'php']),
z.enum(['javascript', 'python', 'php', 'ruby']),
z.object({
title: z.string().min(1),
request: z.string(),
Expand Down Expand Up @@ -68,6 +69,11 @@ export const createCodeSample = (
request: createPythonRequest(codeSampleDefinition, context),
response: createPythonResponse(codeSampleDefinition, context),
},
ruby: {
title: 'Ruby',
request: createRubyRequest(codeSampleDefinition, context),
response: createRubyResponse(codeSampleDefinition, context),
},
php: {
title: 'PHP',
request: createPhpRequest(codeSampleDefinition, context),
Expand Down
14 changes: 14 additions & 0 deletions test/snapshots/blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ Generated by [AVA](https://avajs.dev).
response: 'Foo(foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33", name="Best foo")',
title: 'Python',
},
ruby: {
request: 'seam.foos.get(foo_id: "8d7e0b3a-b889-49a7-9164-4b71a0506a33")',
response: `<Seam::Foo:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo">`,
title: 'Ruby',
},
},
description: 'This is the way to get a foo',
request: {
Expand Down Expand Up @@ -134,6 +141,13 @@ Generated by [AVA](https://avajs.dev).
response: 'Foo(foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33", name="Best foo")',
title: 'Python',
},
ruby: {
request: 'seam.foos.get(foo_id: "8d7e0b3a-b889-49a7-9164-4b71a0506a33")',
response: `<Seam::Foo:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo">`,
title: 'Ruby',
},
},
description: 'This is the way to get a foo',
request: {
Expand Down
Binary file modified test/snapshots/blueprint.test.ts.snap
Binary file not shown.

0 comments on commit 391a97d

Please sign in to comment.