Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Sinatra: a Micro-framework for Web Applications<br />Kevin Reiss<br />Office of Library Services<br />
  2. 2. What is Sinatra?<br />A web application micro-framework<br />Minimal stack framework<br />A ruby Domain Specific Language (DSL)<br />A ruby “rack” application<br />A ruby “gem”<br />Gems are the ruby equivalent of<br />Perl modules<br />PHP PEAR<br />Managing gems is very easy<br />- Gem install sinatra is all you need<br />
  3. 3. Why Sinatra?<br />It is lightweight<br />Built on the rack “gem”<br />Rack is a generic ruby library for building web applications<br />It is nicely pluggable within other applications<br />Rails3 has featured support for sinatra apps as middleware<br />Good tool for creating REST-like interfaces <br />Supports many different template syntaxes<br />It has it’s own web server: default thin<br />Convention over Configuration: the Ruby Framework Way<br />
  4. 4. Hello World?<br /># hello_world.rb<br />require 'sinatra' <br /># a single sinatra block with a return value<br />get '/' do <br /> 'Hello world!' # return the string “Hello World”<br />end<br />To run the app:<br />> ruby hellow_world.rb<br />
  5. 5. Second Hello World App<br />Sinatra configuration<br />Sinatra routes<br />Sinatra helpers<br />Watch your Request values<br />
  6. 6. Sinatra: Helpers, Routes, and Blocks<br />helpers do<br /> def format_title(name)<br />name.capitalize # your return value<br /> end<br />End<br />get '/library/:department' do<br /> @title = "#{format_title(params[:department])} Department".strip<br />haml :department # invoke a template<br />end<br />
  7. 7. Views with Sinatra<br />Standard convention<br />Views always go in the “views” directory<br />Templates marked “layout.ext” are automatically used as wrappers for “content-like templates” like “mycustomdisplay.ext”<br />Static files (stylesheets, images, etc.) are served up out of the “public” directory<br />Support a wide variety of template options to generate XHTML, HTML5, XML, etc. <br />Can easily define custom formats<br />Integration with RACK makes it easy to deal with the parts of an HTTP Request<br />
  8. 8. HAML Templates<br />HAML – HTML Templates for the Lazy<br />Also bundled with a CSS Template: SASS<br />%html{:"xml:lang"=>"en"}<br /> %head<br /> %title My Library Web: #{@title}<br /> %link{:type=>"text/css",:rel=>"stylesheet",:media=>"screen",:href=>"/stylesheet.css"}<br /> %body<br /> #pageContent<br /> #header<br /> %ul.nav<br /> %li<br /> %a{:href=>"/"} Home<br /> #body<br /> =yield<br /> #footer<br /> %p Office of Library Services<br />
  9. 9. Where is the Registry?<br />Set configuration options and constants easily<br />Sinatra’s integration with rack makes it easy to access and manipulate the contents of the http.request associated<br />
  10. 10. What about Testing?<br />Unit tests are built into rack<br />set :environment, :test<br />class LibraryTest < Test::Unit::TestCase<br /> def test_it_says_hello_world<br /> browser =<br />browser.get '/'<br /> assert browser.last_response.ok?<br />assert_equal '<h1>Hello World!</h1>', browser.last_response.body<br /> end<br /> def test_it_says_hello_to_a_department<br /> browser =<br />browser.get '/library/ill'<br /> assert browser.last_response.body.include?('ill')<br /> end<br />end<br />
  11. 11. Deployment Options<br />Launch Locally with ruby thin web server<br />This is built into the application<br />Publish directly to heroku<br />Launch via apache<br />proxy_balancer module<br />passenger module<br />ngnix<br />
  12. 12. Heroku Features<br />Read-only filesystem<br />Installed as a ruby gem<br />Integrated with git and ssh<br />Free for low-volume applications<br />Add-ons to expand functionality<br />Memcache<br />Databases,DNS, <br />heroku logs<br />Good documentation<br />Not just sinatra, rails and any other rack-based application<br />Similar to the “Google App Engine”<br />
  13. 13. Heroku Deployment <br />Ruby Platform as a Service –<br />Rails, Sinatra, generic Rack Applications<br />Integrated directly with git version to control<br />To Deploy:<br />create “” file – defines your rack application<br />create .gems file – defines libraries your app requires<br />git init<br />git add .<br />git commit –m “Initial Commit”<br />heroku create<br />git push heroku master<br />
  14. 14. Thin Server Configuration<br />--- <br />chdir: /home/kreiss/ruby/lookup/<br />rackup: /home/kreiss/ruby/lookup/<br />environment: production<br />address:<br />port: 5000<br />timeout: 30<br />log: /home/kreiss/ruby/lookup/log/thin.log<br />pid: /home/kreiss/ruby/lookup/tmp/pids/<br />max_conns: 1024<br />max_persistent_conns: 512<br />require: []<br />wait: 30<br />servers: 3<br />daemonize: true<br />
  15. 15. An Actual Application<br />Standard number checker<br />Wrapper for our web services provided by our ILS<br />Embed this as a web service in other application/scripts<br />
  16. 16. REST-Like Interface<br />Output multiple formats<br />JSON<br />Custom XML with builder gem<br />XML parsing handled by the nokogiri ruby gem<br />
  17. 17. More Complicated Sample Route<br />get '/:campus/isbn/:isbn' do<br />lookup_base = is_valid_sublibrary(params[:campus])<br /> @number = is_valid_isbn(params[:isbn])<br /> @type = "ISBN"<br /> @title = “#{@type}: #{@number} for campus #{lookup_base}"<br /> @items = standard_lookup(@number, "bath.isbn", lookup_base)<br /> if params[:format] == "xml"<br /> builder :bib<br />elsifparams[:format] == "json"<br /> @items.to_json<br /> else<br />haml :standard<br /> end<br />end<br />
  18. 18. Heroku Deployment<br />486 curl<br />487 curl<br />488 curl<br />489 curl<br />490 curl<br />491 curl<br />492 curl<br />493 curl<br />494 curl<br />495 curl<br />496 curl<br />497 curl<br />498 curl<br />
  19. 19. Gotchas<br />Ruby 1.8 v. Ruby 1.9<br />Heroku supports both<br />Gems need to be called differently in ruby 1.8<br />Name your gems sensibly – annoying rubyism<br />Dalli: name for memcache gem<br />Nokogiri: name for xml processing gem<br />Sinatra vs. Sinatra Base<br />If you plan to use as middleware in another ruby app you need to define your application with sinatra base <br />
  20. 20. Other frameworks in Libraries<br />Rails (Blacklight)<br />PHP Zend (Omeka)<br />Django (Solr front-end)<br />Xerxes (Metasearch)<br />
  21. 21. Drupal Platform <br />Services<br />Views datasource<br />Drupal Workflow building capabilities<br />Workflow module<br />New workbench toolkit<br />D7 Rest Server<br />
  22. 22. Code4lib Report<br />Looks of repository middleware<br />
  23. 23. Code4libNYC <br />Suggestion to host day long event<br />