Heroku Connect with Sinatra and ActiveRecrod

This article shows how to run a Sinatra app with ActiveRecord which uses PostgreSQL based Heroku Connect

../_images/heroku-connect-flow-sinatra.png

Figure 1 : Heroku-Connect Data flow

Figure 1 show the how HerokuConnect Add-On interacts with Heroku Postgres and force.com behind the scenes Make sure you have Ruby along with Sinatra installed. Also, install the Heroku Toolbelt

Install Dependencies

Sinatra is installed like any other gem

$ gem install sinatra

Demo app

  1. Clone the Demo App from github

    $ git clone https://github.com/rajdeepd/no-local-dev-getting-started
    
  2. Controller of the App is in the file app.rb. Notice that :code:”/contacts” is commented out.

    # app.rb
    
    require 'sinatra'
    require 'sinatra/activerecord'
    require './environments'
    
    
    get "/" do
      erb :home
    end
    
    
    class Contact < ActiveRecord::Base
      self.table_name = 'salesforce.contact'
    
    end
    
    #get "/contacts" do
    #  @contacts = Contact.all
    #  erb :index
    #end
    
  3. Procfile shown below will be used by Heroku to detect the sinatra app

    web: ruby app.rb
    console: tux
    
  1. Run the app using the following command

    $ bundle install
    $ rackup
    

    Output will be similar to the listing below

    $ rackup
    Ignoring binding_of_caller-0.7.2 because its extensions are not built.  Try: gem pristine binding_of_caller --version 0.7.2
    Ignoring byebug-8.2.0 because its extensions are not built.  Try: gem pristine byebug --version 8.2.0
    Ignoring debug_inspector-0.0.2 because its extensions are not built.  Try: gem pristine debug_inspector --version 0.0.2
    Ignoring gem-wrappers-1.2.7 because its extensions are not built.  Try: gem pristine gem-wrappers --version 1.2.7
    Ignoring json-1.8.3 because its extensions are not built.  Try: gem pristine json --version 1.8.3
    Ignoring nokogiri-1.6.6.4 because its extensions are not built.  Try: gem pristine nokogiri --version 1.6.6.4
    [2015-12-24 16:26:28] INFO  WEBrick 1.3.1
    [2015-12-24 16:26:28] INFO  ruby 2.2.0 (2014-12-25) [x86_64-linux]
    [2015-12-24 16:26:28] INFO  WEBrick::HTTPServer#start: pid=54104 port=9292
    

    Your app should now be running locally. Check the port no from the console

Deploying to Heroku

$ heroku create
$ git push heroku master
$ heroku open

You will see that default URL works as shown below

../_images/sinatra_home.png

Contact Endpoint will give an error, we will fix it

../_images/sinatra_list_contacts_err.png

PostgreSQL Add-On

PostgreSQL Add-On will be already added in your application

Add Heroku Connect Add-On

Configure Heroku Connect Add-On. Command below configures Herok-Connect Add-On to the application.

$ heroku addons:create herokuconnect

Configure Herok Connect Add-On

  1. Setup Connection
../_images/setup-connection1.png
  1. Enter Schema Name : This is the schema name underwhich database will be created.
../_images/enter-schemaname1.png
  1. Trigger OAuth
../_images/trigger-oauth1.png
  1. Enter Salesforce.com developer account credentials
../_images/oauth1.png
  1. Create Mappings
../_images/create-mappings1.png
  1. Create Mappings Contacts : Choose the fields in Salesforce Schema which need to be mapped to Postgres Database in the application.
../_images/create-mapping-contacts1.png
  1. Explore Contacts in the Dashboard
../_images/contacts-explorer1.png

Add Code for contacts endpoint

Uncomment the following lines which configure Connection object conn to PostgreSQL Database.

get "/contacts" do
  @contacts = Contact.all
  erb :index
end

Update Changes in Heroku

$ git add .
$ git commit -m "Added code for contacts"
$ git push heroku master

Open the App again in Heroku

$ heroku open

Show Contacts

Browse to URL http://{your-app-name}.herokuapp.com/contacts to see the list of contact names.

../_images/sinatra_list_contacts.png

Summary

In this tutorial we learnt how to configure a Ruby Sinatra Application to work with Heroku Connect. We used ActiveRecord ORM Framework for talking to the PostgreSQL database deployed on Heroku.