Devise Facebook Omniauth login with connect and disconnect functionality

When talking about users authentication in Rails land there is one name that generally stands above all the other available gems. This name is Devise.

I would not be so wrong to call it the de facto standard of Rails authentication considering the time has been around and the vast documentation it has under its belt.

Regarding for example the OAuth2 functionality there is a well documented page inside the wiki that describes how to implement it for Facebook.

Unfortunately what presented inside the documentation doesn’t always blend well with the other functionalities of an application.

Continue reading “Devise Facebook Omniauth login with connect and disconnect functionality”

Previewing email in Rails with ActionMailer::Preview and Letter Opener

Developing email templates to be sent by our Rails application may sometimes be a long and tedious operation because under development you must continuously send to yourself the email in order to test the changes.

If you don’t want to waste your time, Rails provides a very helpful tool for those who need to build the layout for the emails of our application: ActionMailer::Preview. In fact, with this tool (available from Rails 4.1), you are able to generate the Rails mailer view without the need to actually send the email.
Continue reading “Previewing email in Rails with ActionMailer::Preview and Letter Opener”

There is trouble brewing, or: Openssl issues on Ruby with Homebrew

This post is to describe an issue I’ve run into using < 2.3 versions of Ruby on OSX with rvm and brew.

Don’t even get me started on working with older versions of Ruby — we have some maintenance projects running in ruby-1.8.3! Even modern Rubies may give you a hard time, however, and this is one of the times. Actually, I am writing this post as a reminder for me because I ran into the same problem twice now, and what is worse, the second time it took me nearly the same amount of fruitless googling to solve it as it took the first. And the solution was the same! So, let us proceed with order.

Continue reading “There is trouble brewing, or: Openssl issues on Ruby with Homebrew”

asdf the “easy to write and hard to read” version manager

As a Rubyist one of the first thing you end up doing is to manage many different Ruby versions on the same machine. As a matter of fact, one of the first steps in setting up a new workstation is to install some kind of version manager like RVM or rbenv.

Unfortunately it doesn’t end up simply like this…

Continue reading “asdf the “easy to write and hard to read” version manager”

Wrap and iterate with the power of yield

Iterating over a collection isn’t rocket science. Obviously if you need to iterate over a really big collection you may need to adjust your strategy but it is something that in the vast majority of programming languages you get quite for free.

In Ruby for example you can just send the each message to your collection together with a block of code. The block will be executed for each element of the collection itself.

But what if you need “something more” from the objects you’re iterating on? If, for example, each element of your initial collection is an array and you want to abstract away how you access each of its elements? What options do you have?

Continue reading “Wrap and iterate with the power of yield”

Euruko 2016 – Ruby is dead, long live Ruby!

It has almost been three weeks since we (me and two colleagues) came back from a wonderful trip to Sofia. Thanks to Mikamai (and I will never stop to be thankful for this! 🤗) we had the opportunity to attend the 2016 Euruko conference.

Euruko2016

Considering the list of speakers, all well known for their accomplishments in and out the Ruby community, I was overexcited by the time we had the confirmation of our trip.
Personally I was particularly thrilled to listen to the two keynotes. It doesn’t happen everyday to listen to Matz and José live! 😉
Anyway, enough fangirling!

Continue reading “Euruko 2016 – Ruby is dead, long live Ruby!”

Calculate an approximation of a server response time with Rails and Apache

This post was born from the need of calculating some kind of server response time during a page request. Although this is not perfect, because doesn’t account for the time that the response takes to reach the user, it’s pretty accurate on the server side.

In order to have the request start time I added this header in the Apache configuration server:

RequestHeader append Request-Started "%t"

I didn’t use a simple before_filter because it would have not considered the time spent in rack middlewares, for example.

Now in my action I can count on a request header like this:

Request-Started: t=1460473552166899

Note that this timestamp is in microseconds.

Now in the page view template I can get the total request time doing some simple math:


server_response_time = current_time - time_from_header

def current_time
  Time.now.utc
end

def time_from_header
  Time.at(request.headers['Request-Started'].split("=").last.to_i / 1_000_000).utc
end