Intro to Sinatra (Thu Jan 27, lect 4) | previous | next | slides |

Sinatra is a ruby application server

Logistics

  • What to do if you are unable or late submitting an assignment
  • What to do if you need an excused absence
  • Remember we are in person next week. Room: Shapiro Science Center 014

What is Sinatra?

  • A ruby library
  • Easiest way to create a simple web server
  • Much smaller and easy to understand than rails (also less powerful)
  • What is Sinatra according to their own Sinatra Intro

The Sinatra app

  • Review Web Server (or http server) vs. Application Server
  • Single main ruby program
  • Filters (before, after)
  • Discuss the role of Rack and Rackup
  • Logging
  • Browser caching
  • Configuration
  • Running sinatra for different environments

APP_ENV=production ruby my_app.rb
  • Testing - using minitest
  • Modular vs. Classic Style Apps

Routing

  • One method for each “route”
  • Rendering of Views
  • Routes with parameters
  • Routes with patterns
  • Sessions to cary information from one request to the next
  • Nice pattern matching to generalize similar routes
  • At the end of the method, either render html, json, or redirect
  • Passing parameters to view templates
  • Each of the route methods are “kind of like” a controller

Data

  • Sinatra doesn’t do anything special about working with the database
  • Whatever database access you prefer will work
  • Our examples use ActiveRecord as a library to interface with the database
  • But there are other ones that work just as well
  • Typical design
    1. encapsulates the data access in a class
    2. one class for each table or entity
    3. often referred to as a model class

Views

  • Erb is a very common templating language
  • Look at ERB Cribsheet
  • They are what is generated at the end of the route method
  • In addition to erb there are tons of others
  • What they have in common: they all generate data (in the end) that is returned as the result of an HTTP request

Thank you. Questions?  (random Image from picsum.photos)