rails in the bowels

28
Rails in the b owels* * Rails nas entranhas. Source: Google Translate =) by Raphael Monteiro - 04/2015

Upload: bankfacil

Post on 07-Aug-2015

153 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Rails in the bowels

Rails in the bowels*

* Rails nas entranhas. Source: Google Translate =)

by Raphael Monteiro - 04/2015

Page 2: Rails in the bowels
Page 3: Rails in the bowels

What are we talking about?

Things that nobody(or almost nobody) talk about.

Keep the secret or DIE!!(just kidding S2)

Page 4: Rails in the bowels

Let me explain...

● Rails on Rack (What the RACK is going on?!)

● ‘Action Dispatch: Where it all begins’

Page 5: Rails in the bowels

Rails on Rack: part I

‘Rack aims to provide a minimal API for connecting web server and web frameworks’

Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.

Page 6: Rails in the bowels

Rails on Rack: part II

● Example: rails server

RACK(process and build)

Web Serverand

App Server(Webrick)

Browser(Client side)

Web Framework

Page 7: Rails in the bowels

Rails on Rack: part III

● The most simple Rack application

Page 8: Rails in the bowels

Rails on Rack: part IV

● Rails 2.3, request handling was moved to Rack and the concept of middleware was introduced.

● Classes that satisfy Rack's call interface can be chained together as filters. (Rack Middleware)

Page 9: Rails in the bowels

Rails on Rack: part V

● Much of Action Controller is implemented as Rack middleware modules.

$ rake middleware

Page 10: Rails in the bowels
Page 11: Rails in the bowels

Rails on Rack: part VII

Page 12: Rails in the bowels

Rails on Rack: part VIII

● You can create your own middleware● config/application.rb ou environment/*.rb● config.middleware.use(new_middleware,

args)● config.middleware.insert_before(existing_

middleware, new_middleware, args)● Don't forget to initizalize and call

app.call(env)

Page 13: Rails in the bowels

Rails on Rack: summarize

● Rack is a framework to roll your own ruby framework.● Rack provides an interface between different web servers and your framework/application.

Making it very simple for your framework/application to be compatible with any webserver that supports Rack – Phusion Passenger, Litespeed, Mongrel, Thin, Ebb, Webrick to name a few.

● Rack cuts your chase. You get request, response, cookies, params & sessions for free.● Makes it possible to use multiple frameworks for the same application, provided there is no

class collision. Rails and sinatra integration is a good example of this.● Middlewares ! Think of middlewares as Rails’s before_filter/after_filter that are reusable across

different rack supported frameworks/applications. For example, you can use the same Anti-spamming rack middleware for your Rails app, Sinatra app and your custom Rack application too!

Page 14: Rails in the bowels

Action Dispatch: Where it all begins

● The entry point to a request is an instance of ActionDispatch::Routing::RouteSet

● Example:route: get 'foo', to: 'foo#index'instance: FooController.action(:index).call

Page 15: Rails in the bowels

Action Dispatch: Where it all begins

Page 16: Rails in the bowels

Action Dispatch: Where it all begins

Page 17: Rails in the bowels

Action Dispatch: Where it all begins

Page 18: Rails in the bowels

Action Dispatch: Where it all begins

endpoint is our application's routes, an instance of ActionDispatch::Routing::RouteSet

Page 19: Rails in the bowels

Action Dispatch: Where it all begins

● Operational::Application.routes

and then invoke the #call method...

Page 20: Rails in the bowels

@router is an instance of Journey::Router

Action Dispatch: Where it all begins

Page 21: Rails in the bowels

Action Dispatch: Where it all begins

● Journey is the core routing module in ActionDispatch.

● It is constructed by the code you define in config/routes.rb

Page 22: Rails in the bowels

Action Dispatch: Where it all begins

Page 23: Rails in the bowels

Action Dispatch: Where it all begins

● When constructing the routing table, the ActionDispatch::Routing::Mapper class calls add_route, which will construct a Journey::Route instance associated with a controller endpoint (the Rack app) and add it to the routing table.

Page 24: Rails in the bowels

Action Dispatch: Where it all begins

Page 25: Rails in the bowels

Action Dispatch: Where it all begins

● @set is the RouteSet ● app referenced here is an instance of

ActionDispatch::Routing::Dispatcher

Page 26: Rails in the bowels

Action Dispatch: Where it all begins

Page 27: Rails in the bowels

Action Dispatch: Where it all begins

Finally…

FooController.action(:index).call

Page 28: Rails in the bowels

>More information</

● Rack Creator: http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html

● Inside Rack and ActionDispatcher: http://pothibo.com/2013/11/ruby-on-rails-inside-actiondispatch-and-rack/

● Understaing Rails from request to response in 3 parts(part I): http://andrewberls.com/blog/post/rails-from-request-to-response-part-1--introduction

● How to create a Ruby web server: https://github.com/macournoyer/tube

● The Rails 4 Way, O. Fernandez, K. Faustino, June 2014