Over the last few days we have been getting a few queries for integrating Sinatara application with App42 Ruby SDK, so we thought about writing a blog post to make this task easy and straightforward.
Today, we’re going to talk about how to configure App42 BaaS with Sinatra. To get started, go to the directory where you want to create your application and create app.rb file (root file of your sinatra application) on your terminal. Copy and paste the below code in the app.rb file.
require 'sinatra'
# Get the welcome page
get '/' do
'Hello world'
end
Once it’s done, create a Gemfile to handle ruby gem dependencies by using bundle init
bundle init will create a Gemfile in your current directory. Now open Gemfile, copy and paste the below code:
source :rubygems
gem "sinatra"
This file starts the sinatra application:
require 'sinatra'
require './app'
run Sinatra::Application
Now run your application from command prompt by typing
$ rackup config.ru
Open your favorite browser and type http://localhost:9292/.You will see the welcome screen.
Now it’s time to create a configuration file which will load your App42 API and Secret key into your application and will create an instance of ServiceAPI.
First create a YML file app42baas.yml inside the config folder of the application which looks like:
api_key: "your api key"
secret_key: "your secret key"
Now load API and Secret key in app.rb file which you should have received during registration, refer the below code.
require 'App42_RUBY_SDK'
APP_CONFIG = YAML.load_file("config/app42baas.yml")
$api = App42::ServiceAPI.new(APP_CONFIG['api_key'], APP_CONFIG['secret_key'] )
That’s it. Now you are ready to use App42 Cloud API module in your application. If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com
Leave A Reply