Use Ruby SDKs generated in Postman
This guide provides instructions for using Ruby SDKs generated by Postman SDK Generator. It covers how to install the SDK into your project, run example code, and best practices for using the SDK effectively in your applications.
The Postman SDK Generator creates Ruby SDKs with models, service classes for API endpoints, authentication handling, error handling, and comprehensive documentation to help you integrate with your API quickly and efficiently. Models include attribute validation, serialization support, and clear error messages to enhance development experience.
SDK structure
Generated Ruby SDKs include the following key components:
- Main client —
lib/[sdk_name].rbfor SDK initialization and configuration. - Service layer — Organized in
lib/[sdk_name]/services/with individual service classes. - Data models — Well-structured classes in
lib/[sdk_name]/models/with validation and serialization. - Network infrastructure — Located in
lib/[sdk_name]/http/handling HTTP transport, configuration, and authentication.
The directory structure includes documentation files, example implementations, and gemspec configuration for Ruby ecosystem integration.
The following is an example of the typical structure of a generated Ruby SDK:
Ruby conventions and features
Generated Ruby models follow Ruby conventions and best practices, providing a natural Ruby development experience.
Serialization
Models serialize to API-compatible hashes using to_h, which applies field aliases and excludes unset attributes:
Ruby naming conventions
The SDK uses Ruby naming conventions (snake_case) for model attributes and service methods, and converts to API field names (camelCase) during serialization. This allows you to work with Ruby idioms while ensuring correct API communication.
Example usage
Each generated SDK includes an example/ directory with a working project demonstrating SDK usage.
The example includes the following features:
- SDK configuration and initialization
- API calls with Ruby conventions
- Error handling with custom exceptions
- Authentication setup and usage
Location
Run the example
Example code structure
example/example.rb
Install the Ruby SDK locally
You can install the generated SDK into your Ruby project using Bundler, RubyGems, or by adding it to your load path. Below are instructions for each method.
Install using Bundler with local path
To install the SDK using Bundler with a local path, do the following:
-
Add the SDK to your
Gemfile: -
Install dependencies:
-
Use the SDK in your code:
Install using Bundler with Git repository
To install the SDK directly from a Git repository, add the following to your Gemfile:
Publish to RubyGems
To share your generated Ruby SDK with the community, you can publish it to RubyGems. This allows other developers to easily install it using gem install. Follow the steps below to publish your SDK to RubyGems.
Publish your Ruby SDK to RubyGems
To publish your SDK to RubyGems, do the following:
-
If you haven’t done so, create a RubyGems account:
- Register at rubygems.org.
- Set up your API credentials:
-
Prepare your gem for publishing:
This creates
your-sdk-1.0.0.gemin the current directory. -
Validate the gem:
-
Publish to RubyGems:
-
Verify publication:
Publish updates
-
Update the version in
lib/your_sdk/version.rb: -
Update
CHANGELOG.mdwith changes. -
Build and publish:
Best practices for publishing Ruby SDKs
Use the following best practices when publishing your Ruby SDK to RubyGems:
- Use semantic versioning (
MAJOR.MINOR.PATCH). - Maintain a
CHANGELOG.mddocumenting changes. - Use YARD documentation comments for API documentation.
- Follow Ruby style guidelines (use RuboCop).
- Pin major versions of dependencies in your gemspec.
- Include metadata URLs in your gemspec for better discoverability.
- Test your gem installation in a clean environment before publishing.