-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ruby request/response code samples (#42)
* Add createRubyRequest and createRubyResponse * Update test snapshots * ci: Generate code * ci: Format code * Update default response object memory address * ci: Generate code * ci: Generate code --------- Co-authored-by: Seam Bot <devops@getseam.com>
- Loading branch information
1 parent
914d381
commit 391a97d
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { pascalCase, snakeCase } from 'change-case' | ||
|
||
import type { CodeSampleDefinition, Context } from './schema.js' | ||
|
||
export const createRubyRequest = ( | ||
{ request }: CodeSampleDefinition, | ||
_context: Context, | ||
): string => { | ||
const parts = request.path.split('/') | ||
const params = Object.entries(request.parameters) | ||
.map(([key, value]) => `${snakeCase(key)}: ${JSON.stringify(value)}`) | ||
.join(', ') | ||
|
||
return `seam${parts.map((p) => snakeCase(p)).join('.')}(${params})` | ||
} | ||
|
||
export const createRubyResponse = ( | ||
{ response, title }: CodeSampleDefinition, | ||
context: Context, | ||
): string => { | ||
const { endpoint } = context | ||
|
||
if (endpoint.response.responseType === 'void') return 'nil' | ||
|
||
const { responseKey } = endpoint.response | ||
const responseValue = response?.body?.[responseKey] | ||
|
||
if (responseValue == null) { | ||
throw new Error(`Missing ${responseKey} for '${title}'`) | ||
} | ||
|
||
const responseRubyClassName = pascalCase(responseKey) | ||
const responseRubyParams = Object.entries(responseValue) | ||
.map(([paramKey, paramValue]) => { | ||
const formattedValue = | ||
paramValue === null ? 'nil' : JSON.stringify(paramValue) | ||
return ` ${snakeCase(paramKey)}=${formattedValue}` | ||
}) | ||
.join('\n') | ||
|
||
return `<Seam::${responseRubyClassName}:0x00000\n${responseRubyParams}>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.