Category: 4. Sinatra
-
Deploying Your Sinatra App to Production
A step-by-step guide for deploying a Sinatra app to production would definitely make this tutorial too long. But to give you an idea of the options you have, consider: If you use Heroku, one thing to note is that you will need to include a Procfile in your app’s root: text To deploy to a…
-
Running the Sinatra App
To run a modular Sinatra app, you need to include a config.ru file where you specify: ruby
-
Views and Adding Styles
All our views are located in the “views” folder. In here, we also have a layout file — layout.erb — which all views inherit their structure from. It is similar to the layout file in Rails. html We also add a local copy of Bulma CSS and a custom stylesheet in public/css to provide styling for our app.
-
Connecting to the Cost-of-Living API
For our app to show relevant cost-of-living data for whatever city a user inputs, we have to fetch it via an API call to this API. Create a free RapidAPI account to access it if you haven’t done so. We’ll make the API call using the Faraday gem. Add it to the Gemfile and run bundle install.…
-
Database Setup for the Sinatra App
The database setup for our Sinatra app consists of the following: Here’s the database config file: yaml And the ORM and database adaptor gems in the Gemfile: ruby And here’s how you register the ORM and database config in app.rb. ruby
-
The Main File (app.rb)
app.rb is the main entry point into our app where we define what the app does. Notice how we’ve subclassed Sinatra::Base to make the app modular. As you can see below, we include some settings for fetching folders as well as defining the public folder (for storing static files). Another important note here is that we register the Sinatra::ActiveRecordExtension which…
-
Structuring Our Ruby App
To begin with, we’ll take the modular approach with this build so it’s easy to organize functionality in a clean and intuitive way. Our cost-of-living calculator app needs: The app will fetch cost-of-living data from an API hosted on RapidAPI. We won’t include any tests or user authentication to keep this tutorial brief. Go ahead…
-
Regular (Classical) Vs. Modular Sinatra Apps
When it comes to structure in Sinatra apps, you can have regular — sometimes referred to as “classical” — apps, or “modular” ones. In a classical Sinatra app, all your code lives in one file. You’ll almost always find that you can only run one Sinatra app per Ruby process if you choose the regular…
-
Our Example Ruby App
The app we are building will let you input how much you earn as well as the city and country you’d like to move to. Then it will output a few living expense figures for that city. Prerequisites To follow along, ensure you have the following: You can also get the full code for the example…
-
What Is Sinatra Good For?
Because of its lightweight and Rack-based architecture, Sinatra is great for building APIs, mountable app engines, command-line tools, and simple apps like the one we’ll build in this tutorial.