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