The generated code samples are now idiomatic with no errors. This improves the developer experience by allowing developers to run the code samples without any error in their applications and to get started quickly.
Details
This release introduces significant improvements to the code samples generated for your API. We've made several updates to the code samples to ensure that they are idiomatic, easier to understand and production-ready. The new code samples are designed to enhance the developer experience that will allow them to grasp the intention of an endpoint as to what the parameters are and how they are being initialized. This will let them get started with your API quickly by skimming through the code samples.
What Has Changed?
To meet the standards, the updated code samples are passed through the linter i.e. rubocop (click here to see the configuration being used).
Following major improvements are now made part of the Ruby code samples:
- Simplified code samples to make them easier to read and understand with the help of properly indented code samples and inline-initialization of the parameters.
- Fixed linting issues in the updated code samples to ensure that they comply with the best practices in terms of coding styles and standards.
- Fixed bugs and errors in the updated code samples to ensure that they work as expected.
Parameter Initialization Section
Model Initialization
Made the model's field initialization in-line which ensures that the object structure is intact, enhancing the code readability.
- Before
- After
model = Employee.new
model.name = 'John'
model.hired_at = DateTimeHelper.from_rfc1123('Mon, 15 Jun 2009 20:45:30 GMT')
model.boss = Person.new
model.boss.name = 'Doe'
model.boss.age = 40
model = Employee.new(
Person.new(
40,
'Doe'
),
DateTimeHelper.from_rfc1123('Mon, 15 Jun 2009 20:45:30 GMT'),
'John'
)
Array Initialization
Updated the array initialization step to multi-line to enhance the code readability.
Example 1
- Before
- After
string_array = ['Bob', 'Alice', 'John', 'Doe']
string_array = [
'Bob',
'Alice',
'John',
'Doe'
]
Example 2
- Before
- After
number_array_of_map = [{"key0":215,"key1":216}, {"key0":217,"key1":218}]
number_array_of_map = [
{
'key0': 215,
'key1': 216,
},
{
'key0': 217,
'key1': 218
}
]
Map Initialization
Updated the map initialization step to multi-line to enhance the code readability.
- Before
- After
number_map = {'key0' => 110, 'key1' => 111, 'key2' => 112 }
number_map = {
'key0': 110,
'key1': 111,
'key2': 112
}
Multi-Dimensional Arrays Initialization
Updated the n-dimensional array initialization step to multi-line to enhance the code readability.
- Before
- After
string_2d_array = [['Bob', 'Alice'],['John', 'Doe']]
string_2d_array = [
[
'Bob',
'Alice'
],
[
'John',
'Doe'
]
]
Client Initialization Section
Without Configurations
Addressed the styling of a client initialization without any configuration parameter.
- Before
- After
client = Tester::Client.new(
)
client = Tester::Client.new
With Configurations
Addressed the trailing comma after the last configuration parameter when initializing the client.
- Before
- After
client = Tester::Client.new(
port: '8000',
suites: SuiteCode::HEARTS,
)
client = Tester::Client.new(
port: '8000',
suites: SuiteCode::HEARTS
)
API Call Section
Without Parameters
Addressed the styling of an API call method without any parameter.
- Before
- After
response_types_controller = client.response_types
begin
result = response_types_controller.get_data()
rescue APIException => ex
puts "Caught APIException: #{ex.message}"
end
begin
result = client.response_types.get_data
puts result
rescue APIException => e
puts "Caught APIException: #{e.message}"
end
With Parameters
Addressed the styling of an API call method with parameters and made it multi-line in case if the parameters count is more than 1 to have a better readability of an endpoint call.
- Before
- After
response_types_controller = client.response_types
begin
result = response_types_controller.send_data(param1, param2, param3)
rescue APIException => ex
puts "Caught APIException: #{ex.message}"
end
begin
result = client.response_types.send_data(
param1,
param2,
param3
)
puts result
rescue APIException => e
puts "Caught APIException: #{e.message}"
end
A Comparison of Complete Example Code Sample
The following code shows the complete comparison between the code sample generation for initializing an employee model as an input parameter for ResponseTypesController Send endpoint.
Employee Model
"employee_model": {
"name": "John",
"hiredAt": "Mon, 15 Jun 2009 20:45:30 GMT",
"salary": null,
"boss": {
"name": "Doe",
"age": 40
}
}
- Before
- After
require 'tester'
include Tester
client = Tester::Client.new(
suites: SuiteCode::HEARTS,
)
response_types_controller = client.response_types
model = Employee.new
model.name = 'John'
model.hired_at = DateTimeHelper.from_rfc1123('Mon, 15 Jun 2009 20:45:30 GMT')
model.boss = Person.new
model.boss.name = 'Doe'
model.boss.age = 40
begin
result = response_types_controller.send(model)
rescue APIException => ex
puts "Caught APIException: #{ex.message}"
end
require 'tester'
include Tester
model = Employee.new(
Person.new(
40,
'Doe'
),
DateTimeHelper.from_rfc1123('Mon, 15 Jun 2009 20:45:30 GMT'),
'John',
nil
)
client = Tester::Client.new(
suites: SuiteCode::HEARTS
)
begin
result = client.response_types.send(model)
puts result
rescue APIException => e
puts "Caught APIException: #{e.message}"
end
Bugs Fixed
- Fixed an issue with OAuth 2.0 token initialization.
- Fixed an issue with parameter initialization of a map containing array types.