Ruby Cookbook

Getting Started

The Ruby driver is published to https://rubygems.org/gems/cloudcms

Install the gem as you would any other Gem gem install cloudcms

You may choose to use Bundler or other dependency management tool.

Connecting to Gitana

To connect, create a file called gitana.json in a folder location readable by your application. For information on how to acquire this file, please read up on Acquiring your API Keys.

Load the library containing the driver: require 'cloudcms'

Connect to Cloud CMS by calling the connect method:

The connect method looks for a file in the current directory called gitana.json for connection and credential information. Or you can specify the file location.

cloudcms = Cloudcms::Cloudcms.new()
platform = cloudcms.connect()

The gitana.json file should look like:

{
    "clientKey": "{your client key}",
    "clientSecret": "{your client secret}",
    "username": "{your username or auth grant key}",
    "password": "{your password or auth grant secret}",
    "baseURL": "{the url to the Cloud CMS API endpoint}",
    "application": {"application id of the application object where these keys exist"}
}

To get this file and these values, please check out our section on API Keys.

Code Samples

Here are some code samples to help you get started. For all of these scenarios, we assume that you're connecting using the simplest method (loading gitana.json from the current working folder of your process).

In Gitana, a project contains a content repository. And this repository can have multiple branches (similar to Git). Branches are where you work with content nodes. There is always a master branch on a repository by default.

The first thing you should do is connect. You will then have a Platform object. Platform.project represents the project you are connected to based on the 'application' property in your gitana.json file.

Get your Repository

platform.project.repository is your is your content repository. There is also a shortcut on the Platform object: platform.repository:

cloudcms = Cloudcms::Cloudcms.new()
platform = cloudcms.connect()

repository = platform.repository

Get the Master Branch

For the purpose of most of these examples, we'll use the master branch on the content repository.

Use the read_branch method on the Repository object to fetch a specific branch. The user you've logged in as must have READ permissions to the branch. If they do not have read rights, a nil response is expected.

cloudcms = Cloudcms::Cloudcms.new()
platform = cloudcms.connect()

repository = platform.repository

branch = repository.read_branch("master");

There is also a shortcut to the master branch on the Repository object:

cloudcms = Cloudcms::Cloudcms.new()
platform = cloudcms.connect()

repository = platform.repository

branch = repository.master

Node Creation

Create a Node

Create a basic node of type n:node (the default type).

cloudcms = Cloudcms::Cloudcms.new()
platform = cloudcms.connect()

branch = platform.repository.master

new_node = {
    title: 'My First Node'
}

node = branch.create_node(new_node)

Create a Node for a Custom Content Type

Suppose we have a custom content type called custom:article that looks like this:

{
    "title": "Custom Article",
    "_qname": "custom:article",
    "type": "object",
    "properties": {
        "title": {
            "type": "string"
        },
        "body": {
            "type": "string"
        },
        "categories": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "rating": {
            "type": "number"
        }
    }
}

Create a content instance of this type like this:

cloudcms = Cloudcms::Cloudcms.new()
platform = cloudcms.connect()

master = platform.repository.master

new_node = {
    "title": "Custom Article",
    "_qname": "custom:article",
    "type": "object",
    "properties": {
        "title": {
            "type": "string"
        },
        "body": {
            "type": "string"
        },
        "categories": {
            "type": "array",
            "items": {
                "type": "string"
            }
        },
        "rating": {
            "type": "number"
        }
    }
}

node = master.create_node(new_node)

Query for Nodes

Let's now look at querying for content. This makes use of MongoDB's query language to express some pretty powerful queries. We recommend further reading on Query within Gitana.

Basic Querying

Find all of the nodes of type "custom:article1" and have a category with the value category1.

cloudcms = Cloudcms::Cloudcms.new()
platform = cloudcms.connect()

master = platform.repository.master

query = {
    '_type': 'custom:article',
    'category': 'category1'
}

node_array = branch.query_nodes(query);

node_array.each do |node|
    puts 'node: ' + JSON.pretty_generate(node)
end