Growsurf Ruby API library
The Growsurf Ruby library provides convenient access to the Growsurf REST API from any Ruby 3.2.0+ application. It ships with comprehensive types & docstrings in Yard, RBS, and RBI – see below for usage with Sorbet. The standard library's net/http is used as the HTTP transport, with connection pooling via the connection_pool gem.
This library was originally generated with Stainless and is now maintained by GrowSurf.
Documentation
Documentation for releases of this gem can be found on RubyDoc.
See the GrowSurf REST API reference for endpoint documentation.
Installation
To use this gem, install via Bundler by adding the following to your application's Gemfile:
gem "growsurf-ruby", "~> 1.10.0"Usage
require "bundler/setup"
require "growsurf_ruby"
growsurf = GrowsurfRuby::Client.new(
api_key: ENV["GROWSURF_API_KEY"] # This is the default and can be omitted
)
campaigns = growsurf.campaign.list
puts(campaigns.campaigns)Handling errors
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of GrowsurfRuby::Errors::APIError will be thrown:
begin
campaign = growsurf.campaign.list
rescue GrowsurfRuby::Errors::APIConnectionError => e
puts("The server could not be reached")
puts(e.cause) # an underlying Exception, likely raised within `net/http`
rescue GrowsurfRuby::Errors::RateLimitError => e
puts("A 429 status code was received; we should back off a bit.")
rescue GrowsurfRuby::Errors::APIStatusError => e
puts("Another non-200-range status code was received")
puts(e.status)
endError codes are as follows:
| Cause | Error Type |
|---|---|
| HTTP 400 | BadRequestError |
| HTTP 401 | AuthenticationError |
| HTTP 403 | PermissionDeniedError |
| HTTP 404 | NotFoundError |
| HTTP 409 | ConflictError |
| HTTP 422 | UnprocessableEntityError |
| HTTP 429 | RateLimitError |
| HTTP >= 500 | InternalServerError |
| Other HTTP error | APIStatusError |
| Timeout | APITimeoutError |
| Network error | APIConnectionError |
Retries
Retryable requests are automatically retried 2 times by default, with a short exponential backoff.
Automatic retries apply only to GET and HEAD requests, plus POST /api-key/rotate. For those requests, the library retries connection errors, timeouts, HTTP 408, HTTP 409, HTTP 429, and HTTP 5xx responses. Other mutations are never retried automatically.
You can use the max_retries option to configure or disable this:
# Configure the default for all requests:
growsurf = GrowsurfRuby::Client.new(
max_retries: 0 # default is 2
)
# Or, configure per-request:
growsurf.campaign.list(request_options: {max_retries: 5})Timeouts
By default, requests will time out after 60 seconds. You can use the timeout option to configure or disable this:
# Configure the default for all requests:
growsurf = GrowsurfRuby::Client.new(
timeout: nil # default is 60
)
# Or, configure per-request:
growsurf.campaign.list(request_options: {timeout: 5})On timeout, GrowsurfRuby::Errors::APITimeoutError is raised.
A timed-out request is retried only when it meets the retry rules above.
Advanced concepts
BaseModel
All parameter and response objects inherit from GrowsurfRuby::Internal::Type::BaseModel, which provides several conveniences, including:
-
All fields, including unknown ones, are accessible with
obj[:prop]syntax, and can be destructured withobj => {prop: prop}or pattern-matching syntax. -
Structural equivalence for equality; if two API calls return the same values, comparing the responses with == will return true.
-
Both instances and the classes themselves can be pretty-printed.
-
Helpers such as
#to_h,#deep_to_h,#to_json, and#to_yaml. -
Normalized HTTP response headers through
#response_headers. For example,response.response_headers["ratelimit"]exposes the current rate-limit state.
Making custom or undocumented requests
Undocumented properties
You can send undocumented parameters to any endpoint, and read undocumented response properties, like so:
Note: the extra_ parameters of the same name overrides the documented parameters.
campaigns =
growsurf.campaign.list(
request_options: {
extra_query: {my_query_parameter: value},
extra_body: {my_body_parameter: value},
extra_headers: {"my-header": value}
}
)
puts(campaigns[:my_undocumented_property])Undocumented request params
If you want to explicitly send an extra param, you can do so with the extra_query, extra_body, and extra_headers under the request_options: parameter when making a request, as seen in the examples above.
Undocumented endpoints
To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests using client.request, like so:
response = client.request(
method: :post,
path: '/undocumented/endpoint',
query: {"dog": "woof"},
headers: {"useful-header": "interesting-value"},
body: {"hello": "world"}
)Concurrency & connection pooling
The GrowsurfRuby::Client instances are threadsafe, but are only are fork-safe when there are no in-flight HTTP requests.
Each instance of GrowsurfRuby::Client has its own HTTP connection pool with a default size of 99. As such, we recommend instantiating the client once per application in most settings.
When all available connections from the pool are checked out, requests wait for a new connection to become available, with queue time counting towards the request timeout.
Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
Sorbet
This library provides comprehensive RBI definitions, and has no dependency on sorbet-runtime.
You can provide typesafe request parameters like so:
growsurf.campaign.listOr, equivalently:
# Hashes work, but are not typesafe:
growsurf.campaign.list
# You can also splat a full Params class:
params = GrowsurfRuby::CampaignListParams.new
growsurf.campaign.list(**params)Enums
Since this library does not depend on sorbet-runtime, it cannot provide T::Enum instances. Instead, we provide "tagged symbols" instead, which is always a primitive at runtime:
# :CREDIT_PENDING
puts(GrowsurfRuby::Campaign::Create::ReferralStatus::CREDIT_PENDING)
# Revealed type: `T.all(GrowsurfRuby::Campaign::Create::ReferralStatus, Symbol)`
T.reveal_type(GrowsurfRuby::Campaign::Create::ReferralStatus::CREDIT_PENDING)Enum parameters have a "relaxed" type, so you can either pass in enum constants or their literal value:
# Using the enum constants preserves the tagged type information:
growsurf.campaign.create_mobile_participant_token(
referral_status: GrowsurfRuby::Campaign::Create::ReferralStatus::CREDIT_PENDING,
# …
)
# Literal values are also permissible:
growsurf.campaign.create_mobile_participant_token(
referral_status: :CREDIT_PENDING,
# …
)Versioning
This package follows SemVer conventions. Breaking API changes use a new major version.
This package considers improvements to the (non-runtime) *.rbi and *.rbs type definitions to be non-breaking changes.
Requirements
Ruby 3.2.0 or higher.