Skip to content

Commit

Permalink
fix: Render ruby response as literal (#71)
Browse files Browse the repository at this point in the history
* fix: Do not render class in ruby response

* ci: Generate code

* fix: Ruby hash map format

* ci: Format code

* ci: Generate code

* Wrap ruby hash with curly braces

* ci: Generate code

* Remove responseRubyClassName

* Fix curly brace location

* ci: Generate code

* ci: Format code

* Use comma not newline

* ci: Generate code

---------

Co-authored-by: Seam Bot <[email protected]>
  • Loading branch information
razor-x and seambot authored Aug 30, 2024
1 parent e35aae9 commit d4556aa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 53 deletions.
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.

0 comments on commit d4556aa

Please sign in to comment.