Skip to content

Instantly share code, notes, and snippets.

@bmizerany
bmizerany / myapp.rb
Created July 25, 2008 23:28 — forked from pedro/rackup.ru
Running Sinatra in a rackup file
require "rubygems"
require "sinatra"
get '/' do
'hi'
end
@bmizerany
bmizerany / gist:5868
Created August 17, 2008 23:29 — forked from auser/gist:5867
class Doc
def method_missing(m, *args, &block)
args.empty? ? options[m] : options[m] = args[0]
end
def options
@options ||= {}
end
def initialize(&block)
self.instance_eval(&block) if block
end
@bmizerany
bmizerany / app.rb
Created August 29, 2008 21:41 — forked from sr/app.rb
helpers do
def rest
@rest ||= RestClient.new('http://foo.org')
end
end
get '/' do
rest.put '/foo'
'hey'
# A Sinatra app that does the same thing
require 'rubygems'
require 'sinatra'
set :env, :production
get '/' do
"x" * (params[:size].to_i * 1024)
end
require 'rubygems'
require 'sinatra'
require 'sinatra/test/unit'
require 'app'
class MyAppTest < Test::Unit::TestCase
def test_hello_world
assert_equal :test, Sinatra.application.env
get_it '/'
assert @response.ok?
require 'resolv'
def ips_for(host)
Resolv::DNS.open do |dns|
ress = dns.getresources(host, Resolv::DNS::Resource::IN::A)
ress.map { |r| r.address.to_s }
end
end
puts ips_for('heroku.com').sort.first
@bmizerany
bmizerany / gist:18810
Created October 22, 2008 21:36 — forked from tmm1/gist:17730
class String
def space_case
gsub(/([a-z]|[A-Z]+)([A-Z])/){ "#{$1} #{$2}" }
end
def snake_case
gsub(/\B[A-Z][^A-Z]/, '_\&').downcase.gsub(' ', '_')
end
end
class Graph
@bmizerany
bmizerany / meh.rb
Created November 4, 2008 21:31 — forked from seaofclouds/README
# meh is a rainy day experiment with atom and twitter
#----------------------------------------------------
#use latest version of sinatra
$:.unshift File.dirname(__FILE__) + '/sinatra/lib'
require 'rubygems'
require 'sinatra'
require 'net/http'
require 'uri'
require 'sinatra'
require 'thin'
require 'lizzy'
Thread.new do
sleep(1) until EM.reactor_running?
Lizzy.start
end
post "/some/url/:id" do
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"