SlideShare a Scribd company logo
Ruby off Rails

 by Stoyan Zhekov
Kyoto, 17-May-2008
Table of content
 ●   Coupling and Cohesion
 ●   Common needs for various web apps
 ●   ORM - Sequel (short overview)
 ●   Ruby Web Evolution
 ●   Enter Rack - The Middle Man
 ●   Thin, Ramaze, Tamanegi
 ●   Summary, Extras, Questions

08/05/19                                 2
Coupling and Cohesion (1)
 ●   Cohesion
       http://en.wikipedia.org/wiki/Cohesion_(computer_science)
 ●   Coupling
       http://en.wikipedia.org/wiki/Coupling_(computer_science)




08/05/19                                                          3
Coupling and Cohesion (2)

Applications should be composed of components
 that show:
 ●   High cohesion
       –   doing small number of things and do them well
 ●   Low (loosely) coupling
       –   easy replace any component with another one




08/05/19                                                   4
Coupling and Cohesion (3)




             Google vs Amazon



08/05/19                               5
Coupling and Cohesion (4)




                     VS




08/05/19                               6
What about Rails?




08/05/19                       7
Ruby on Rails
 ●   Full Stack – was good. Now?
 ●   Convention over configuration – good?
 ●   DRY – possible?
       –   Generators
       –   Plugins
       –   Libraries




08/05/19                                     8
Life without Rails




           Is it possible?



08/05/19                        9
Common needs for various web apps

 ●   Database handling: ORM
 ●   Request processing: CGI env, uploads
 ●   Routing/dispatching: map URLs to classes
 ●   Rendering/views: rendering libraries
 ●   Sessions: persist objects or data




08/05/19                                        10
Database ORM
 ●   Active Record ?= ActiveRecord
 ●   ActiveRecord – a lot of problems
       –   http://datamapper.org/why.html
 ●   Data Mapper – fast, but not ready
       –   No composite keys (when I started using it)
       –   Have it's own database drivers
 ●   Sequel – good, I like it


08/05/19                                                 11
ORM – Sequel (Why?)
 ●   Pure Ruby
 ●   Thread safe
 ●   Connection pooling
 ●   DSL for constructing DB queries
 ●   Lightweight ORM layer for mapping records to
     Ruby objects
 ●   Transactions with rollback


08/05/19                                            12
ORM – Sequel (Core)
 ●   Database Connect
      require 'sequel'
      DB = Sequel.open 'sqlite:///blog.db'


      DB = Sequel.open 'postgres://cico:12345@localhost:5432/mydb'


      DB = Sequel.open(quot;postgres://postgres:postgres@localhost/my_dbquot;,
       :max_connections => 10, :logger => Logger.new('log/db.log'))




08/05/19                                                                 13
ORM – Sequel (Core, 2)
DB << quot;CREATE TABLE users (name VARCHAR(255) NOT NULL)quot;

DB.fetch(quot;SELECT name FROM usersquot;) do |row|
 p r[:name]
end


dataset = DB[:managers].where(:salary => 50..100).order(:name, :department)


paginated = dataset.paginate(1, 10) # first page, 10 rows per page
paginated.page_count #=> number of pages in dataset
paginated.current_page #=> 1



08/05/19                                                                      14
ORM – Sequel (Model)

           class Post < Sequel::Model(:my_posts)
            set_primary_key [:category, :title]

            belongs_to :author
            has_many :comments
            has_and_belongs_to_many :tags

            after_create do
             set(:created_at => Time.now)
            end

           end


08/05/19                                           15
ORM – Sequel (Model, 2)

           class Person < Sequel::Model
            one_to_many :posts, :eager=>[:tags]

            set_schema do
             primary_key :id
             text :name
             text :email
             foreign_key :team_id, :table => :teams
            end
           end




08/05/19                                              16
Evolution




08/05/19               17
Ruby Web Evolution




           Webrick   Mongrel   Event     Thin   Ebb
                               Machine




05/19/08                                              18
Thin web server
 ●   Fast – Ragel and Event Machine
 ●   Clustering
 ●   Unix sockets (the only one I found) - nginx
 ●   Rack-based – Rails, Ramaze etc.
 ●   Rackup files support (follows)




05/19/08                                           19
Thin + Nginx
           thin start --servers 3 --socket /tmp/thin
           thin start --servers 3 –post 3000
           # nginx.conf
           upstream backend {
             fair;
             server unix:/tmp/thin.0.sock;
             server unix:/tmp/thin.1.sock;
             server unix:/tmp/thin.2.sock;
           }

