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

Publisher resources

View/Submit Errata

Table of contents

  1. Sinatra: Up and Running
  2. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. About the Authors
  10. SPECIAL OFFER: Upgrade this ebook with O’Reilly
  11. Copyright

Product information

  • Title: Sinatra: Up and Running
  • Author(s): Alan Harris, Konstantin Haase
  • Release date: November 2011
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449323974