Skip to content

Instantly share code, notes, and snippets.

@mizutaki
mizutaki / sinatra_2-35.rb
Last active August 29, 2015 14:07
#operation Cookie
require 'sinatra'
get '/' do
response.set_cookie "sushi", "tenpura"#argument 2
"Cookie set. Would like to <a href='/read'>read it.</a>?"
end
get '/read' do
"Cookie has a value of:#{request.cookies['sushi']}."
end
@mizutaki
mizutaki / sinatra_2-36.rb
Last active August 29, 2015 14:07
attachment file
require 'sinatra'
before do
content_type :txt
end
get '/attachment' do
attachment 'name_your_konishiki.txt'
"Here's what will be sent downstream, in an attachment called 'name_your_konishiki.txt.'"
end
require 'sinatra'
before do
content_type :txt
end
connections = []
get '/consume' do
stream(:keep_open) do |out|
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
@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/base'
module Sinatra
module PostGet
def post_get(route, &block)
get(route, &block)
post(route, &block)
end
end
end
require 'sinatra'
require 'sinatra/post_get'
post_get '/' do
"Hi #params[:name]"
end
<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'
module MySinatra
class Application
def self.call(env)
new.call(env)
end
def call(env)
headers = {'Content-Type' => 'text/html'}
if env['PATH_INFO'] == '/'
require 'sinatra'
helpers do
def assert(condition)
fail "something is terribly broken" unless condition
end
end
get '/' do
p env['PATH_INFO']