Cloudant
Table of Contents
Cloudant is a distributed CouchDB service. You can use it just like you would standalone CouchDB but you have the added advantage of your data being redundantly distributed over multiple machines.
Local Setup
Since CouchDB and Cloudant have the same REST API you can use them the same way. You just need a http client to interface with the database and a JSON parser.
In the simplest case you can just use the rest-client and json gems:
$ sudo gem install rest-client json
If you don’t already have CouchDB on your local machine:
OSX:
$ brew install couchdb
Ubuntu:
$ sudo apt-get install couchdb
Using Cloudant with Sinatra
The following code will GET the specified document from the “music” database and then print it to the page:
DB = "#{ENV['CLOUDANT_URL']}/music"
get "/doc/:doc" do
doc = RestClient.get("#{DB}/#{params[:doc]}")
@result = JSON.parse(doc)
haml :doc_id
end
The view would look something like:
%h1 A Doc from CouchDB!
- @result.each do |k,v|
%b=k
%em=v
%br
You would request this page by pointing your browser to:
http://SOMEAPP.heroku.com/doc/YOURDOCUMENT
Deploying to Heroku
To use Cloudant on Heroku, install the Cloudant add-on:
$ heroku addons:add cloudant:basic