Skip to content

Instantly share code, notes, and snippets.

View include's full-sized avatar
👽
Free Jaffa

Francisco Cabrita include

👽
Free Jaffa
View GitHub Profile
class App1 < Sinatra::Base
set :views, "/path/to/views"
set :public, "/path/to/static/files"
def strong(text)
"<strong>#{text}</strong>"
end
get '/' do
strong "Hello World"
# Sinatra: multiple route patterns / same action
['/foo', '/bar'].each do |pattern|
get pattern do
"Hello World!"
end
end
require 'sinatra'
require 'compass'
require 'ostruct'
enable :static, :logging
set :views, "#{root}/app/views"
set :haml, :format => :html4, :attr_wrapper => '"'
set :sass, :style => :compact
class CachePolicy < Sinatra::Base
# always forward the request downstream immediately (before processing routes)
before { forward }
# anything with a cache breaking timestamp gets an insanely long max-age
get '/public/*' do
pass unless request.query_string =~ /^\d+$/
response['Cache-Control'] = 'public, max-age=1000000000'
end
Warden::Manager.serialize_into_session{|user| user.id }
Warden::Manager.serialize_from_session{|id| User.get(id) }
Warden::Manager.before_failure do |env,opts|
# Sinatra is very sensitive to the request method
# since authentication could fail on any type of method, we need
# to set it for the failure app so it is routed to the correct block
env['REQUEST_METHOD'] = "POST"
end
# The code in lib/foobar.rb
require 'sinatra/base'
class Foobar < Sinatra::Base
VERSION = '1.0.0'
configure do
set :foobar, environment
end
end
# The tests in test/test_foobar.rb
@rtomayko
rtomayko / .screenrc
Created October 28, 2009 07:42
~rtomayko/.screenrc
# ~rtomayko/.screenrc
# -------------------------------------------------------------------
# Settings
# -------------------------------------------------------------------
crlf off # No Microsoft linebreaks
startup_message off # bypass GPL notice (we're aware)
defscrollback 15000 # big scrollback
shell bash # don't start login shells
shelltitle "" # no title by default - set in PS1
@ches
ches / gist:243611
Created November 26, 2009 19:09 — forked from lukesutton/gist:107966
basic example of Warden authentication with Sinatra
Warden::Manager.serialize_into_session{|user| user.id }
Warden::Manager.serialize_from_session{|id| User.get(id) }
Warden::Manager.before_failure do |env,opts|
# Sinatra is very sensitive to the request method
# since authentication could fail on any type of method, we need
# to set it for the failure app so it is routed to the correct block
env['REQUEST_METHOD'] = "POST"
end
require 'bcrypt'
module EasyAuth
# http://techspeak.plainlystated.com/2010/03/drop-dead-simple-authentication-for.html
# To generate a crypted password (in irb):
# require 'easy_auth'
# EasyAuth.encrypt_password('my_password') # Put returned array in AUTHORIZED_USERS
AUTHORIZED_USERS = {
# Author: Pieter Noordhuis
# Description: Simple demo to showcase Redis PubSub with EventMachine
#
# Update 7 Oct 2010:
# - This example does *not* appear to work with Chrome >=6.0. Apparently,
# the WebSocket protocol implementation in the cramp gem does not work
# well with Chrome's (newer) WebSocket implementation.
#
# Requirements:
# - rubygems: eventmachine, thin, cramp, sinatra, yajl-ruby