05/19/08                                               20
The problem


           Merb    Ramaze       Sinatra    ...




                  How to connect them?



           CGI     Webrick     Mongrel    Thin




05/19/08                                         21
The solution - Rack


           Merb   Ramaze        Sinatra    ...




                  Rack – The Middle Man




           CGI    Webrick      Mongrel    Thin




05/19/08                                         22
What is Rack?




05/19/08                   23
What IS Rack?

           http://rack.rubyforge.org/




             By Christian Neukirchen
05/19/08                                24
WHAT is Rack?
 ●   Specification (and implementation) of a minimal
     abstract Ruby API that models HTTP
       –   Request -> Response
 ●   An object that responds to call and accepts one
     argument: env, and returns:
       –   a status, i.e. 200
       –   the headers, i.e. {‘Content-Type’ => ‘text/html’}
       –   an object that responds to each: ‘some string’


05/19/08                                                       25
This is Rack!
      class RackApp
         def call(env)
            [ 200, {quot;Content-Typequot; => quot;text/plainquot;}, quot;Hi!quot;]
         end
      end

      app = RackApp.new




05/19/08                                                      26
Free Hugs




           http://www.freehugscampaign.org/
05/19/08                                      27
Hugs Application




               p 'Hug!'



05/19/08                      28
ruby hugs.rb

           %w(rubygems rack).each { |dep| require dep }

           class HugsApp
              def call(env)
                 [ 200, {quot;Content-Typequot; => quot;text/plainquot;}, quot;Hug!quot;]
              end
           end

           Rack::Handler::Mongrel.run(HugsApp.new, :Port => 3000)




05/19/08                                                            29
Rack::Builder
   require 'hugs'
   app = Rack::Builder.new {
    use Rack::Static, :urls => [quot;/cssquot;, quot;/imagesquot;], :root => quot;publicquot;
    use Rack::CommonLogger
    use Rack::ShowExceptions

       map quot;/quot; do
        run lambda { [200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hi!quot;]] }
       end

       map quot;/hugquot; do
        run HugsApp.new
       end
   }

   Rack::Handler::Thin.run app, :Port => 3000
05/19/08                                                                  30
Rack Building Blocks
 ●   request = Rack::Request.new(env)
       –   request.get?
 ●   response = Rack::Response.new('Hi!')
       –   response.set_cookie('sess-id', 'abcde')
 ●   Rack::URLMap
 ●   Rack::Session
 ●   Rack::Auth::Basic
 ●   Rack::File
05/19/08                                             31
Rack Middleware

           Can I create my own use Rack::... blocks?
           class RackMiddlewareExample
            def initialize app
              @app = app
            end

            def call env
            @app.call(env)
            end
           end

05/19/08                                               32
Common needs for various web apps

 ●   Database handling: ORM Sequel
 ●   Request processing: Rack::Request(env)
 ●   Routing/dispatching: Rack 'map' and 'run'
 ●   Rendering/views: Tenjin, Amrita2
 ●   Sessions: Rack::Session




05/19/08                                         33
Does it scale?



05/19/08                    34
rackup hugs.ru

           require 'hugs'
           app = Rack::Builder.new {
           use Rack::Static, :urls => [quot;/cssquot;, quot;/imagesquot;], :root => quot;publicquot;

           map quot;/quot; do
            run lambda { [200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hi!quot;]] }
           end

           map quot;/hugquot; do
             run HugsApp.new
           end
           }
           Rack::Handler::Thin.run app, :Port => 3000


05/19/08                                                                       35
Hugs Service
 ●   rackup -s webrick -p 3000 hugs.ru
 ●   thin start --servers 3 -p 3000 -R hugs.ru
 ●   SCGI
 ●   FastCGI
 ●   Litespeed
 ●   ....



05/19/08                                         36
Rack-based frameworks
 ●   Invisible – thin-based framework in 35 LOC
       –   http://github.com/macournoyer/invisible/
 ●   Coset – REST on Rack
 ●   Halcyon – JSON Server Framework
 ●   Merb
 ●   Sinatra
 ●   Ramaze
 ●   Rails !?
05/19/08                                              37
Rack coming to Rails
 ●   Rails already have Rack, but:
       –   raw req -> rack env -> Rack::Request ->CGIWrapper ->
           CgiRequest
 ●   Ezra Zygmuntowicz (merb)'s repo on github
       –   http://github.com/ezmobius/rails
       –   raw req -> rack env -> ActionController::RackRequest
       –   ./script/rackup -s thin -c 5 -e production




