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

As Time Goes By

As Time Goes By

Really old Sinatra slides I used for a tech talk about Sinatra in 2008

Corey Donohoe

March 18, 2012
Tweet

More Decks by Corey Donohoe

Other Decks in Technology

Transcript

  1. Hello World require 'rubygems' require 'sinatra' helpers do def greeting

    haml :greeting end end template :greeting do "%h2 Hello World" end get('/') do greeting end Thursday, May 14, 2009
  2. ["/foo", "/bar", "/baz"].each do |path| get path do "You've reached

    me at #{request.path_info}" end end Thursday, May 14, 2009
  3. helpers do include Rack::Utils alias_method :h, :escape_html def localhost(suffix) port_part

    = case request.scheme when "http" request.port == 80 ? "" : ":#{request.port}" when "https" request.port == 443 ? "" : ":#{request.port}" end "#{request.scheme}://#{request.host}#{port_part}#{suffix}" end end Thursday, May 14, 2009
  4. set :foo, 'bar' set :baz, Proc.new { "Hello " +

    foo } get '/baz' do "baz is set to " + options.baz end Thursday, May 14, 2009
  5. module Hancock class ConfigurationError < StandardError; end class App <

    Sinatra::Default enable :sessions set :sreg_params, [:email, :first_name, :last_name, :internal] set :provider_name, 'Hancock SSO Provider!' set :do_not_reply, nil register Sinatra::Hancock::Defaults register Sinatra::Hancock::Sessions register Sinatra::Hancock::Users register Sinatra::Hancock::OpenIDServer end end Thursday, May 14, 2009
  6. module Sinatra module Hancock module Sessions def self.sessions_template(file) template =

    File.expand_path(File.dirname(__FILE__)+'/views/sessions') File.read("#{template}/#{file}.haml") end module Helpers def session_user session['user_id'].nil? ? nil : ::Hancock::User.get(session['user_id']) end end def self.registered(app) app.helpers(Sinatra::Hancock::Sessions::Helpers) app.template(:unauthenticated) { sessions_template ('unauthenticated') } app.get '/sso/login' do ensure_authenticated end app.post '/sso/login' do @user = ::Hancock::User.authenticate(params['email'], params['password']) if @user session['user_id'] = @user.id end ensure_authenticated redirect session['return_to'] || '/' end app.get '/sso/logout' do session.clear redirect '/' end end end end end Thursday, May 14, 2009
  7. module Hancock class ConfigurationError < StandardError; end class App <

    Sinatra::Default enable :sessions set :sreg_params, [:email, :first_name, :last_name, :internal] set :provider_name, 'Hancock SSO Provider!' set :do_not_reply, nil register Sinatra::Hancock::Defaults register Sinatra::Hancock::Sessions register Sinatra::Hancock::Users register Sinatra::Hancock::OpenIDServer end end OMG LOOK WHAT I MADE! you can share it with your buddies Thursday, May 14, 2009
  8. under the hood yo dawg, i heard you were into

    rack Thursday, May 14, 2009
  9. #!/usr/bin/env rackup require File.dirname(__FILE__) + '/lib/setup' require 'gateway/app' require 'migration/app'

    use Rack::Static, :urls => ["/css", "/img", "/js"], :root => "public" map "/gateway/" do use EySso run Gateway::App end map "/migration/" do use Rack::ShowExceptions if ENV["RACK_ENV"] == "production" use EySso do |sso| sso.only_staff! end end run Migration::App end map "/" do app = lambda do |env| [404, {"Content-Type" => "text/plain", "Content-Length" => "9"}, ["Not found"]] end run app end Thursday, May 14, 2009
  10. #!/usr/bin/env rackup require File.dirname(__FILE__) + '/lib/setup' require 'gateway/app' require 'migration/app'

    use Rack::Static, :urls => ["/css", "/img", "/js"], :root => "public" map "/gateway/" do use EySso run Gateway::App end map "/migration/" do use Rack::ShowExceptions if ENV["RACK_ENV"] == "production" use EySso do |sso| sso.only_staff! end end run Migration::App end map "/" do app = lambda do |env| [404, {"Content-Type" => "text/plain", "Content-Length" => "9"}, ["Not found"]] end run app end Thursday, May 14, 2009
  11. http { ... server { listen 80; server_name hancock.atmos.org root

    /data/hancock; passenger_enabled on; # <--- These lines passenger_base_uri /hancock; # <--- added. } ... } nginx config Thursday, May 14, 2009
  12. <VirtualHost *:80> ServerName hancock.atmos.org DocumentRoot /data/hancock/current/public RackEnv 'production' <Directory /data/hancock/current/public>

    Options FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> Apache 2 Vhost Config Thursday, May 14, 2009
  13. #!/usr/bin/env ruby require "irb" require 'rubygems' require 'twitrsvp' DataMapper.setup(:default, "mysql://atmos:s3cr3t@localhost/twitrsvp_production")

    ENV['TWIT_RSVP_READKEY'] = '4the' ENV['TWIT_RSVP_READSECRET'] = 'lulz' include ::TwitRSVP if __FILE__ == $0 IRB.start(__FILE__) else # check -e option if /^-e$/ =~ $0 IRB.start(__FILE__) else IRB.setup(__FILE__) end end script/console Thursday, May 14, 2009
  14. require "rack/test" class HomepageTest < Test::Unit::TestCase include Rack::Test::Methods def app

    MyApp.new end def test_redirect_logged_in_users_to_dashboard authorize "bryan", "secret" get "/" follow_redirect! assert_equal "http://example.org/redirected", last_request.url assert last_response.ok? end end Thursday, May 14, 2009
  15. That’s it! :D ✤ http://github.com/sinatra/sinatra ✤ http://github.com/chneukirchen/rack ✤ http://github.com/atmos/hancock ✤

    http://github.com/atmos/hancock-client ✤ http://github.com/atmos/hancock-client-rails ✤ http://github.com/brynary/rack-bug ✤ http://github.com/foca/integrity Thursday, May 14, 2009
  16. gem 'hancock', '~>0.0.1' require 'hancock' DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/development.db") Hancock::App.set :views, 'views'

    Hancock::App.set :public, 'public' Hancock::App.set :environment, :production Hancock::App.set :provider_name, 'Example SSO Provider' Hancock::App.set :do_not_reply, '[email protected]' Sinatra::Mailer.config = { :host => 'smtp.example.com', :port => '25', :user => 'sso', :pass => 'lolerskates', :auth => :plain # :plain, :login, :cram_md5, the default is no auth :domain => "example.com" # the HELO domain provided by the client to the server } run Hancock::App config.ru Thursday, May 14, 2009
  17. require File.join(File.dirname(__FILE__), 'boot') require 'hancock' Rails::Initializer.run do |config| config.gem 'hancock',

    :lib => 'hancock' config.middleware.use 'Hancock::Client' # all your other normal stuff end config/environment.rb Thursday, May 14, 2009