diff --git a/src/lib/code-sample/ruby.ts b/src/lib/code-sample/ruby.ts index a35c73a..be22435 100644 --- a/src/lib/code-sample/ruby.ts +++ b/src/lib/code-sample/ruby.ts @@ -1,5 +1,7 @@ import { pascalCase, snakeCase } from 'change-case' +import type { Json, NonNullJson } from 'lib/json.js' + import type { CodeSampleDefinition, Context } from './schema.js' export const createRubyRequest = ( @@ -8,12 +10,15 @@ export const createRubyRequest = ( ): string => { const parts = request.path.split('/') const params = Object.entries(request.parameters) - .map(([key, value]) => `${snakeCase(key)}: ${JSON.stringify(value)}`) + .map(([key, value]) => `${snakeCase(key)}: ${formatRubyValue(value)}`) .join(', ') return `seam${parts.map((p) => snakeCase(p)).join('.')}(${params})` } +const formatRubyValue = (value: Json): string => + value == null ? 'nil' : JSON.stringify(value) + export const createRubyResponse = ( { response, title }: CodeSampleDefinition, context: Context, @@ -30,13 +35,41 @@ export const createRubyResponse = ( } const responseRubyClassName = pascalCase(responseKey) - const responseRubyParams = Object.entries(responseValue) - .map(([paramKey, paramValue]) => { - const formattedValue = - paramValue === null ? 'nil' : JSON.stringify(paramValue) - return ` ${snakeCase(paramKey)}=${formattedValue}` + + return Array.isArray(responseValue) + ? formatRubyArrayResponse(responseValue, responseRubyClassName, title) + : formatRubyResponse(responseValue, responseRubyClassName) +} + +const formatRubyArrayResponse = ( + responseArray: Json[], + responseRubyClassName: string, + title: string, +): string => { + const formattedItems = responseArray + .map((item) => { + if (item == null) { + throw new Error(`Null value in response array for '${title}'`) + } + return formatRubyResponse(item, responseRubyClassName) }) - .join('\n') + .join(',\n') + + return `[${formattedItems}]` +} - return `` +const formatRubyResponse = ( + responseParams: NonNullJson, + responseRubyClassName: string, +): string => { + const params = formatRubyArgs(responseParams) + return `` } + +const formatRubyArgs = (jsonParams: NonNullJson): string => + Object.entries(jsonParams as Record) + .map( + ([paramKey, paramValue]) => + `${snakeCase(paramKey)}=${formatRubyValue(paramValue)}`, + ) + .join('\n') diff --git a/test/snapshots/blueprint.test.ts.md b/test/snapshots/blueprint.test.ts.md index d99fd95..5b6a011 100644 --- a/test/snapshots/blueprint.test.ts.md +++ b/test/snapshots/blueprint.test.ts.md @@ -77,8 +77,8 @@ Generated by [AVA](https://avajs.dev). ruby: { request: 'seam.foos.get(foo_id: "8d7e0b3a-b889-49a7-9164-4b71a0506a33")', response: ``, + foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊ + name="Best foo">`, title: 'Ruby', }, }, @@ -144,8 +144,8 @@ Generated by [AVA](https://avajs.dev). ruby: { request: 'seam.foos.get(foo_id: "8d7e0b3a-b889-49a7-9164-4b71a0506a33")', response: ``, + foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊ + name="Best foo">`, title: 'Ruby', }, }, @@ -210,8 +210,9 @@ Generated by [AVA](https://avajs.dev). }, ruby: { request: 'seam.foos.list()', - response: ``, + response: `[]`, title: 'Ruby', }, }, @@ -276,8 +277,9 @@ Generated by [AVA](https://avajs.dev). }, ruby: { request: 'seam.foos.list()', - response: ``, + response: `[]`, title: 'Ruby', }, }, diff --git a/test/snapshots/blueprint.test.ts.snap b/test/snapshots/blueprint.test.ts.snap index b721216..2bced83 100644 Binary files a/test/snapshots/blueprint.test.ts.snap and b/test/snapshots/blueprint.test.ts.snap differ