Table of contents
1.
Introduction
2.
What is Sinatra?
3.
Accessing the request object
4.
Attachments
5.
Output Examples
6.
Frequently Asked Questions
6.1.
How does Sinatra function?
6.2.
Why should Sinatra always be chosen over Rails?
6.3.
What is Rack middleware?
6.4.
Is Flask similar to Sinatra?
6.5.
What exactly are Rails and Sinatra?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Accessing the Request Object in Sinatra

Author Manan Singhal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Sinatra is a Domain Specific Language for rapidly making web applications in Ruby. It keeps an insignificant list of capabilities, passing on the designer to utilize the devices that best suit them and their application. It can be integrated with any Rack-based application stack, including Rails. Companies like Apple, BBC, GitHub, LinkedIn, and others use it.

Sinatra

What is Sinatra?

Sinatra is a web framework that gives users the core essentials and abstractions to create straightforward and dynamic Ruby web applications. Before developing Sinatra applications, it is crucial to have a fundamental understanding of the Ruby programming language.

The advantage of using Sinatra over more powerful web frameworks like Rails is that it's easier to comprehend how an application's fundamental features operate. It would be more practical to transition to more complex tools, like Rails, after knowing how the essential elements function using a lightweight tool for constructing apps.

MVC stands for Model-View-Controller in web applications.

In other words, MVC emphasizes the separation of logic in our program. Model View Controller is a software design pattern that separates the logic while creating an application.

  1. M: M stands for the models that serve as the central logic and house the data needed for our applications. Classes that interface with the Database to store data can represent the models. Becoming familiar with Active Record is essential as it is utilized in creating Sinatra apps to deal with our model classes effectively. Active Record is an ORM that offers standardized methods to let developers interact model classes with databases, saving them a tonne of time while creating applications.
     
  2. V: V stands for views and is the user's interface with our application. The views comprise our JavaScript, CSS, and HTML code.
     
  3. C: C stands for the controllers in our application is the "middle-man" that communicates with our models and database to correctly render some feature to our browser, depending on the request (sign-in, sign-out, create an account, etc.) the user made to the application. The MVC pattern would be utilized in the straightforward Sinatra application "Vacation Budget Tracker" previously stated.

Accessing the request object

Sinatra offers some incoming request objects which can be accessed from the request level, i.e., filter, error handlers, etc., through the request method option:

get '/foo' do
  t = %w[text/css text/html application/javascript]
  request.accept
  request.accept? 'text/xml'
  request.preferred_type(t)
  request.body
  request.scheme
  request.script_name
  request.path_info
  request.port
  request.request_method
  request.query_string
  request.content_length
  request.media_type
  request.host
  request.get?
  request.form_data?
  request["some_param"]
  request.referrer
  request.user_agent
  request.cookies
  request.xhr?
  request.url
  request.path
  request.ip
  request.secure?
  request.forwarded?
  request.env
end


Sinatra offers some other options, i.e., script_name, path_info, etc., which can be used as follows:

before { request.path_info = "/" }

get "/" do
  "END"
end


The request.body is a StringIO object:

post "/api" do
  request.body.rewind
  data = JSON.parse request.body.read
  "Hello!"
end

Attachments

Sinatra offers a syntax attachment that tells the browser or custom tabs that the response should be stored somewhere in the disk rather than displayed in the browser:

get '/' do
  attachment
  "Store it somewhere."
end


We can also pass the file name where to store the attachment.

get '/' do
  Attachment “store.txt”
  "Store it somewhere."
end

Output Examples

attachment -> we parse string which its store on the given file.

get '/' do
  Attachment “store.txt”
  "Store it somewhere."
end

It stores "Store it somewhere." in the file with a name store.txt

Frequently Asked Questions

How does Sinatra function?

The web server responds to a series of queries that Sinatra sends and receives. These requests are contained in a controller file, which is part of your program and communicates with the internet. A controller is a component of the MVC architectural design pattern (Models-Views-Controllers).

Why should Sinatra always be chosen over Rails?

Sinatra performs fewer functions out of the box and is significantly more lightweight, resource-efficient, and demanding. Rails are crammed with features and come with a tonne of code, and if you know how to use it, it makes it very simple to construct complex web applications quickly.

What is Rack middleware?

You can allude rack middleware as a bit of part that helps with the execution of an undertaking. For instance, You can imagine different middleware doing various cycles like logging and reserving.

Is Flask similar to Sinatra?

Sinatra is ideal for basic apps, much like Flask. Only 4 LOC of the actual Sinatra application is contained in a single Ruby source file, due to which Flask is used instead of libraries like Ruby on Rails. Sinatra is used since it allows you to start small and grow the application as necessary.

What exactly are Rails and Sinatra?

Rail is a framework for creating model-driven web applications, while Sinatra is a library for handling HTTP on the server side. Sinatra is an excellent tool for handling HTTP requests and responses. Rails are the way to go if you need full integration and as little boilerplate as possible.

Conclusion

In this article, we have learned how to access the request object in Sinatra. I hope you would have gained a better understanding of these topics now!

Refer to our guided paths on Coding Ninjas Studio to learn about Data Structure and Algorithms, Competitive Programming, JavaScript, etc. Enroll in our courses and refer to our mock test available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Thank You
Live masterclass