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: Render ruby response as literal #71

Merged
merged 13 commits into from
Aug 30, 2024
31 changes: 10 additions & 21 deletions src/lib/code-sample/ruby.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pascalCase, snakeCase } from 'change-case'
import { snakeCase } from 'change-case'

import type { Json, NonNullJson } from 'lib/json.js'

Expand Down Expand Up @@ -34,42 +34,31 @@ export const createRubyResponse = (
throw new Error(`Missing ${responseKey} for '${title}'`)
}

const responseRubyClassName = pascalCase(responseKey)

return Array.isArray(responseValue)
? formatRubyArrayResponse(responseValue, responseRubyClassName, title)
: formatRubyResponse(responseValue, responseRubyClassName)
? formatRubyArrayResponse(responseValue, title)
: formatRubyResponse(responseValue)
}

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)
return formatRubyResponse(item)
})
.join(',\n')

return `[${formattedItems}]`
}

const formatRubyResponse = (
responseParams: NonNullJson,
responseRubyClassName: string,
): string => {
const params = formatRubyArgs(responseParams)
return `<Seam::${responseRubyClassName}:0x00000\n${params}>`
const formatRubyResponse = (responseParams: NonNullJson): string => {
const values = Object.entries(responseParams as Record<string, Json>).map(
([paramKey, paramValue]) =>
`"${snakeCase(paramKey)}" => ${formatRubyValue(paramValue)}`,
)
return `{${values.join(',')}}`
}

const formatRubyArgs = (jsonParams: NonNullJson): string =>
Object.entries(jsonParams as Record<string, Json>)
.map(
([paramKey, paramValue]) =>
`${snakeCase(paramKey)}=${formatRubyValue(paramValue)}`,
)
.join('\n')
40 changes: 8 additions & 32 deletions test/snapshots/blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ Generated by [AVA](https://avajs.dev).
ruby: {
request: 'seam.foos.get(foo_id: "8d7e0b3a-b889-49a7-9164-4b71a0506a33")',
request_syntax: 'ruby',
response: `<Seam::Foo:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo"␊
nullable_property=nil>`,
response: '{"foo_id" => "8d7e0b3a-b889-49a7-9164-4b71a0506a33","name" => "Best foo","nullable_property" => nil}',
response_syntax: 'ruby',
title: 'Ruby',
},
Expand Down Expand Up @@ -178,10 +175,7 @@ Generated by [AVA](https://avajs.dev).
ruby: {
request: 'seam.foos.get(foo_id: "8d7e0b3a-b889-49a7-9164-4b71a0506a33")',
request_syntax: 'ruby',
response: `<Seam::Foo:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo"␊
nullable_property=nil>`,
response: '{"foo_id" => "8d7e0b3a-b889-49a7-9164-4b71a0506a33","name" => "Best foo","nullable_property" => nil}',
response_syntax: 'ruby',
title: 'Ruby',
},
Expand Down Expand Up @@ -262,10 +256,7 @@ Generated by [AVA](https://avajs.dev).
ruby: {
request: 'seam.foos.list()',
request_syntax: 'ruby',
response: `[<Seam::Foos:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo"␊
nullable_property=nil>]`,
response: '[{"foo_id" => "8d7e0b3a-b889-49a7-9164-4b71a0506a33","name" => "Best foo","nullable_property" => nil}]',
response_syntax: 'ruby',
title: 'Ruby',
},
Expand Down Expand Up @@ -346,10 +337,7 @@ Generated by [AVA](https://avajs.dev).
ruby: {
request: 'seam.foos.list()',
request_syntax: 'ruby',
response: `[<Seam::Foos:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo"␊
nullable_property=nil>]`,
response: '[{"foo_id" => "8d7e0b3a-b889-49a7-9164-4b71a0506a33","name" => "Best foo","nullable_property" => nil}]',
response_syntax: 'ruby',
title: 'Ruby',
},
Expand Down Expand Up @@ -510,10 +498,7 @@ Generated by [AVA](https://avajs.dev).
seam.foos.get(foo_id: "8d7e0b3a-b889-49a7-9164-4b71a0506a33")`,
request_syntax: 'ruby',
response: `// ruby␊
<Seam::Foo:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo"␊
nullable_property=nil>`,
{"foo_id" => "8d7e0b3a-b889-49a7-9164-4b71a0506a33","name" => "Best foo","nullable_property" => nil}`,
response_syntax: 'ruby',
title: 'Ruby',
},
Expand Down Expand Up @@ -604,10 +589,7 @@ Generated by [AVA](https://avajs.dev).
seam.foos.get(foo_id: "8d7e0b3a-b889-49a7-9164-4b71a0506a33")`,
request_syntax: 'ruby',
response: `// ruby␊
<Seam::Foo:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo"␊
nullable_property=nil>`,
{"foo_id" => "8d7e0b3a-b889-49a7-9164-4b71a0506a33","name" => "Best foo","nullable_property" => nil}`,
response_syntax: 'ruby',
title: 'Ruby',
},
Expand Down Expand Up @@ -698,10 +680,7 @@ Generated by [AVA](https://avajs.dev).
seam.foos.list()`,
request_syntax: 'ruby',
response: `// ruby␊
[<Seam::Foos:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo"␊
nullable_property=nil>]`,
[{"foo_id" => "8d7e0b3a-b889-49a7-9164-4b71a0506a33","name" => "Best foo","nullable_property" => nil}]`,
response_syntax: 'ruby',
title: 'Ruby',
},
Expand Down Expand Up @@ -792,10 +771,7 @@ Generated by [AVA](https://avajs.dev).
seam.foos.list()`,
request_syntax: 'ruby',
response: `// ruby␊
[<Seam::Foos:0x00000␊
foo_id="8d7e0b3a-b889-49a7-9164-4b71a0506a33"␊
name="Best foo"␊
nullable_property=nil>]`,
[{"foo_id" => "8d7e0b3a-b889-49a7-9164-4b71a0506a33","name" => "Best foo","nullable_property" => nil}]`,
response_syntax: 'ruby',
title: 'Ruby',
},
Expand Down
Binary file modified test/snapshots/blueprint.test.ts.snap
Binary file not shown.