05/19/08                                                          38
Ramaze
 ●   What: A modular web application framework
 ●   Where: http://ramaze.net/
 ●   How: gem install ramaze
 ●   git clone http://github.com/manveru/ramaze.git
 ●   Who: Michael Fellinger and Co.




05/19/08                                              39
Why Ramaze?
 ●   Modular (low [loosely] coupling)
 ●   Rack-based
 ●   Easy to use
 ●   A lot of examples (~25) – facebook, rapaste
 ●   Free style of development
 ●   Friendly community - #ramaze
 ●   “All bugs are fixed within 48 hours of reporting.”

05/19/08                                              40
Deployment options
           ●   CGI           ●   Ebb
           ●   FastCGI       ●   Evented Mongrel
           ●   LiteSpeed     ●   Swiftiplied Mongrel
           ●   Mongrel       ●   Thin
           ●   SCGI          ●   Whatever comes along
           ●   Webrick           and fits into the Rack



05/19/08                                                  41
Templating engines
           ●   Amrita2     ●   RedCloth
           ●   Builder     ●   Remarkably
           ●   Erubis      ●   Sass
           ●   Ezmar       ●   Tagz
           ●   Haml        ●   Tenjin
           ●   Liquid      ●   XSLT
           ●   Markaby

05/19/08                                    42
Ezamar




05/19/08            43
Easy to use?
           %w(rubygems ramaze).each {|dep| require dep}

           class MainController < Ramaze::Controller
             def index; quot;Hiquot; end
           end

           class HugsController < Ramaze::Controller
             map '/hug'
             def index; quot;Hug!quot; end
           end

           Ramaze.start :adapter => :thin, :port => 3000

05/19/08                                                   44
Ramaze Usage
           class SmileyController <
           Ramaze::Controller
              map '/smile'

             helper :smiley

             def index
               smiley(':)')
             end
           end


05/19/08                              45
Ramaze Usage (2)
           module Ramaze
             module Helper
               module SmileyHelper
                  FACES = {
                   ':)' => '/images/smile.png',
                   ';)' => '/images/twink.png'}
                  REGEXP = Regexp.union(*FACES.keys)

                  def smiley(string)
                    string.gsub(REGEXP) { FACES[$1] }
                  end
                end
             end
           end

05/19/08                                                46
Alternative Stack
 ●   ORM – Sequel
 ●   Web server – Thin + NginX
 ●   Rack Middleware
 ●   Ramaze framework
 ●   Templates – HAML, Tenjin, Amrita2




05/19/08                                 47
Summary
 ●   Not trying to tell you to not use Rails
 ●   Just keep your mind open
 ●   Sequel is good ORM
 ●   Use Rack – DIY framework, DIY server
 ●   Ramaze is cool and easy to use




05/19/08                                       48

More Related Content

What's hot

Fast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleFast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleRaimonds Simanovskis
 
Spring into rails
Spring into railsSpring into rails
Spring into railsHiro Asari
 
Torquebox @ Raleigh.rb - April 2011
Torquebox @ Raleigh.rb - April 2011Torquebox @ Raleigh.rb - April 2011
Torquebox @ Raleigh.rb - April 2011tobiascrawley
 
Complex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBoxComplex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBoxbobmcwhirter
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011bobmcwhirter
 
Merb Slices
Merb SlicesMerb Slices
Merb Sliceshassox
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudHiro Asari
 
Rails Sojourn: One Man's Journey - Wicked Good Ruby Conference 2013
Rails Sojourn: One Man's Journey - Wicked Good Ruby Conference 2013Rails Sojourn: One Man's Journey - Wicked Good Ruby Conference 2013
Rails Sojourn: One Man's Journey - Wicked Good Ruby Conference 2013Mike Desjardins
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...bobmcwhirter
 
Rails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraRails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraAdam Wiggins
 
Ror Seminar With agilebd.org on 23 Jan09
Ror Seminar With agilebd.org on 23 Jan09Ror Seminar With agilebd.org on 23 Jan09
Ror Seminar With agilebd.org on 23 Jan09Shaer Hassan
 
Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - DeploymentFabio Akita
 
Crash course to the Apache Camel
Crash course to the Apache CamelCrash course to the Apache Camel
Crash course to the Apache CamelHenryk Konsek
 
