Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/contentful/the-example-app.rb

Example app for Contentful in Ruby
https://github.com/contentful/the-example-app.rb

contentful heroku ruby sinatra

Last synced: about 2 months ago
JSON representation

Example app for Contentful in Ruby

Awesome Lists containing this project

README

        

> **Note**: This repo is no longer officially maintained as of Jan, 2023.
> Feel free to use it, fork it and patch it for your own needs.

## The Ruby example app

[![CircleCI](https://img.shields.io/circleci/project/github/contentful/the-example-app.rb.svg)](https://circleci.com/gh/contentful/the-example-app.rb)

The Ruby example app teaches the very basics of how to work with Contentful:

- consume content from the Contentful Delivery and Preview APIs
- model content
- edit content through the Contentful web app

The app demonstrates how decoupling content from its presentation enables greater flexibility and facilitates shipping higher quality software more quickly.

Screenshot of the example app

You can see a hosted version of `The Ruby example app` on Heroku.

## What is Contentful?
[Contentful](https://www.contentful.com) provides a content infrastructure for digital teams to power content in websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship digital products faster.

## Requirements

* Ruby 2.3 or higher
* Git
* Contentful CLI (only for write access)

Without any changes, this app is connected to a Contentful space with read-only access. To experience the full end-to-end Contentful experience, you need to connect the app to a Contentful space with read _and_ write access. This enables you to see how content editing in the Contentful web app works and how content changes propagate to this app.

## Common setup

Clone the repo and install the dependencies.

```bash
git clone https://github.com/contentful/the-example-app.rb.git
```

```bash
bundle install
```

## Steps for read-only access

To start the server, run the following

```bash
foreman start
```

Open [http://localhost:3000](http://localhost:3000) and take a look around.

## Steps for read and write access (recommended)

Step 1: Install the [Contentful CLI](https://www.npmjs.com/package/contentful-cli)

Step 2: Login to Contentful through the CLI. It will help you to create a [free account](https://www.contentful.com/sign-up/) if you don't have one already.
```
contentful login
```
Step 3: Create a new space
```
contentful space create --name 'My space for the example app'
```
Step 4: Seed the new space with the content model. Replace the `SPACE_ID` with the id returned from the create command executed in step 3
```
contentful space seed -s '' -t the-example-app
```
Step 5: Head to the Contentful web app's API section and grab `SPACE_ID`, `DELIVERY_ACCESS_TOKEN`, `PREVIEW_ACCESS_TOKEN`.

Step 6: Open `.env` and inject your credentials so it looks like this

```
APP_ENV=development
CONTENTFUL_SPACE_ID=
CONTENTFUL_DELIVERY_TOKEN=
CONTENTFUL_PREVIEW_TOKEN=
SESSION_SECRET=someSecretTokenHere
PORT=3000
```

Step 7: To start the server, run the following
```bash
rerun 'foreman start'
```
Final Step:

Open [http://localhost:3000?editorial_features=enabled](http://localhost:3000?editorial_features=enabled) and take a look around. This URL flag adds an “Edit” button in the app on every editable piece of content which will take you back to Contentful web app where you can make changes. It also adds “Draft” and “Pending Changes” status indicators to all content if relevant.

## Use Docker
You can also run this app as a Docker container:

Step 1: Clone the repo

```bash
git clone https://github.com/contentful/the-example-app.rb.git
```

Step 2: Build the Docker image

```bash
docker build -t the-example-app.rb .
```

Step 3: Run the Docker container locally:

```bash
docker run -p 3000:3000 -d the-example-app.rb
```

If you created your own Contentful space, you can use it by overriding the following environment variables:

```bash
docker run -p 3000:3000 \
-e CONTENTFUL_SPACE_ID= \
-e CONTENTFUL_DELIVERY_TOKEN= \
-e CONTENTFUL_PREVIEW_TOKEN= \
-d the-example-app.rb
```

## Deploy to Heroku
You can also deploy this app to Heroku:

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)