DEV Community

Lucas Barret
Lucas Barret

Posted on

The Railtie, the Gem and the Middleware

I had the luck to interview Alexis Bernard about his works for my podcast. You should check this out and test one of his gems RoRvsWild.

I was surprised when I installed the gem. The middleware was directly available in the rails Middlewares list without any action except: bundle install and rails s.

I wanted to understand how this is possible in Rails.

Create our Gem

Since a part the blog post's subject is creating a gem, let's create one. This is simple. You can follow the following simple steps :

  1. Create a gemspec file like the following
#middlwar.gemspec
    Gem::Specification.new do |s|
  s.name        = "middlwar"
  s.version     = "0.0.0"
  s.summary     = "middl"
  s.authors     = ["Lucas Barret"]
  s.files       = ["lib/middlwar.rb"]
end
Enter fullscreen mode Exit fullscreen mode
  1. Create the code of your gem
    We will cover this in the second part of the article :)

  2. Build the gem with :

gem build my_middlware.gemspec
Enter fullscreen mode Exit fullscreen mode

If you want to have more information and dig even deeper, you can check in the guides of rubygems website.

Okay, since we know how to create the gem, we can know focus in the second part of the article how to create a middleware, use it as gem and fin insert it in rails Middlewares list.

Our Gem code

This is the problem of the eggs or the chicken. But here I will begin with my middleware code.

To create a middleware for a rails app. All you have to do is creating a class that looks like this one :

class MyMiddleware
    def initialize(app)
      @app = app
    end

    def call(env)
      return [200, {}, ['I am a middleware']]
    end
end
Enter fullscreen mode Exit fullscreen mode

This middleware get the HTTP request and always send back a response with HTTP code 200 and 'I am a middleware' as a body.

But this won't appear in your app as is, you need to something more. Initialize your middleware and insert it in your rails app.

#lib/middlwar.rb
class MyRailtie < Rails::Railtie
    def self.insert_middleware
      initializer "middle.configure_rails_initialization" do |app|
        app.middleware.unshift MyMiddleware
      end
    end 
    insert_middleware
end

Enter fullscreen mode Exit fullscreen mode

What I want to put stress on is the Middleware part and especially the Railties.

As usual, going through documentation cannot make anything worse—and going to the Rails documentation espcially.

We can learn that Railties are the core of the Rails Framework and can modify or extend the initialization process of Rails.

Every significant component of Rails, like ActiveRecord, implements a Railtie.
And they are responsible for their initialization.

Could we say that Rails is a collection of useful gem that makes sense all together ?

Whatever but here thanks to the railtie we initiatilize how own middleware/gem.

I have define a class level method and then call it directly from inside my class. (Yeah remember even class code is executed when parsed by ruby interpreter).

And that's it my middleware is instantiate and thanks to the unshift method put at the top of the middleware list.

Put it in the Rails app

This is maybe the simplest thing in this article :
We will not push this gem on the Rubygems website, but we can include our gem code locally.

In your gem file, you can add a line like this :

gem "my_middlware", path: "../my_middlware"
Enter fullscreen mode Exit fullscreen mode

Now go for a bundle install.

After that, you can load your middleware by restarting your server or just listing your server' Middlewares with rails middleware

Wrap all the things up

Image description

That's all folks, we have done a lot today. Creating a middleware extract it as a gem, and insert it in our Rails app as the first middleware. Eventually we even learn how to use local gem.

I hope to see you for another article, in the mean time if you have question do not hesitate to comment :).

Top comments (0)