HTML5 Dev Conf - Sass, Compass & the new Webdev tools
HTML5 Dev Conf - Sass, Compass &  the new Webdev toolsHTML5 Dev Conf - Sass, Compass &  the new Webdev tools
HTML5 Dev Conf - Sass, Compass & the new Webdev toolsDirk Ginader
 
Rails 3 : Cool New Things
Rails 3 : Cool New ThingsRails 3 : Cool New Things
Rails 3 : Cool New ThingsY. Thong Kuah
 

What's hot (20)

Fast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleFast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on Oracle
 
Devignition 2011
Devignition 2011Devignition 2011
Devignition 2011
 
Spring into rails
Spring into railsSpring into rails
Spring into rails
 
Torquebox @ Raleigh.rb - April 2011
Torquebox @ Raleigh.rb - April 2011Torquebox @ Raleigh.rb - April 2011
Torquebox @ Raleigh.rb - April 2011
 
Complex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBoxComplex Made Simple: Sleep Better with TorqueBox
Complex Made Simple: Sleep Better with TorqueBox
 
CouchDB Google
CouchDB GoogleCouchDB Google
CouchDB Google
 
TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011TorqueBox at DC:JBUG - November 2011
TorqueBox at DC:JBUG - November 2011
 
Merb Slices
Merb SlicesMerb Slices
Merb Slices
 
JRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the CloudJRuby, Ruby, Rails and You on the Cloud
JRuby, Ruby, Rails and You on the Cloud
 
Intro to Rack
Intro to RackIntro to Rack
Intro to Rack
 
Rails Sojourn: One Man's Journey - Wicked Good Ruby Conference 2013
Rails Sojourn: One Man's Journey - Wicked Good Ruby Conference 2013Rails Sojourn: One Man's Journey - Wicked Good Ruby Conference 2013
Rails Sojourn: One Man's Journey - Wicked Good Ruby Conference 2013
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
Rails Metal, Rack, and Sinatra
Rails Metal, Rack, and SinatraRails Metal, Rack, and Sinatra
Rails Metal, Rack, and Sinatra
 
Ror Seminar With agilebd.org on 23 Jan09
Ror Seminar With agilebd.org on 23 Jan09Ror Seminar With agilebd.org on 23 Jan09
Ror Seminar With agilebd.org on 23 Jan09
 
Riak with Rails
Riak with RailsRiak with Rails
Riak with Rails
 
Till Vollmer Presentation
Till Vollmer PresentationTill Vollmer Presentation
Till Vollmer Presentation
 
Fisl - Deployment
Fisl - DeploymentFisl - Deployment
Fisl - Deployment
 
Crash course to the Apache Camel
Crash course to the Apache CamelCrash course to the Apache Camel
Crash course to the Apache Camel
 
HTML5 Dev Conf - Sass, Compass & the new Webdev tools
HTML5 Dev Conf - Sass, Compass &  the new Webdev toolsHTML5 Dev Conf - Sass, Compass &  the new Webdev tools
HTML5 Dev Conf - Sass, Compass & the new Webdev tools
 
Rails 3 : Cool New Things
Rails 3 : Cool New ThingsRails 3 : Cool New Things
Rails 3 : Cool New Things
 

Similar to Ruby off Rails (english)

Ruby off Rails (japanese)
Ruby off Rails (japanese)Ruby off Rails (japanese)
Ruby off Rails (japanese)Stoyan Zhekov
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summarydaniel.mattes
 
Ruby w/o Rails (Олександр Сімонов)
Ruby w/o Rails (Олександр Сімонов)Ruby w/o Rails (Олександр Сімонов)
Ruby w/o Rails (Олександр Сімонов)Fwdays
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular applicationmirrec
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Racksickill
 
Camel and JBoss
Camel and JBossCamel and JBoss
Camel and JBossJBug Italy
 
Crank Up Your Apps With TorqueBox
Crank Up Your Apps With TorqueBoxCrank Up Your Apps With TorqueBox
Crank Up Your Apps With TorqueBoxJim Crossley
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteDr Nic Williams
 
Rapid Prototyping FTW!!!
Rapid Prototyping FTW!!!Rapid Prototyping FTW!!!
Rapid Prototyping FTW!!!cloudbring
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyNick Sieger
 
Gridify your Spring application with Grid Gain @ Spring Italian Meeting 2008
Gridify your Spring application with Grid Gain @ Spring Italian Meeting 2008Gridify your Spring application with Grid Gain @ Spring Italian Meeting 2008
Gridify your Spring application with Grid Gain @ Spring Italian Meeting 2008Sergio Bossa
 
JRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell YouJRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell Youelliando dias
 
TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011Lance Ball
 
.NET Architects Day - DNAD 2011
.NET Architects Day - DNAD 2011.NET Architects Day - DNAD 2011
.NET Architects Day - DNAD 2011Fabio Akita
 
09 - Fábio Akita - Além do rails
09 - Fábio Akita - Além do rails09 - Fábio Akita - Além do rails
09 - Fábio Akita - Além do railsDNAD
 

Similar to Ruby off Rails (english) (20)

Ruby off Rails (japanese)
Ruby off Rails (japanese)Ruby off Rails (japanese)
Ruby off Rails (japanese)
 
Railswaycon 2009 - Summary
Railswaycon 2009 - SummaryRailswaycon 2009 - Summary
Railswaycon 2009 - Summary
 
Rack
RackRack
Rack
 
Ruby w/o Rails (Олександр Сімонов)
Ruby w/o Rails (Олександр Сімонов)Ruby w/o Rails (Олександр Сімонов)
Ruby w/o Rails (Олександр Сімонов)
 
Rails Engine | Modular application
Rails Engine | Modular applicationRails Engine | Modular application
Rails Engine | Modular application
 
Building web framework with Rack
Building web framework with RackBuilding web framework with Rack
Building web framework with Rack
 
Camel and JBoss
Camel and JBossCamel and JBoss
Camel and JBoss
 
Crank Up Your Apps With TorqueBox
Crank Up Your Apps With TorqueBoxCrank Up Your Apps With TorqueBox
Crank Up Your Apps With TorqueBox
 
Serverless and React
Serverless and ReactServerless and React
Serverless and React
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Derailing rails
Derailing railsDerailing rails
Derailing rails
 
RubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - KeynoteRubyEnRails2007 - Dr Nic Williams - Keynote
RubyEnRails2007 - Dr Nic Williams - Keynote
 
Rapid Prototyping FTW!!!
Rapid Prototyping FTW!!!Rapid Prototyping FTW!!!
Rapid Prototyping FTW!!!
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
Gridify your Spring application with Grid Gain @ Spring Italian Meeting 2008
Gridify your Spring application with Grid Gain @ Spring Italian Meeting 2008Gridify your Spring application with Grid Gain @ Spring Italian Meeting 2008
Gridify your Spring application with Grid Gain @ Spring Italian Meeting 2008
 
JRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell YouJRuby on Rails Deployment: What They Didn't Tell You
JRuby on Rails Deployment: What They Didn't Tell You
 
Wider than rails
Wider than railsWider than rails
Wider than rails
 
TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011TorqueBox - Ruby Hoedown 2011
TorqueBox - Ruby Hoedown 2011
 
.NET Architects Day - DNAD 2011
.NET Architects Day - DNAD 2011.NET Architects Day - DNAD 2011
.NET Architects Day - DNAD 2011
 
09 - Fábio Akita - Além do rails
09 - Fábio Akita - Além do rails09 - Fábio Akita - Além do rails
09 - Fábio Akita - Além do rails
 

More from Stoyan Zhekov

Padrino - the Godfather of Sinatra
Padrino - the Godfather of SinatraPadrino - the Godfather of Sinatra
Padrino - the Godfather of SinatraStoyan Zhekov
 
Deployment on Heroku
Deployment on HerokuDeployment on Heroku
Deployment on HerokuStoyan Zhekov
 
Push the web with HTML5
Push the web with HTML5Push the web with HTML5
Push the web with HTML5Stoyan Zhekov
 
Foreman - Process manager for applications with multiple components
Foreman - Process manager for applications with multiple componentsForeman - Process manager for applications with multiple components
Foreman - Process manager for applications with multiple componentsStoyan Zhekov
 
Social Network for spare parts
Social Network for spare partsSocial Network for spare parts
Social Network for spare partsStoyan Zhekov
 
Using XMPP Presence stanzas for real-time parking information
Using XMPP Presence stanzas for real-time parking informationUsing XMPP Presence stanzas for real-time parking information
Using XMPP Presence stanzas for real-time parking informationStoyan Zhekov
 
Websockets with ruby
Websockets with rubyWebsockets with ruby
Websockets with rubyStoyan Zhekov
 
Webhooks - glue for the web (japanese)
Webhooks - glue for the web (japanese)Webhooks - glue for the web (japanese)
Webhooks - glue for the web (japanese)Stoyan Zhekov
 
