rails in the bowels

Post on 07-Aug-2015

153 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Rails in the bowels*

* Rails nas entranhas. Source: Google Translate =)

by Raphael Monteiro - 04/2015

What are we talking about?

Things that nobody(or almost nobody) talk about.

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

Let me explain...

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

● ‘Action Dispatch: Where it all begins’

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.

Rails on Rack: part II

● Example: rails server

RACK(process and build)

Web Serverand

App Server(Webrick)

Browser(Client side)

Web Framework

Rails on Rack: part III

● The most simple Rack application

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)

Rails on Rack: part V

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

$ rake middleware

Rails on Rack: part VII

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)

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!

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

Action Dispatch: Where it all begins

Action Dispatch: Where it all begins

Action Dispatch: Where it all begins

Action Dispatch: Where it all begins

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

Action Dispatch: Where it all begins

● Operational::Application.routes

and then invoke the #call method...

@router is an instance of Journey::Router

Action Dispatch: Where it all begins

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

Action Dispatch: Where it all begins

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.

Action Dispatch: Where it all begins

Action Dispatch: Where it all begins

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

ActionDispatch::Routing::Dispatcher

Action Dispatch: Where it all begins

Action Dispatch: Where it all begins

Finally…

FooController.action(:index).call

>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

top related