Skip to content

Instantly share code, notes, and snippets.

require './sinatra_middleware'
require './config/environment'
use Sinatra::Application
run MyRailsProject::Application
@mizutaki
mizutaki / sinatra_3-16.rb
Created October 13, 2014 03:27
dispatcher
module MySinatra
class Base
def self.prototype
@prototype ||= new
end
def self.call(env)
prototype.call(env)
end
require 'sinatra'
helpers do
def assert(condition)
fail "something is terribly broken" unless condition
end
end
get '/' do
p env['PATH_INFO']
require 'sinatra'
module MySinatra
class Application
def self.call(env)
new.call(env)
end
def call(env)
headers = {'Content-Type' => 'text/html'}
if env['PATH_INFO'] == '/'
<html>
<head>
<title>Link Helper Test</title>
</head>
<body>
<nav>
<ul>
<li><a href="<%= link(:index) %>">index</a></li>
<li><a href="<%= link(:about) %>">About</a></li>
<li><a href="<%= link(:random) %>">Random</a></li>
require 'sinatra'
require 'sinatra/post_get'
post_get '/' do
"Hi #params[:name]"
end
require 'sinatra/base'
module Sinatra
module PostGet
def post_get(route, &block)
get(route, &block)
post(route, &block)
end
end
end
@mizutaki
mizutaki / sinatra_3-3.rb
Last active August 29, 2015 14:07
inner self
require 'sinatra'
outer_self = self
get '/' do
content_type :txt
"outer self: #{outer_self}, inner self: #{self}"
end
require 'sinatra'
before do
content_type :txt
end
get '/har-har' do
stream do |out|
out << "Wanna hear a joke about potassium?\n"
sleep 1.5
require 'sinatra'
before do
content_type :txt
end
connections = []
get '/consume' do
stream(:keep_open) do |out|