When people speak of Ruby web development, it has historically been in reference to the opinionated juggernaut that is Rails. This is certainly not an unfounded association; Hulu, Yellow Pages, Twitter, and countless others have relied on Rails to power their (often massive) web presences, and Rails facilitates that process with zeal.
Why, then, are people so interested in Sinatra, the tiny little domain-specific language that could?
Rails was a breath of fresh air to many developers exhausted by the âold waysâ; Sinatra enters the arena with a similar game-changer: a beautifully minimalistic, âIâll get out of your wayâ approach. No generators, no complex folder hierarchies, and a brief yet expressive syntax that maps closely to the functionality exposed by the Hypertext Transfer Protocol verbs.
In short, Sinatra is for classy web development.
Our goal is to provide the core concepts and accompanying examples to help you feel comfortable using Sinatra as quickly as possible. By the end, you should have a working knowledge of Sinatra and how it fits into the larger Ruby web development ecosystem. You should know when Sinatra will get the job done quickly and when it might be better to lean on Rails, Padrino, or similar frameworks. You should also have a better sense of the internals of Sinatra, as well as the Rack specification and accompanying gem.
Note
No worries, we wonât short-change you on the reference aspects; you can certainly return to this book to recall how to perform daily tasks without excessive searching.
With that said, letâs get you up and running.
Sinatra: Up and Running is for developers with some Ruby experience under their belt, and ideally some web development experience as well. Some concepts that are core to web development (the HTTP specification, HTML, CSS, etc.) are critical to understanding how to be productive with Sinatra; we recommend that you have at least a passing familiarity with these concepts to make the experience a little easier.
If youâve written some web applications before but not specifically in Ruby, thatâs no problem. Our discussion of other tools is primarily limited to comparing and contrasting with how Sinatra does things.
Our plan is to try to address the needs of several distinct camps of readers: those with a Ruby web development background in Rails but no experience with Sinatra, as well those who are familiar with Sinatra but would like a tour of its internals and philosophy. Where possible, weâd also like to help bring developers without direct web experience into the fold. Pretty tricky if you think about it, but weâll do our best to speak to all the seats in the house by the conclusion.
Given these stated goals, weâve divided the materials into three sections. The beginning of the book focuses on the bare minimum you need to know to work with Sinatra. Here youâll find the fundamentals, such as how to craft routes, manage sessions, create views, and so on. Immediately afterward, we will lift the veil and examine some of the techniques behind the scenes, which will open up a world of possibilities for implementation and integration. Finally, we will wrap up the discussion with some practical applications, including developing a GitHub-powered blog.
Note
Weâve also tried to inject as much related information as possible for the various topics covered within, ranging from gotchas to other resources where one could explore subtopics in greater depth.
One aside: if you encounter a section explaining information youâre already well-versed in, please bear with us as other readers may benefit from the discussion. We strive to keep the pace brisk, but weâd prefer not to leave any folks out.
Sinatra: Up and Running is organized as follows.
Chapter 1, Taking the Stage, serves as a high-level introduction to some of the core concepts in Sinatra. It also discusses how to install the Sinatra gem, and walks through the creation of a simple application.
Chapter 2, Fundamentals, covers the different features of Sinatra, such as route definitions, creating views, managing sessions, and so on. It also serves as something of a reference chapter, with each topic discussed in granular fashion.
Note
If youâve already built some Sinatra applications of your own and are fairly comfortable doing so, you can likely just skim through Chapters 1 and 2, although the newest release of Sinatra (version 1.3.1) contains a number of changes that are worth exploring (including support for the HTTP PATCH verb, streaming, etc.).
In Chapter 3, A Peek Behind the Curtain, we discuss the internals of Sinatra and its implementation; this includes coverage of Rack, building middleware, and other topics that clarify what really happens under the hood.
Chapter 4, Modular Applications, covers the various approaches available for subclassing Sinatra, allowing you to create significantly more flexible and robust architectures.
Note
If youâve built some Sinatra applications but have never really explored the source code (or written Rack applications directly), this section will help to flesh out your knowledge. Understanding the modular application approach is critical to taking full advantage of what Sinatra offers.
In Chapter 5, Your Own Blog Engine, we put the theory into application and create a Markdown-powered blog that takes advantage of the service hooks provided by the GitHub API.
The following typographical conventions are used in this book:
- Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values determined by context.
Tip
This icon signifies a tip, suggestion, or general note.
Caution
This icon indicates a warning or caution.
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless youâre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from OâReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your productâs documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: âSinatra: Up and Running by Alan Harris and Konstantin Haase (OâReilly). Copyright 2012 OâReilly Media, Inc., 978-1-449-30423-2.â
If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.
Note
Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.
OâReilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from OâReilly and other publishers, sign up for free at http://my.safaribooksonline.com.
Please address comments and questions concerning this book to the publisher:
OâReilly Media, Inc. |
1005 Gravenstein Highway North |
Sebastopol, CA 95472 |
800-998-9938 (in the United States or Canada) |
707-829-0515 (international or local) |
707-829-0104 (fax) |
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
http://shop.oreilly.com/product/0636920019664.do |
To comment or ask technical questions about this book, send email to:
bookquestions@oreilly.com |
For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Get Sinatra: Up and Running now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.