### Install Otto Gem
Source: https://github.com/delano/otto/blob/main/README.md
Command to install the Otto Ruby gem using the RubyGems package manager.
```bash
gem install otto
```
--------------------------------
### Basic Otto App Structure
Source: https://github.com/delano/otto/blob/main/README.md
Demonstrates the essential files for an Otto application: a Rackup file (config.ru), a Ruby application class (app.rb), and a plain-text routes file.
```bash
cd myapp && ls
config.ru app.rb routes
```
--------------------------------
### Enable Security Features in Otto
Source: https://github.com/delano/otto/blob/main/README.md
Demonstrates how to initialize an Otto application with security features enabled, such as CSRF protection, request validation, and trusted proxy configuration.
```ruby
# Enable security features
app = Otto.new("./routes", {
csrf_protection: true, # CSRF tokens and validation
request_validation: true, # Input sanitization and limits
trusted_proxies: ['10.0.0.0/8']
})
```
--------------------------------
### Implement Application Logic in Ruby
Source: https://github.com/delano/otto/blob/main/README.md
Shows a basic Ruby class structure for an Otto application, including methods to handle different routes like 'index', 'show_product', and 'robots_text'. It demonstrates how to access request parameters and set response bodies and headers.
```ruby
# app.rb
class App
def initialize(req, res)
@req, @res = req, res
end
def index
res.body = '
Hello Otto
'
end
def show_product
product_id = req.params[:id]
res.body = "Product: #{product_id}"
end
def robots_text
res.header['Content-Type'] = "text/plain"
rules = 'User-agent: *', 'Disallow: /private/keep/out'
res.body = rules.join($/)
end
end
```
--------------------------------
### Define Routes in Plain Text
Source: https://github.com/delano/otto/blob/main/README.md
Illustrates how to map HTTP methods and URL patterns to specific methods within a Ruby class using a plain-text routes file.
```text
# routes
GET / App#index
POST /feedback App#receive_feedback
GET /product/:id App#show_product
GET /robots.txt App#robots_text
GET /404 App#not_found
```
--------------------------------
### Configure Rackup File for Otto
Source: https://github.com/delano/otto/blob/main/README.md
Provides the standard Rackup file (config.ru) configuration needed to run an Otto application, including requiring the Otto gem and the application class.
```ruby
# config.ru
require 'otto'
require 'app'
run Otto.new("./routes")
```
--------------------------------
### Configure Internationalization (Locales) in Otto
Source: https://github.com/delano/otto/blob/main/README.md
Shows various ways to configure internationalization settings for Otto applications, including setting available locales and default locales globally, during initialization, or at runtime.
```ruby
# Global configuration (affects all Otto instances)
Otto.configure do |opts|
opts.available_locales = { 'en' => 'English', 'es' => 'Spanish', 'fr' => 'French' }
opts.default_locale = 'en'
end
# Or configure during initialization
app = Otto.new("./routes", {
available_locales: { 'en' => 'English', 'es' => 'Spanish', 'fr' => 'French' },
default_locale: 'en'
})
# Or configure at runtime
app.configure(
available_locales: { 'en' => 'English', 'es' => 'Spanish' },
default_locale: 'en'
)
# Legacy support (still works)
app = Otto.new("./routes", {
locale_config: {
available_locales: { 'en' => 'English', 'es' => 'Spanish', 'fr' => 'French' },
default_locale: 'en'
}
})
```
--------------------------------
### Utilize Locale Helper in Application Logic
Source: https://github.com/delano/otto/blob/main/README.md
Illustrates how to use Otto's `check_locale!` helper within an application's Ruby class to automatically detect and retrieve the user's locale based on URL parameters, headers, or a default setting.
```ruby
class App
def initialize(req, res)
@req, @res = req, res
end
def show_product
# Automatically detects locale from:
# 1. URL parameter: ?locale=es
# 2. User preference (if provided)
# 3. Accept-Language header
# 4. Default locale
locale = req.check_locale!
# Use locale for localized content
res.body = localized_content(locale)
end
end
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.