Webhooks - glue for the web
Webhooks - glue for the webWebhooks - glue for the web
Webhooks - glue for the webStoyan Zhekov
 
Microblogging via XMPP (japanese)
Microblogging via XMPP (japanese)Microblogging via XMPP (japanese)
Microblogging via XMPP (japanese)Stoyan Zhekov
 
Microblogging via XMPP
Microblogging via XMPPMicroblogging via XMPP
Microblogging via XMPPStoyan Zhekov
 
Rails Deployment with NginX
Rails Deployment with NginXRails Deployment with NginX
Rails Deployment with NginXStoyan Zhekov
 

More from Stoyan Zhekov (17)

Multirotors
MultirotorsMultirotors
Multirotors
 
ZeroMQ
ZeroMQZeroMQ
ZeroMQ
 
Padrino - the Godfather of Sinatra
Padrino - the Godfather of SinatraPadrino - the Godfather of Sinatra
Padrino - the Godfather of Sinatra
 
Sequel
SequelSequel
Sequel
 
Deployment on Heroku
Deployment on HerokuDeployment on Heroku
Deployment on Heroku
 
Push the web with HTML5
Push the web with HTML5Push the web with HTML5
Push the web with HTML5
 
Foreman - Process manager for applications with multiple components
Foreman - Process manager for applications with multiple componentsForeman - Process manager for applications with multiple components
Foreman - Process manager for applications with multiple components
 
Social Network for spare parts
Social Network for spare partsSocial Network for spare parts
Social Network for spare parts
 
Using XMPP Presence stanzas for real-time parking information
Using XMPP Presence stanzas for real-time parking informationUsing XMPP Presence stanzas for real-time parking information
Using XMPP Presence stanzas for real-time parking information
 
Ruby cooking
Ruby cookingRuby cooking
Ruby cooking
 
Websockets with ruby
Websockets with rubyWebsockets with ruby
Websockets with ruby
 
EventMachine
EventMachineEventMachine
EventMachine
 
Webhooks - glue for the web (japanese)
Webhooks - glue for the web (japanese)Webhooks - glue for the web (japanese)
Webhooks - glue for the web (japanese)
 
Webhooks - glue for the web
Webhooks - glue for the webWebhooks - glue for the web
Webhooks - glue for the web
 
Microblogging via XMPP (japanese)
Microblogging via XMPP (japanese)Microblogging via XMPP (japanese)
Microblogging via XMPP (japanese)
 
Microblogging via XMPP
Microblogging via XMPPMicroblogging via XMPP
Microblogging via XMPP
 
Rails Deployment with NginX
Rails Deployment with NginXRails Deployment with NginX
Rails Deployment with NginX
 

Recently uploaded

IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIES VE
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Alison B. Lowndes
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesBhaskar Mitra
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...Sri Ambati
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeCzechDreamin
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Jeffrey Haguewood
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...Elena Simperl
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCzechDreamin
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityScyllaDB
 

Recently uploaded (20)

IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 

