Ruby SDK

Reference documentation for Jahuty's Ruby SDK.


This library requires Ruby 2.6+.

Add this line to your application’s Gemfile and then run bundle install:

gem 'jahuty', '~> 3.1'


Instantiate a client with your API key and call snippets.render:

jahuty = api_key: 'YOUR_API_KEY'

puts jahuty.snippets.render YOUR_SNIPPET_ID

You can also access the render’s content with to_s or content:

jahuty = api_key: 'YOUR_API_KEY'

render = jahuty.snippets.render YOUR_SNIPPET_ID

a = render.to_s

b = render.content

a == b  # returns true

In an HTML view:

<%- jahuty = api_key: 'YOUR_API_KEY' -%>
<!doctype html>
    <title>Awesome example</title>
    <%== jahuty.snippets.render YOUR_SNIPPET_ID %>


You can pass parameters into your snippet using the params option:

jahuty = api_key: 'YOUR_API_KEY'

jahuty.snippets.render YOUR_SNIPPET_ID, params: { foo: "bar" }

The parameters above would be equivalent to assigning the variable below in your snippet:

{% assign foo = "bar" %}


You can use caching to control how frequently this library requests the latest content from Jahuty’s API.

  • When content is in development (i.e., frequently changing and low traffic), you can use the default in-memory store to view content changes instantaneously with slower response times.
  • When content is in production (i.e., more stable and high traffic), you can use persistent caching to update content less frequently and improve your application’s average response time.

Caching in memory (default)

By default, this library uses an in-memory cache to avoid requesting the same render more than once during the same request lifecycle. For example:

jahuty = api_key: 'YOUR_API_KEY'

# This call will send a synchronous API request; cache the result in memory;
# and, return the result to the caller.
render1 = jahuty.snippets.render YOUR_SNIPPET_ID

# This call skips sending an API request and uses the cached value instead.
render2 = jahuty.snippets.render YOUR_SNIPPET_ID

The in-memory cache only persists for the duration of the original request, however. At the end of the request’s lifecycle, the cache will be discarded. To store renders across requests, you need a persistent cache.

Caching persistently

A persistent cache allows renders to be cached across multiple requests. This reduces the number of synchronous network requests to Jahuty’s API and improves your application’s average response time.

To configure Jahuty to use your persistent cache, pass a cache implementation to the client via the cache configuration option:

jahuty = new
  api_key: 'YOUR_API_KEY',
  cache: cache

The persistent cache implementation you choose and configure is up to you. There are many libraries available, and most frameworks provide their own. At this time, we support any object which responds to get(key) or read(key) and set(key, value, expires_in:) or write(key, value, expires_in:) including ActiveSupport::Cache::Store.


There are three methods for configuring this library’s :expires_in, the amount of time between when a render is stored and when it’s considered stale. From lowest-to-highest precedence, the methods are:

  1. configuring your caching implementation,
  2. configuring this library’s default :expires_in, and
  3. configuring a render’s :expires_in.

Configuring your caching implementation

You can usually configure your caching implementation with a default :expires_in. If no other :expires_in is configured, this library will defer to the caching implementation’s default :expires_in.

Configuring the library

You can configure a default :expires_in for all of this library’s renders by passing an integer number of seconds via the client’s :expires_in configuration option:

jahuty =
  api_key: 'YOUR_API_KEY',
  cache: cache,
  expires_in: 60  # Cache all renders for sixty seconds

If this library’s default :expires_in is set, it will take precedence over the default :expires_is of the caching implementation.

Configuring a render

You can configure a single render’s :expires_in by passing an integer number of seconds via its :expires_in configuration option:

# Default to the caching implementation's :expires_in for all renders.
jahuty = api_key: 'YOUR_API_KEY', cache: cache

# Except, cache this render for 60 seconds.
render = jahuty.snippets.render 1, expires_in: 60

If a render’s :expires_in is set, it will take precedence over the library’s default :expires_in and the caching implementation’s :expires_in.

Disabling caching

You can disable caching, even the default in-memory caching, by passing an :expires_in of zero (0) or a negative integer (e.g., -1) via any of the methods described above. For example:

# Disable all caching.
jahuty1 = api_key: 'YOUR_API_KEY', expires_in: 0

# Disable caching for this render.
jahuty2 = api_key: 'YOUR_API_KEY', expires_in: 60
jahuty2.snippets.render 1, expires_in: 0


If an error occurs with Jahuty’s API, a Jahuty::Exception::Error exception will be raised:

  jahuty = api_key: 'YOUR_API_KEY'
  jahuty.snippets.render YOUR_SNIPPET_ID
rescue Jahuty::Exception::Error => e
  # The API returned an error. See e.problem for details.
  puts e.problem.type    # a URL to more information
  puts e.problem.status  # the status code
  puts e.problem.detail  # a description of the error


If you’d like to see the SDK in action, see jahuty/jahuty-ruby-example.


Bug reports and pull requests are welcome on GitHub.

Find a typo? Something is wrong in this documentation? Fork and edit it!