Difference between Date, Time and DateTime


Bookmark and Share

Date and time are one of the most important aspects which every coder has to deal with in Ruby. Well, let’s get to know how we keep it up alive and functional.
There are 3 different classes in Ruby that handles date and time. They are Date, Time and DateTime. Date and DateTime classes are both from date library. And Time class from its own time library.

In this article we’ll see how Date and Time works. Let’s have a look at each one of them.

Date

When you need a string format of year, month and day, you have to go through Date class.

  • Has date attributes only (year, month, day)
  • Based on integer whole-day intervals from an arbitrary “day zero”
  • Can handle date arithmetic in units of whole days
  • Date object is created with ::new, ::jd, ::ordinal, ::commercial, ::parse, ::strptime, ::today, Time#to_date etc.
  • Takes 4 bytes to store.

Eg:

Time

If you need both date and time values, we can make use of Time class.

  • Has date and time attributes (year, month, day, hour, min, sec, subsec)
  • Can handle negative times before unix time
  • Can handle time arithmetic in units of seconds

Eg:

Also rails provide a really good time class called ActiveSupport::TimeWithZone. It contains all the features the Time

Read More

Rack::Attack – secure you rails app for the real world


Bookmark and Share

Are you worried about the security issues in your Rails app? The rack-attack gem, can help you. Rack::Attack is a rack middleware which provides security to our rails application. It allows us to safelist, blacklist, throttle and to track requests.

  • If the request matches any safelist, it is allowed.
  • If the request matches any blocklist, it is blocked.
  • If the request matches any throttle, a counter is incremented in the Rack::Attack.cache. If any throttle’s limit is exceeded, the request is blocked.
  • Otherwise, all tracks are checked, and the request is allowed.

Implementation

Install the rack-attack gem, or add it to you Gemfile as:

Then tell your app to use the Rack::Attack middleware. For Rails 3+ apps:

Or you can use it in Rackup file as

By default, Rack Attack uses Rails cache. You can override that by setting the Rack::Attack.cache.store value. It is used for throttling. If you want to create use a custom adapter, for example, memory store,  create a file called rack_attack.rb in config/initializers to configure Rack Attack and put the following code in the file:

Throttle

Here we are limiting the request per seconds from the same IP address. Here we are limiting only 3 requests in 10 sec.

Safelist

Above example always …

Read More

How to format Git log for meaningful information


Bookmark and Share

Git is a wonderful piece of software that makes life easier and more productive for a programmer. But sadly most developers don’t use it up to their full potential so we are going to blog about various simple and advanced usages of git as a series. In the first article of the series, we are going to talk about git logs.

Git log is a great feature, that allows us to keep track of our works. There are different options available under git log command, which help us to customize the output of git log and also to filter the log.

Git Graphs

The –graph option draws a graph representing the branches and its structure of commit history. –oneline is used to display commit message and its hash in a single line, which is used along with –decorate, which helps us to easily see which commit belongs to which branch.

Custom formatting

This lets you display each commit however you want in printf style. For example %cn,  %h and %cd which represents committer name, abbreviated hash and committer date respectively.

Filtering the output

Git log can be filtered by different filters and format output

By amount

It displays latest n commits and …

Read More