Skip to content

Instantly share code, notes, and snippets.

View jamiehodge's full-sized avatar

Jamie Hodge jamiehodge

  • Zendesk
  • Copenhagen, Denmark
View GitHub Profile
@jamiehodge
jamiehodge / gist:1327195
Created October 31, 2011 09:38
Warden and Sinatra example
require 'sinatra/base'
require 'rack/flash'
require 'warden'
require 'slim'
require 'sequel'
require 'sqlite3'
DB = Sequel.sqlite
DB.create_table :users do
primary_key :id
@jamiehodge
jamiehodge / gist:1337882
Created November 3, 2011 21:45
Warden basic http authentication
Warden::Strategies.add(:basic) do
def auth
@auth ||= Rack::Auth::Basic::Request.new(env)
end
def valid?
auth.provided? && auth.basic? && auth.credentials
end
@jamiehodge
jamiehodge / gist:1338190
Created November 3, 2011 22:58
Sinatra human and api routes
module App
class Users < App::Base
use Rack::Parser
helpers do
def cycle
@cycle ||= %w{odd even}.cycle
end
end
@jamiehodge
jamiehodge / gist:1408464
Created November 30, 2011 08:57
Nested Sinatra resources
require 'sinatra/base'
class Foo < Sinatra::Base
get '/' do
'foo'
end
get '/:id' do
params.inspect
end
@jamiehodge
jamiehodge / nginx.rb
Created January 7, 2012 20:58
Homebrew formula for nginx with option for ngx_http_mp4_module
require 'formula'
class Nginx < Formula
homepage 'http://nginx.org/'
url 'http://nginx.org/download/nginx-1.0.11.tar.gz'
head 'http://nginx.org/download/nginx-1.1.12.tar.gz'
if ARGV.build_head?
md5 '2a98411773c87a98e92c5aa68f322338'
else
@jamiehodge
jamiehodge / app.rb
Created January 12, 2012 09:50
Sinatra API for resumable.js
require 'sinatra'
require 'slim'
require 'coffee-script'
require 'sass'
require 'sequel'
DB = Sequel.sqlite
DB.create_table :uploads do
String :id, text: true, primary_key: true
@jamiehodge
jamiehodge / Gemfile
Created January 15, 2012 22:37
Spine.js contacts demo Sinatra backend
source 'https://rubygems.org'
gem 'sinatra', git: 'git://github.com/sinatra/sinatra.git'
gem 'sequel'
gem 'sqlite3'
gem 'yajl-ruby', require: 'yajl'
@jamiehodge
jamiehodge / app.rb
Created February 20, 2012 13:22
Mount Sinatra subclasses
require 'sinatra/base'
class Sinatra::Base
def self.mount(klass, route="/#{klass.name.downcase}s")
before "#{route}/*" do
halt klass.call(
env.merge!(
'SCRIPT_NAME' => route,
'PATH_INFO' => params.delete('splat').join('/'),
'rack.request.query_hash' => params
require 'sinatra/base'
module Sinatra
module Exchange
RESPONSE = Struct.new(:status, :headers, :body)
def get_local(path, params={})
r = env['operator'].call(
env.merge(
'SCRIPT_NAME' => '',
@jamiehodge
jamiehodge / flash.rb
Created March 20, 2012 15:08
Sinatra flash
module Sinatra
module Flash
module InstanceMethods
def flash
@flash ||= {}
end
def flash_now
@flash_now ||= {}
end