Hello everyone !

In this blog I want to share basic routes with Ruby Sinatra. Last weeks I have been studying Sinatra, so first question is …

What is Ruby Sinatra???

First of all, sinatra is a free and open source software web application library and domain-specific language written in Ruby. It is an alternative to other Ruby web application frameworks such as Ruby on Rails. And web service can be made from sinatra. Web service can be used to transfer data between web application or other application such as mobile application.

Installation

Open your terminal and run :

gem install sinatra

And add any file that need Sinatara using require 'sinatra'

The relationship between sinatra and http method to create web service?

Oke, by using sinatra routes (which is include HTTP method in needed) its enable to establish HTTP request-respond cycle needed in order to create a web service. One of architectural style using HTTP method is REST API for creating Web Service. The point is Rest Client will access to the Rest Server with URL and will be return from Rest Sever usually with text format, JSON or XML. The most popular return from Rest Server is JSON.

REST API Routing started with URL (Uniform Resource Locator) or commonly refered as web address/path. And than Rest convention gives a blueprint of making the basic routes for CRUD (Create, Read, Update, Delete) functionality in a uniform way.

Most popular HTTP Method usually using in REST API is GET POST PUT DELETE. Obviously so many method from http but i will use the popular HTTP method. For example:

  • Index Get /orders returns list of all orders
  • Show Get /orders/:id Get /orders/123 returns order with id 123
  • Create Post /order creates a new order, returns order or confirmation
  • Update Put /orders/123 updated orders with id 123
  • Destroy Delete /orders/123 Deletes order with id 123

Here i’ll explain routing sinatra work and how work with method get because basicly get is to read data/resource.

Lets imagine we have an Item class that exists in Item.rb and Item class will have access like .get_all_item which get all data from databases. We will take 2 routes, one for all the items and one for for specific instance of the item. It will be:

get '/item' do
    @item = Item.all
    erb: index
end

and

get '/item/:id' do
    @item = Item.find_by(id: params[:id])
    erb: show
end

For first method will excute method all in the class Item.rb and will return with instance variable that will be transferred to the index.erb file, and the index.erb file will be show the list all the instances.

.erb is a method that shows the response to user. Its mean whenever you see you will just be showing to the user with erb also you want to use as an input from the form, you can showing page with .erb to show the form.

And the second, method will be showing a specific instance of Item with id value from URL through the params method and will be showing in the show.erb.

Currently, only this what can i share and so far usually i using method GET and POST. And understanding how the http request-respond cycle work was important to build web service dude, so keep learning.

Thank you!