Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correctly handle array responses for Ruby code samples #54

Merged
merged 10 commits into from
Aug 7, 2024
49 changes: 41 additions & 8 deletions src/lib/code-sample/ruby.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand All @@ -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,
Expand All @@ -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 `<Seam::${responseRubyClassName}:0x00000\n${responseRubyParams}>`
const formatRubyResponse = (
responseParams: NonNullJson,
responseRubyClassName: string,
): string => {
const params = formatRubyArgs(responseParams)
return `<Seam::${responseRubyClassName}:0x00000\n${params}>`
}

const formatRubyArgs = (jsonParams: NonNullJson): string =>
Object.entries(jsonParams as Record<string, Json>)
.map(
([paramKey, paramValue]) =>
`${snakeCase(paramKey)}=${formatRubyValue(paramValue)}`,
)
.join('\n')
18 changes: 10 additions & 8 deletions test/snapshots/blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ Generated by [AVA](https://avajs.dev).
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">`,
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo">`,
title: 'Ruby',
},
},
Expand Down Expand Up @@ -144,8 +144,8 @@ Generated by [AVA](https://avajs.dev).
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">`,
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo">`,
title: 'Ruby',
},
},
Expand Down Expand Up @@ -210,8 +210,9 @@ Generated by [AVA](https://avajs.dev).
},
ruby: {
request: 'seam.foos.list()',
response: `<Seam::Foos:0x00000␊
0={"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo"}>`,
response: `[<Seam::Foos:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo">]`,
title: 'Ruby',
},
},
Expand Down Expand Up @@ -276,8 +277,9 @@ Generated by [AVA](https://avajs.dev).
},
ruby: {
request: 'seam.foos.list()',
response: `<Seam::Foos:0x00000␊
0={"foo_id":"8d7e0b3a-b889-49a7-9164-4b71a0506a33","name":"Best foo"}>`,
response: `[<Seam::Foos:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo">]`,
title: 'Ruby',
},
},
Expand Down
Binary file modified test/snapshots/blueprint.test.ts.snap
Binary file not shown.