Simple Counter App - Sinatra Typing CST Test
Loading…
Simple Counter App — Sinatra Code
Demonstrates a simple Sinatra app with a counter using in-memory state and route handlers.
require 'sinatra'
count = 0
get '/counter' do
"<h2>Counter: #{count}</h2>
<form method='post' action='/increment'>
<button type='submit'>+</button>
</form>
<form method='post' action='/decrement'>
<button type='submit'>-</button>
</form>
<form method='post' action='/reset'>
<button type='submit'>Reset</button>
</form>"
end
post '/increment' do
count += 1
redirect '/counter'
end
post '/decrement' do
count -= 1
redirect '/counter'
end
post '/reset' do
count = 0
redirect '/counter'
end
# Run with: ruby app.rb -p 4567Sinatra Language Guide
Sinatra is a lightweight, domain-specific Ruby web framework for quickly creating web applications and APIs with minimal boilerplate, emphasizing simplicity and flexibility.
Primary Use Cases
- ▸Building simple web applications quickly
- ▸Creating RESTful APIs
- ▸Prototyping services and applications
- ▸Microservices for modular architectures
- ▸Lightweight web apps that don’t need full Rails stack
Notable Features
- ▸DSL for defining HTTP routes and handlers
- ▸Rack-based middleware support
- ▸Built-in request and response handling
- ▸Template rendering with ERB, Haml, or Slim
- ▸Lightweight, fast, and minimalistic
Origin & Creator
Created by Blake Mizerany in 2007 as a minimal Ruby web framework alternative to Rails.
Industrial Note
Widely used in lightweight web services, microservices, RESTful APIs, and quick prototypes, often in startups or for internal tooling.
Quick Explain
- ▸Sinatra provides a DSL for defining HTTP routes and their handlers in Ruby.
- ▸Ideal for small web applications, microservices, and APIs.
- ▸Supports multiple request types (GET, POST, PUT, DELETE) out of the box.
- ▸Integrates easily with Rack-compatible middleware and libraries.
- ▸Encourages convention over configuration for rapid development.
Core Features
- ▸Routing - map HTTP methods and paths to Ruby blocks
- ▸Middleware support - use Rack-compatible middleware
- ▸Request/Response objects - access headers, params, and body
- ▸Template rendering - embed Ruby in HTML views
- ▸Error handling and filters - before, after, and error blocks
Learning Path
- ▸Learn basic Ruby syntax
- ▸Understand Rack and middleware
- ▸Create simple Sinatra routes and handlers
- ▸Use templates for dynamic HTML
- ▸Integrate with databases and external services
Practical Examples
- ▸Simple 'Hello World' web app
- ▸JSON REST API with CRUD endpoints
- ▸Rendering HTML templates for a small blog
- ▸Internal tool for data processing with web interface
- ▸Microservice integrated with larger Rails or Sinatra ecosystem
Comparisons
- ▸Sinatra vs Rails: Lightweight, minimal vs full-featured MVC
- ▸Sinatra vs Hanami: Minimal DSL vs structured framework
- ▸Sinatra vs Grape: General web apps vs API-focused DSL
- ▸Sinatra vs Roda: Simplicity vs performance and routing focus
- ▸Sinatra vs Padrino: Minimal vs feature-rich extension of Sinatra
Strengths
- ▸Minimal setup and boilerplate
- ▸Flexible and lightweight for small apps
- ▸Quick to prototype and iterate
- ▸Easily integrates with existing Ruby libraries
- ▸Good for APIs, microservices, and internal tools
Limitations
- ▸Not ideal for large-scale applications
- ▸Lacks built-in ORM or database abstractions
- ▸Limited advanced features compared to Rails
- ▸Less opinionated, requiring more architectural decisions
- ▸Scaling requires external infrastructure setup
When NOT to Use
- ▸Large monolithic applications requiring full MVC
- ▸Projects needing built-in database scaffolding
- ▸High-complexity enterprise apps with multiple modules
- ▸Teams preferring convention over configuration
- ▸Applications requiring extensive background job management
Cheat Sheet
- ▸get '/' do ... end - define GET route
- ▸post '/path' do ... end - define POST route
- ▸params[:key] - access query/form parameters
- ▸erb :template_name - render ERB template
- ▸before { ... }, after { ... } - define filters
FAQ
- ▸Can Sinatra run with Rails? -> Yes, can integrate or mount inside Rails app.
- ▸Is Sinatra suitable for APIs? -> Yes, commonly used for REST APIs.
- ▸Does Sinatra include ORM? -> No, database libraries must be added.
- ▸Can Sinatra handle large apps? -> Possible, but Rails or Hanami may be better.
- ▸Is Sinatra open-source? -> Yes, MIT license.
30-Day Skill Plan
- ▸Week 1: Build single-file Hello World app
- ▸Week 2: Add routes and template rendering
- ▸Week 3: Create JSON API endpoints
- ▸Week 4: Integrate middleware and database
- ▸Week 5: Deploy production-ready Sinatra service
Final Summary
- ▸Sinatra is a lightweight Ruby web framework for simple apps and APIs.
- ▸Provides a DSL for routing, request handling, and response generation.
- ▸Ideal for microservices, prototypes, and internal tools.
- ▸Integrates easily with Rack middleware and Ruby libraries.
- ▸Focuses on simplicity, speed, and developer flexibility.
Project Structure
- ▸app.rb - main Sinatra application file
- ▸config.ru - Rack configuration file
- ▸views/ - template files (ERB, Haml, Slim)
- ▸public/ - static assets
- ▸Gemfile - gem dependencies
Basic Concepts
- ▸Route - defines an endpoint and HTTP method
- ▸Handler - block of code executed when route is matched
- ▸Request - object representing HTTP request data
- ▸Response - object representing HTTP response
- ▸Middleware - optional layer for processing requests/responses
Official Docs
- ▸http://sinatrarb.com/documentation.html
- ▸Sinatra GitHub repository
- ▸Rack Middleware Documentation
- ▸RubyGems Sinatra page
- ▸Sinatra Examples and Recipes