Published on February 23, 2015
We are continuing the series of features of our partners contributions to the Contentful developer ecosystem. Today our guest author is Ed Jones, director and co-owner of Error, a Liverpool-based digital creative studio. Ed and his team have created two Ruby gems that simplify and quicken developing Ruby apps with Contentful.
At Error weâve developed websites, applications and interactive content for lots of interesting organisations. One thing weâve learned over the years is that building bespoke content management systems is difficult! Thatâs why weâre so pleased to be a Contentful partner, and let them take care of content management complexity.
Weâre always working to standardise our processes and workflow, so we donât have to keep reinventing the wheel for each client. We can develop faster; clients and partners get to see their project come together more quickly; support and training effort is reduced to a minimum.
As we develop a lot of projects in Ruby, I thought it would be nice to share with you a couple of gems weâve developed to simplify integration with Contentful.
Itâs great to abstract complexity away when youâre implementing similar projects several times. So weâve built a wrapper around Contentfulâs Ruby client, which weâve called ContentfulModel. You can include it in any Ruby project, and with a simple declaration in the model you can access remote attributes as if they were database-backed.
The Readme has most of the details to get you started, so I wonât repeat them all here. But hereâs an example of how you set up your Contentful connection, create a model, and call some methods:
This can be used independently, as part of a framework like Sinatra, or in a Rails app. The only dependencies are the Contentful Ruby client and the ActiveSupport gem for some string manipulation. Itâs ideal if youâre planning to develop something really lightweight but need the power of a content management system.
Of course you might well be planning to use the Contentful CMaaS as the backend for a Rails app. Thatâs how we use it most often. Weâve developed a ContentfulRails gem to help you integrate with a Rails app, and promptly get all the power of the Contentful backend.
Here is what ContentfulRails does:
Registers all your models so you get the right classes returned when calling them. (You need to do this manually without ContentfulRails).
Provides a mountable Rails Engine to receive webhook calls from Contentful when your content changes
Caches timestamps for Contentful content, so you can use normal âRussian Dollâ caching techniques without incurring heavy page load penalties.
Provides a view helper to parse Markdown
As with ContentfulModel, the gemâs Readme has most of what you need to get started. Since weâre using ContentfulModel as a dependency, youâll need to configure both gems in an initializer.
Everyone loves a fast website: thatâs why caching is so important for content delivery. While Contentfulâs CDN is pretty fast, the speed can always be improved by delivering from application server memory.
One of the issues with Rails normal view caching is that it relies on a database hit to determine the timestamp of the model. Thatâs fine if itâs local â most likely, the query will be cached in your database server and, provided the data hasnât changed, thatâs the only query required to render the page.
However, if the timestamp of the model is at the other end of an expensive, slow API call, it makes a lot of sense to cache it locally. Thatâs what we do in this gem. If youâve switched on caching in your environment file (usually off for development, and on for production environments), every call to a Contentful-backed model updated_at()
method will cache the response, meaning that itâs really quick for Rails to check the timestamp.
In the ContentfulModel::Base
class, we have a method called cache_key()
which is used by Rails to determine the key to use to cache the document.
If you dive into the code (and I encourage you to do so!) youâll see that there is a simple controller which you can mount into routes.rb as follows:
Youâll get two new routes added to the Rails project, one of which is just for debugging, and only works in the development environment. Just configure your Contentful spaceâs webhook callbacks with your URL. Weâre big fans of Ngrok for local webhooks development.
At the moment, this webhook only deletes the cached timestamp for the entry which has been updated. In future iterations, it would be nice to expose this callback so you can add custom code. It might be a valid case for using Railsâ instrumentation hooks.
Because a lot of Contentful content is stored as Markdown, it makes sense to provide a helper to decode this and render in Rails as HTML. Weâve added a helper built around Redcarpet to simplify this conversion:
While weâre using these gems in production, theyâre only at the beginning of their life. Iâd be delighted if you could join us in developing them. Our to-do list includes:
mimicking a parent-child relationship in a local cache, so we can resolve links between content types quickly;
optionally caching the whole object instead of just the timestamp;
some tests.
If youâve got ideas, please feel free to raise an issue on either project â or even better, a pull request! If youâre keen, weâll happily give you maintainer / commit status on the project. The gems are MIT-licensed, so have fun!
Thank you for reading. We hope our gems will help your projects shine.
We create digital products and brands: websites, software as a service, and mobile applications. Weâre connoisseurs of design, champions for the user, rather nerdy about technology, and great at helping our clients solve tricky problems.
Error works best with people who intend to change an industry, market, society or culture.
Whether itâs a time saving application, a mission-critical at-a-glance dashboard, or a disruptive startup carving out a completely new market with a digital product, our work must empower both our clients and their customers or users.
Ed is passionate about anything technical. He has worked for companies as diverse as UK national radio broadcasters, recording studios, web consultancies and even an online vegetable retailer.
With a degree in education, a background in project management and business analysis, and a strong set of technical skills, you can get him excited about almost everything!
Ed is a director and co-owner of Error, with responsibility for business operations and consultancy. He can be contacted via the studio website.
Subscribe for updates
Build better digital experiences with Contentful updates direct to your inbox.