Skip to content

Commit

Permalink
fix: Correctly handle array responses for Ruby code samples (#54)
Browse files Browse the repository at this point in the history
* Correctly handle null param values in ruby code samples

* Update test snapshot

* ci: Format code

* Add formatRubyValue

* Add formatRubyArrayResponse for better readability

* ci: Format code

* Fix eslint error

* ci: Generate code

* Move formatRubyArgs to the bottom of the file

---------

Co-authored-by: Seam Bot <devops@getseam.com>
  • Loading branch information
andrii-balitskyi and seambot authored Aug 7, 2024
1 parent a1473ef commit 9287e74
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
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.

0 comments on commit 9287e74

Please sign in to comment.