Ruby off Rails (english)

  • 1. Ruby off Rails by Stoyan Zhekov Kyoto, 17-May-2008
  • 2. Table of content ● Coupling and Cohesion ● Common needs for various web apps ● ORM - Sequel (short overview) ● Ruby Web Evolution ● Enter Rack - The Middle Man ● Thin, Ramaze, Tamanegi ● Summary, Extras, Questions 08/05/19 2
  • 3. Coupling and Cohesion (1) ● Cohesion http://en.wikipedia.org/wiki/Cohesion_(computer_science) ● Coupling http://en.wikipedia.org/wiki/Coupling_(computer_science) 08/05/19 3
  • 4. Coupling and Cohesion (2) Applications should be composed of components that show: ● High cohesion – doing small number of things and do them well ● Low (loosely) coupling – easy replace any component with another one 08/05/19 4
  • 5. Coupling and Cohesion (3) Google vs Amazon 08/05/19 5
  • 6. Coupling and Cohesion (4) VS 08/05/19 6
  • 8. Ruby on Rails ● Full Stack – was good. Now? ● Convention over configuration – good? ● DRY – possible? – Generators – Plugins – Libraries 08/05/19 8
  • 9. Life without Rails Is it possible? 08/05/19 9
  • 10. Common needs for various web apps ● Database handling: ORM ● Request processing: CGI env, uploads ● Routing/dispatching: map URLs to classes ● Rendering/views: rendering libraries ● Sessions: persist objects or data 08/05/19 10
  • 11. Database ORM ● Active Record ?= ActiveRecord ● ActiveRecord – a lot of problems – http://datamapper.org/why.html ● Data Mapper – fast, but not ready – No composite keys (when I started using it) – Have it's own database drivers ● Sequel – good, I like it 08/05/19 11
  • 12. ORM – Sequel (Why?) ● Pure Ruby ● Thread safe ● Connection pooling ● DSL for constructing DB queries ● Lightweight ORM layer for mapping records to Ruby objects ● Transactions with rollback 08/05/19 12
  • 13. ORM – Sequel (Core) ● Database Connect require 'sequel' DB = Sequel.open 'sqlite:///blog.db' DB = Sequel.open 'postgres://cico:12345@localhost:5432/mydb' DB = Sequel.open(quot;postgres://postgres:postgres@localhost/my_dbquot;, :max_connections => 10, :logger => Logger.new('log/db.log')) 08/05/19 13
  • 14. ORM – Sequel (Core, 2) DB << quot;CREATE TABLE users (name VARCHAR(255) NOT NULL)quot; DB.fetch(quot;SELECT name FROM usersquot;) do |row| p r[:name] end dataset = DB[:managers].where(:salary => 50..100).order(:name, :department) paginated = dataset.paginate(1, 10) # first page, 10 rows per page paginated.page_count #=> number of pages in dataset paginated.current_page #=> 1 08/05/19 14
  • 15. ORM – Sequel (Model) class Post < Sequel::Model(:my_posts) set_primary_key [:category, :title] belongs_to :author has_many :comments has_and_belongs_to_many :tags after_create do set(:created_at => Time.now) end end 08/05/19 15
  • 16. ORM – Sequel (Model, 2) class Person < Sequel::Model one_to_many :posts, :eager=>[:tags] set_schema do primary_key :id text :name text :email foreign_key :team_id, :table => :teams end end 08/05/19 16
  • 18. Ruby Web Evolution Webrick Mongrel Event Thin Ebb Machine 05/19/08 18
  • 19. Thin web server ● Fast – Ragel and Event Machine ● Clustering ● Unix sockets (the only one I found) - nginx ● Rack-based – Rails, Ramaze etc. ● Rackup files support (follows) 05/19/08 19
  • 20. Thin + Nginx thin start --servers 3 --socket /tmp/thin thin start --servers 3 –post 3000 # nginx.conf upstream backend { fair; server unix:/tmp/thin.0.sock; server unix:/tmp/thin.1.sock; server unix:/tmp/thin.2.sock; } 05/19/08 20
  • 21. The problem Merb Ramaze Sinatra ... How to connect them? CGI Webrick Mongrel Thin 05/19/08 21
  • 22. The solution - Rack Merb Ramaze Sinatra ... Rack – The Middle Man CGI Webrick Mongrel Thin 05/19/08 22
  • 24. What IS Rack? http://rack.rubyforge.org/ By Christian Neukirchen 05/19/08 24
  • 25. WHAT is Rack? ● Specification (and implementation) of a minimal abstract Ruby API that models HTTP – Request -> Response ● An object that responds to call and accepts one argument: env, and returns: – a status, i.e. 200 – the headers, i.e. {‘Content-Type’ => ‘text/html’} – an object that responds to each: ‘some string’ 05/19/08 25
  • 26. This is Rack! class RackApp def call(env) [ 200, {quot;Content-Typequot; => quot;text/plainquot;}, quot;Hi!quot;] end end app = RackApp.new 05/19/08 26
  • 27. Free Hugs http://www.freehugscampaign.org/ 05/19/08 27
  • 28. Hugs Application p 'Hug!' 05/19/08 28
  • 29. ruby hugs.rb %w(rubygems rack).each { |dep| require dep } class HugsApp def call(env) [ 200, {quot;Content-Typequot; => quot;text/plainquot;}, quot;Hug!quot;] end end Rack::Handler::Mongrel.run(HugsApp.new, :Port => 3000) 05/19/08 29
  • 30. Rack::Builder require 'hugs' app = Rack::Builder.new { use Rack::Static, :urls => [quot;/cssquot;, quot;/imagesquot;], :root => quot;publicquot; use Rack::CommonLogger use Rack::ShowExceptions map quot;/quot; do run lambda { [200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hi!quot;]] } end map quot;/hugquot; do run HugsApp.new end } Rack::Handler::Thin.run app, :Port => 3000 05/19/08 30
  • 31. Rack Building Blocks ● request = Rack::Request.new(env) – request.get? ● response = Rack::Response.new('Hi!') – response.set_cookie('sess-id', 'abcde') ● Rack::URLMap ● Rack::Session ● Rack::Auth::Basic ● Rack::File 05/19/08 31
  • 32. Rack Middleware Can I create my own use Rack::... blocks? class RackMiddlewareExample def initialize app @app = app end def call env @app.call(env) end end 05/19/08 32
  • 33. Common needs for various web apps ● Database handling: ORM Sequel ● Request processing: Rack::Request(env) ● Routing/dispatching: Rack 'map' and 'run' ● Rendering/views: Tenjin, Amrita2 ● Sessions: Rack::Session 05/19/08 33
  • 35. rackup hugs.ru require 'hugs' app = Rack::Builder.new { use Rack::Static, :urls => [quot;/cssquot;, quot;/imagesquot;], :root => quot;publicquot; map quot;/quot; do run lambda { [200, {quot;Content-Typequot; => quot;text/plainquot;}, [quot;Hi!quot;]] } end map quot;/hugquot; do run HugsApp.new end } Rack::Handler::Thin.run app, :Port => 3000 05/19/08 35
  • 36. Hugs Service ● rackup -s webrick -p 3000 hugs.ru ● thin start --servers 3 -p 3000 -R hugs.ru ● SCGI ● FastCGI ● Litespeed ● .... 05/19/08 36
  • 37. Rack-based frameworks ● Invisible – thin-based framework in 35 LOC – http://github.com/macournoyer/invisible/ ● Coset – REST on Rack ● Halcyon – JSON Server Framework ● Merb ● Sinatra ● Ramaze ● Rails !? 05/19/08 37
  • 38. Rack coming to Rails ● Rails already have Rack, but: – raw req -> rack env -> Rack::Request ->CGIWrapper -> CgiRequest ● Ezra Zygmuntowicz (merb)'s repo on github – http://github.com/ezmobius/rails – raw req -> rack env -> ActionController::RackRequest – ./script/rackup -s thin -c 5 -e production 05/19/08 38
  • 39. Ramaze ● What: A modular web application framework ● Where: http://ramaze.net/ ● How: gem install ramaze ● git clone http://github.com/manveru/ramaze.git ● Who: Michael Fellinger and Co. 05/19/08 39
  • 40. Why Ramaze? ● Modular (low [loosely] coupling) ● Rack-based ● Easy to use ● A lot of examples (~25) – facebook, rapaste ● Free style of development ● Friendly community - #ramaze ● “All bugs are fixed within 48 hours of reporting.” 05/19/08 40
  • 41. Deployment options ● CGI ● Ebb ● FastCGI ● Evented Mongrel ● LiteSpeed ● Swiftiplied Mongrel ● Mongrel ● Thin ● SCGI ● Whatever comes along ● Webrick and fits into the Rack 05/19/08 41
  • 42. Templating engines ● Amrita2 ● RedCloth ● Builder ● Remarkably ● Erubis ● Sass ● Ezmar ● Tagz ● Haml ● Tenjin ● Liquid ● XSLT ● Markaby 05/19/08 42
  • 44. Easy to use? %w(rubygems ramaze).each {|dep| require dep} class MainController < Ramaze::Controller def index; quot;Hiquot; end end class HugsController < Ramaze::Controller map '/hug' def index; quot;Hug!quot; end end Ramaze.start :adapter => :thin, :port => 3000 05/19/08 44
  • 45. Ramaze Usage class SmileyController < Ramaze::Controller map '/smile' helper :smiley def index smiley(':)') end end 05/19/08 45
  • 46. Ramaze Usage (2) module Ramaze module Helper module SmileyHelper FACES = { ':)' => '/images/smile.png', ';)' => '/images/twink.png'} REGEXP = Regexp.union(*FACES.keys) def smiley(string) string.gsub(REGEXP) { FACES[$1] } end end end end 05/19/08 46
  • 47. Alternative Stack ● ORM – Sequel ● Web server – Thin + NginX ● Rack Middleware ● Ramaze framework ● Templates – HAML, Tenjin, Amrita2 05/19/08 47
  • 48. Summary ● Not trying to tell you to not use Rails ● Just keep your mind open ● Sequel is good ORM ● Use Rack – DIY framework, DIY server ● Ramaze is cool and easy to use 05/19/08 48