Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby microframeworks

Peter
December 27, 2012

Ruby microframeworks

Small talk about ruby microframewarks. Cuba Sinatra and Rails-Api

Peter

December 27, 2012
Tweet

More Decks by Peter

Other Decks in Programming

Transcript

  1. Rails stack controllers models views database mails tasks assets logging

    tests authentications plugins generators routing пятница, 28 декабря 12 г.
  2. Padrino (alive) Merb (dead) Hobo (dead) Camping (dead) Remaze (alive)

    Cuba (half-alive) Sinatra (alive) Rails-api (just borned) Solutions пятница, 28 декабря 12 г.
  3. Rack middleware Security Testing with Cutest and Capibara Own plugins

    $ gem install cuba пятница, 28 декабря 12 г.
  4. Hello, Cuba! $ gem install cuba require "cuba" require "cuba/render"

    Cuba.plugin Cuba::Render Cuba.define do on get do on "hello" do res.write render("hello.haml", content: "Hello, Cuba") end on root do res.redirect "/hello" end end end пятница, 28 декабря 12 г.
  5. $ gem install cuba require "ohm" Ohm.connect(host: "localhost", port: "6379")

    class Blog < Ohm::Model attribute :title attribute :text end attribute set list reference collection пятница, 28 декабря 12 г.
  6. • Fast • Simple • Extendable Pros and Cons 3ms

    vs Rails’ 13ms Che pleased пятница, 28 декабря 12 г.
  7. Rack middleware Routing Conditions Filters Helpers Template engines %w(erb haml

    sass less markdown etc) Security Cache Error handling No database support No restful routing $ gem install sinatra пятница, 28 декабря 12 г.
  8. Libraries Modular and Classic Apps Sinatra as Middleware (Rails/Cuba/ Camping)

    Api Endpoint $ gem install sinatra пятница, 28 декабря 12 г.
  9. $ gem install sinatra # classic style require 'sinatra' require

    'sinatra/linkblocker' block_links_from 'digg.com' get "/events/recent.json" do Event.recent.to_json end # modular style require 'sinatra/base' require 'sinatra/diggblocker' module Posts class Application < Sinatra::Base register Sinatra::LinkBlocker block_links_from 'digg.com' get "/" do erb :index end end end пятница, 28 декабря 12 г.
  10. $ gem install sinatra require 'sinatra/base' class LoginScreen < Sinatra::Base

    enable :sessions get('/login') { haml :login } post('/login') do if params[:name] == 'admin' && params[:password] == 'admin' session['user_name'] = params[:name] else redirect '/login' end end end class MyApp < Sinatra::Base # middleware will run before filters use LoginScreen before do unless session['user_name'] halt "Access denied, please <a href='/login'>login</a>." end end get('/') { "Hello #{session['user_name']}." } end # config/routes.rb mount API::Endpoints, :at => "/api" пятница, 28 декабря 12 г.
  11. Fast Simple Overlaped with Rails +HTTP -HTML Many lines not

    for pros Doens’t support some gems Pros and Cons пятница, 28 декабря 12 г.
  12. use ActionDispatch::Static use Rack::Lock use Rack::Runtime use ActionDispatch::RequestId use Rails::Rack::Logger

    use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag run ApiApp::Application.routes use ActionDispatch::Static use Rack::Lock use Rack::Runtime use Rack::MethodOverride use ActionDispatch::RequestId use Rails::Rack::Logger use ActionDispatch::ShowExceptions use ActionDispatch::DebugExceptions use ActionDispatch::RemoteIp use ActionDispatch::Reloader use ActionDispatch::Callbacks use ActiveRecord::ConnectionAdapters::ConnectionManagement use ActiveRecord::QueryCache use ActionDispatch::Cookies use ActionDispatch::Session::CookieStore use ActionDispatch::Flash use ActionDispatch::ParamsParser use ActionDispatch::Head use Rack::ConditionalGet use Rack::ETag use ActionDispatch::BestStandardsSupport run Application::Application.routes $ gem install rails-api пятница, 28 декабря 12 г.
  13. $ gem install rails-api find 10 differences AbstractController::Layouts AbstractController::Translation AbstractController::AssetPaths

    Helpers HideActions UrlFor Redirecting Rendering Renderers::All ConditionalGet RackDelegation Caching MimeResponds ImplicitRender Cookies Flash RequestForgeryProtection ForceSSL Streaming DataStreaming RecordIdentifier HttpAuthentication::Basic::ControllerMethods HttpAuthentication::Digest::ControllerMethods HttpAuthentication::Token::ControllerMethods AbstractController::Callbacks Rescue ActionController::UrlFor ActionController::Redirecting ActionController::Rendering ActionController::Renderers::All ActionController::ConditionalGet ActionController::ForceSSL ActionController::RackDelegation ActionController::DataStreaming AbstractController::Callbacks ActionController::Instrumentation ActionController::Rescue пятница, 28 декабря 12 г.
  14. Benchmarks Framework | Requests/s ----------|----------- Rack | 2217 Cuba |

    1975 Sinatra | 950 Rails | 535 Framework | LOC ----------|----------- Cuba | 152 Sinatra | 1476 Rails(AP) | 13181 пятница, 28 декабря 12 г.