SlideShare uma empresa Scribd logo
1 de 48
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 

Mais de 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
 

Último

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 

Último (20)

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 

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