Table of contents
1.
Introduction
2.
What is Sinatra?
3.
Working with Date and Time
4.
Output Examples
5.
Frequently Asked Questions
5.1.
How does Sinatra function?
5.2.
What exactly are Rails and Sinatra?
5.3.
What is Rack middleware?
5.4.
Why should Sinatra always be chosen over Rails?
5.5.
Is Flask similar to Sinatra?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Working with Date and Time 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 used as 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.

Working with Date and Time

Sinatra offers a syntax time_for that creates an instance of the Time object from the input value. We can convert this time object to DateTime and Date or any other related classes.

get '/' do
  pass if Time.now > time_for('Aug 2, 2022')
  "You still have time."
end


time_for method used internally by another syntax i.e. expires, last_modified, etc. So, you can further extend the behavior of these methods by overriding time_for in your application.
 

helpers do
  def time_for(value)
    case value
    when :tomorrow  then Time.now + 24*60*60
    when :yesterday then Time.now - 24*60*60
    else super
    end
  end
end

get '/' do
  last_modified :yesterday
  expires :tomorrow
  "Hi Ninja"
end

Output Examples

Time.now -> returns current time
time_for('Aug 2, 2022') -> returns 2 August 2022 time in ms

Now, we can check if Time.now > time_for('Aug 2, 2022') -> True

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).

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.

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.

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.

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.

Conclusion

In this article, we have learned how to deal with dates and times 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