O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sinatra: Up and Running

Book Description

Take advantage of Sinatra, the Ruby-based web application library and domain-specific language used by Heroku, GitHub, Apple, Engine Yard, and other prominent organizations. With this concise book, you will quickly gain working knowledge of Sinatra and its minimalist approach to building both standalone and modular web applications.

Sinatra serves as a lightweight wrapper around Rack middleware, with syntax that maps closely to functions exposed by HTTP verbs, which makes it ideal for web services and APIs. If you have experience building applications with Ruby, you’ll quickly learn language fundamentals and see under-the-hood techniques, with the help of several practical examples. Then you’ll get hands-on experience with Sinatra by building your own blog engine.

  • Learn Sinatra’s core concepts, and get started by building a simple application
  • Create views, manage sessions, and work with Sinatra route definitions
  • Become familiar with the language’s internals, and take a closer look at Rack
  • Use different subclass methods for building flexible and robust architectures
  • Put Sinatra to work: build a blog that takes advantage of service hooks provided by the GitHub API

Table of Contents

  1. Preface
    1. Who This Book Is For
    2. How This Book Is Organized
      1. The Basics
      2. Digging Deeper
      3. Hands On
    3. Conventions Used in This Book
    4. Using Code Examples
    5. Safari® Books Online
    6. How to Contact Us
  2. 1. Taking the Stage
    1. Characteristics of Sinatra
      1. Is It a Framework?
      2. Does It Implement MVC?
      3. Who’s Using It?
      4. What Does a Production Project Look Like?
      5. What’s the Catch?
      6. Are These Skills Transferrable?
    2. Installation
      1. Thin
    3. Up and Running
      1. Breaking Down the Syntax
      2. Testing with Telnet
    4. Rock, Paper, Scissors or “The Shape of Things to Come”
    5. Summary
  3. 2. Fundamentals
    1. Routing
      1. Hypertext Transfer Protocol
      2. Verbs
      3. Common Route Definition
      4. Many URLs, Similar Behaviors
      5. Routes with Parameters
      6. Routes with Query String Parameters
      7. Routes with Wildcards
      8. The First Sufficient Match Wins
      9. Routes with Regular Expressions
      10. Halting a Request
      11. Passing a Request
      12. Redirecting a Request
    2. Static Files
    3. Views
      1. Inline Templates
      2. External View Files
        1. External Views in Subfolders
      3. Passing Data into Views
    4. Filters
    5. Handling Errors
      1. 404 Not Found
      2. 500 Internal Server Error
    6. Configuration
    7. HTTP Headers
      1. The headers Method
      2. Exploring the request Object
    8. Caching
      1. Setting Headers Manually
      2. Settings Headers via expires
      3. ETags
        1. Generating ETags
        2. Weak ETags
    9. Sessions
      1. Destroying a Session
    10. Cookies
    11. Attachments
    12. Streaming
      1. Keeping the Connection Open
      2. Finite Streaming
    13. Summary
  4. 3. A Peek Behind the Curtain
    1. Application and Delegation
      1. The Inner Self
      2. Where Does get Come From?
      3. Exploring the Implementation
    2. Helpers and Extensions
      1. Creating Sinatra Extensions
      2. Helpers
        1. Helpers Without Modules
      3. Combining Helpers and Extensions
    3. Request and Response
      1. Rack
      2. Sinatra Without Sinatra
    4. Rack It Up
      1. Middleware
      2. Sinatra and Middleware
    5. Dispatching
      1. Dispatching Redux
      2. Changing Bindings
    6. Summary
  5. 4. Modular Applications
    1. Subclassing Sinatra
      1. Running Modular Applications
        1. Using run!
        2. With rackup
      2. About Settings
        1. Settings and Classes
      3. Subclassing Subclasses
        1. Route Inheritance
        2. Architecture
      4. Dynamic Subclass Generation
    2. Better Rack Citizenship
      1. Chaining Classes
        1. Middleware Chain
        2. Cascade
        3. With a Router
      2. On Return Values
      3. Using Sinatra as Router
    3. Extensions and Modular Applications
      1. Helpers
      2. Extensions
    4. Summary
  6. 5. Hands On: Your Own Blog Engine
    1. Workflow Concept
      1. File-Based Posts
      2. Git for Distribution
      3. Semistatic Pages
    2. The Implementation
      1. Displaying Blog Posts
        1. Rendering Markdown
        2. Generating articles
        3. Adding an index
        4. Adding a basic layout
      2. Git Integration
        1. Regenerating content
        2. Pulling changes
        3. Proper cache headers
      3. Glueing Everything Together
        1. Rack It Up!
        2. Setting it up on GitHub
        3. Setting it up on Bitbucket
        4. Using a post-receive hook
        5. What about Heroku?
    3. Summary
  7. About the Authors
  8. Copyright