http://github.com/RafeKettler/sinatra-for-beginners
Sinatra is a simple web framework written in Ruby. This talk assumes some familiarity with Ruby (though I’ll explain all code line by line).
We’ll start with a crash course on HTTP and basic concepts for web programming, then introduce features of Sinatra incrementally, including rendering HTML, sessions, and storing data in a database.
(Ask questions any time)
- HTTP: Hypertext Transfer Protocol
- Hypertext: HTML!
- Transfer: Communication!
- Protocol: standard format and method for communication!
- The protocol that dictates how to communicate on the internet (there are other protocols that do the same, but HTTP is the most common)
In HTTP, clients (think a web browser) make requests to the server, which processes the request and sends a response.
- Requests have a method: GET or POST (some others, but these are the most notable and all you really need)
- They have a destination: a URL
- They might have parameters:
- Through GET parameters in the URL querystring (i.e., http://example.com/?q=hello&r=world)
- Through POST parameters in the request body
- They have a body
- They have headers – Used to specify low-level metadata to the HTTP server
- Used by extensions to HTTP
- Sometimes used by web services (APIs)
- What the server sends back to the client. On your browser, it’s an HTML page, or an image, or a JavaScript, or something else…
- Responses have status codes (201 OK, 301 redirect, 404 not found, 500 internal server error)
- Responses also have headers, which serve a similar purpose as in requests
- Responses have a body, which is just data
- Your job, as a web programmer, is to process requests and render appropriate responses. You’re the middle man.
- This usually gets divided into a few tasks: figuring out what data to display based on the request, and presenting the data. In Sinatra, you write Ruby code to do the first part and can use any number of templating engines to render HTML for the second part.
A member of the Brat Pack – wrong
A full stack, kitchen-sink included web framework – wrong
A simple, minimal web framework with a surprising number of built-in goodies – right!
Import it. Turn on serving static files from “static/” (like JavaScript, CSS, or images). If you have a file “static/foo.js”, it will be accessible at http://example.com/foo.js.
Define some routes. Routes are paths on the server – they’re the destinations that requests ask for. These get mapped to URLS, but are relative to your server’s IP or domain name (so http://example.com/42 is /42 to Sinatra).
Keep track of user state from request to request (using cookies). For nonpersistent state
We’ll demo a system called ERB (Embedded RuBy) that is in the Ruby standard library. Put files in the views/ folder with the extension .erb
Using datamapper, a Ruby ORM (an object-oriented abstraction over a relational database)