├── 05rack.rb ├── 02controller.rb ├── 04helper.rb ├── 03controller.rb ├── 01model.rb ├── 09irbrc.rb ├── 11god.rb ├── 10railsrc.rb ├── 06yield.rb ├── 08scenarios.rb └── 07bash.sh /05rack.rb: -------------------------------------------------------------------------------- 1 | class Barefoot 2 | def call(env) 3 | [ 200, 'Yes sir!', { 'Content-Type' => 'text/plain'} ] 4 | end 5 | end 6 | 7 | Rack::Handler::Mongrel.run(Barefoot.new, :Port => 8000) do |server| 8 | trap("INT") { server.stop } 9 | end -------------------------------------------------------------------------------- /02controller.rb: -------------------------------------------------------------------------------- 1 | class PostController < ApplicationController 2 | before_filter :set_top_posts 3 | 4 | def index 5 | @posts = Post.find(:all) 6 | end 7 | 8 | def set_top_posts 9 | @top_posts = Post.find_top_posts 10 | end 11 | end -------------------------------------------------------------------------------- /04helper.rb: -------------------------------------------------------------------------------- 1 | class PostController < ApplicationController 2 | end 3 | 4 | module PostsHelper 5 | def current_posts 6 | @current_posts ||= Post.find(:all) 7 | end 8 | 9 | def top_posts 10 | @top_posts ||= Post.find_top_posts 11 | end 12 | end -------------------------------------------------------------------------------- /03controller.rb: -------------------------------------------------------------------------------- 1 | class PostController < ApplicationController 2 | helper_method :current_posts, :top_posts 3 | 4 | private 5 | def current_posts 6 | @current_posts ||= Post.find(:all) 7 | end 8 | 9 | def top_posts 10 | @top_posts ||= Post.find_top_posts 11 | end 12 | end -------------------------------------------------------------------------------- /01model.rb: -------------------------------------------------------------------------------- 1 | class RandomUser 2 | acts_as_cached 3 | 4 | # Grab X random users in network Y 5 | def self.grab_from_network(id, options = {}) 6 | sex = options[:sex] || '' 7 | key = regional_key(id, sex) 8 | ids = fetch_cache(key) || [] 9 | 10 | limit = options[:limit] || 10 11 | (0...limit).to_a.map { ids.rand } 12 | end 13 | end -------------------------------------------------------------------------------- /09irbrc.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'irb/completion' 3 | require 'irb/ext/save-history' 4 | require 'pp' 5 | 6 | ARGV.concat [ "--readline", "--prompt-mode", "simple" ] 7 | 8 | IRB.conf[:SAVE_HISTORY] = 100 9 | IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history" 10 | IRB.conf[:PROMPT_MODE] = :SIMPLE 11 | 12 | def time(times = 1) 13 | require 'benchmark' 14 | ret = nil 15 | Benchmark.bm { |x| x.report { times.times { ret = yield } } } 16 | ret 17 | end 18 | 19 | load File.dirname(__FILE__) + '/.railsrc' if $0 == 'irb' && ENV['RAILS_ENV'] 20 | -------------------------------------------------------------------------------- /11god.rb: -------------------------------------------------------------------------------- 1 | rails_root = "/data/github/current" 2 | 3 | God.watch do |w| 4 | w.name = 'bj' 5 | w.interval = 30.seconds 6 | w.start = "#{rails_root}/script/bj run --forever --rails_env=production --rails_root=#{rails_root}" 7 | 8 | w.uid = 'git' 9 | w.gid = 'git' 10 | 11 | # determine the state on startup 12 | w.transition(:init, { true => :up, false => :start }) do |on| 13 | on.condition(:process_running) do |c| 14 | c.running = true 15 | end 16 | end 17 | 18 | # determine when process has finished starting 19 | w.transition([:start, :restart], :up) do |on| 20 | on.condition(:process_running) do |c| 21 | c.running = true 22 | c.interval = 5.seconds 23 | end 24 | 25 | # failsafe 26 | on.condition(:tries) do |c| 27 | c.times = 5 28 | c.transition = :start 29 | c.interval = 5.seconds 30 | end 31 | end 32 | 33 | # start if process is not running 34 | w.transition(:up, :start) do |on| 35 | on.condition(:process_running) do |c| 36 | c.running = false 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /10railsrc.rb: -------------------------------------------------------------------------------- 1 | require 'logger' 2 | Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT)) 3 | 4 | load 'Rakefile' 5 | def rake(task) 6 | Rake::Task[task.to_s].invoke 7 | end 8 | 9 | def routes 10 | rake 'routes' 11 | end 12 | 13 | def Object.method_added(method) 14 | ($rails_hacks ||= []) << method 15 | super 16 | end 17 | 18 | def loud_logger 19 | set_logger_to Logger.new(STDOUT) 20 | end 21 | 22 | def quiet_logger 23 | set_logger_to nil 24 | end 25 | 26 | def set_logger_to(logger) 27 | ActiveRecord::Base.connection.instance_variable_set(:@logger, logger) 28 | end 29 | 30 | def sql(query) 31 | ActiveRecord::Base.connection.select_all(query) 32 | end 33 | 34 | def body 35 | app.response.body.squeeze(' ').squeeze("\n") 36 | end 37 | 38 | def rails_hacks 39 | $rails_hacks 40 | end 41 | 42 | def Object.method_added(method) 43 | return super unless method == :helper 44 | (class< 'text/javascript' } ] 15 | end 16 | 17 | def recognize(path) 18 | if found = self.class.routes.detect { |regexp,| path =~ regexp } 19 | [ found.last, path.scan(found.first).first ] 20 | end 21 | end 22 | end 23 | end 24 | 25 | class Yield < Err::Web 26 | Port = ARGV[0] || 4567 27 | Host = 'y.errfree.com' 28 | 29 | def self.load_ads 30 | const_set(:Ads, YAML.load_file('ads.yml').map { |ad| OpenStruct.new(ad) }) 31 | end 32 | 33 | def self.stats_file 34 | today = Date.today 35 | year, month, day = today.year.to_s, today.month.to_s, today.day.to_s 36 | File.join(File.dirname(__FILE__), 'stats', year, month, day) 37 | end 38 | 39 | def self.count_for_ad(id) 40 | `grep -c ' #{id}$' #{stats_file}`.strip 41 | end 42 | 43 | def self.hit!(site, ad) 44 | Thread.new do 45 | file = stats_file 46 | FileUtils.mkdir_p(file.sub(/\d+$/,'')) unless File.exists? file 47 | File.open(file, 'a') { |file| file.write_nonblock([ site, ad.id ].join(' ') + "\n") } 48 | end 49 | end 50 | 51 | def self.render_ad(ad) 52 | <<-javascript 53 | var ad = '' 54 | ad += '#{ad.name}: #{ad.blurb}' 55 | ad += '

' 56 | ad += '#{ad.name}: ' 57 | ad += '#{ad.blurb}' 58 | ad += '

' 59 | document.write(ad) 60 | javascript 61 | end 62 | 63 | load_ads 64 | 65 | route '/reload' do 66 | load_ads 67 | Ads.to_yaml 68 | end 69 | 70 | route '/honk.js' do 71 | counts = Ads.map { |ad| { :name => ad.name, :value => count_for_ad(ad.id) } } 72 | "yield_stats(#{counts.to_json})" 73 | end 74 | 75 | route '/all.js' do 76 | Ads.map { |ad| render_ad(ad) }.to_s 77 | end 78 | 79 | route %r{\/(\w+)\.js} do |site| 80 | ad = Ads[rand(Ads.size)] 81 | 82 | hit! site, ad 83 | 84 | render_ad ad 85 | end 86 | end 87 | 88 | Rack::Handler::Thin.run(Yield.new, :Port => Yield::Port) do |server| 89 | trap("INT") { server.stop } 90 | end 91 | -------------------------------------------------------------------------------- /08scenarios.rb: -------------------------------------------------------------------------------- 1 | class ActiveRecord::Base 2 | def self.skip_stamps 3 | self.record_timestamps = false 4 | yield 5 | self.record_timestamps = true 6 | end 7 | end 8 | 9 | scenario :default do 10 | users = %w( mojombo\ tom@mojombo.com defunkt\ chris@ozmm.org pj\ pjhyett@gmail.com) 11 | keys = {'mojombo' => [ "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAzJFfKCBmeagAjnTp7X+3n0RKlnCQPwwmz40DbQdvVq3xHUtmGpBIQ3TX/qDpyMY97S4REkQf9oaJ9hevsOkJVg2eykv5F0gBwCTwrxzlxouI071hkToJKQfD3m9BEW3CZm6kt3qW6lVgEy30ijgZI9IZquuDiR01bayaWR3+FFCpY1fYr6yRl+g57KYOm/Kd0iiDlPhwh1W5U8C29RLFFKAirzAWpp78zONfrayXJK6cMxYKVkCGonTrhx7C06PEU5SA4Gl57OG1aFLsUg3TSvEt+nDdxh3lTZC3NAQPMmcpQ6HVEr0HcNylQrtN/fVYvz1cGutAGHkkf1ikw/QDyw== tom@volcano.local", "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1gGaHfex00qg2F2DeOl8XIVTX6vffVitj7FxSZfsB4RtlwGlDFyTVE2QvjOitkPYdicVwGqvD0baRBqB147dgS01J3dHrhDZB9J6l9ZXz0BMap7YsxVkpHe8Lp2FmnU34hrimvbO94sJvSX0QCwVYawOOzOT4i6XHPP5de4z7PrJ9JFMk5rcyMAjnxDBTvLyJCbjiXH1FwBZ0hsjv5eggrHmKWCMbMcRKzmVnRg8KtvUFh4iiIe7s1QJ9vNjNsLeZavtkDj/JGHd9AMNkR94sLq+WDQihn/f144tWt93h0N34BrOChrwgi8ClC6t2ahloPhZ1ZUdGyKIRcN7RL4uZQ== tom@volcano.local"] } 12 | 13 | users.each do |user| 14 | user, email = user.split(' ') 15 | u = User.create! \ 16 | :login => user, 17 | :email => email, 18 | :password => 'password', 19 | :password_confirmation => 'password', 20 | :public_keys => keys[user] 21 | u.update_attribute(:beta_invite_quota, 5) 22 | end 23 | 24 | users = User.find(:all) 25 | 26 | mojombo = users.first 27 | defunkt = users[1] 28 | pj = users[2] 29 | 30 | pj.update_attribute(:beta_invite_quota, 0) 31 | 32 | defunkt.beta_invites.create :email => pj.email, :user => pj 33 | 34 | defunkt.posts.create \ 35 | :title => 'site is almost done', 36 | :body => 'for real', 37 | :published => true 38 | 39 | defunkt.posts.create \ 40 | :title => 'site is done', 41 | :body => 'now its time to rock', 42 | :published => true 43 | 44 | p = mojombo.posts.create \ 45 | :title => 'site is down', 46 | :body => 'i broke something', 47 | :published => true 48 | 49 | pj.comments.create :post => p, :body => 'always something' 50 | pj.comments.create :post => p, :body => 'oh wait, you rock' 51 | 52 | grit = Repository.create :name => 'grit', :owner => mojombo, :public => true, :deploy_key => 'myspecialdeploykey' 53 | ambition = Repository.create :name => 'ambition', :owner => defunkt, :public => false 54 | github = Repository.create :name => 'github', :owner => defunkt, :public => true 55 | github.fork(:owner => users.first) 56 | 57 | ambition.wikis.create :title => "Private", :body => "Awesome repo, not.", :user => defunkt 58 | github.wikis.create :title => "Public", :body => "Are the bomb", :user => defunkt 59 | 60 | mojombo.member_repositories << [ambition, github] 61 | defunkt.watched_repositories << grit 62 | 63 | CommitEvent.trigger(grit, 'fc128af28cb263bf1f524f84609a7a75ffa27a9b', 'b6e1b765e0c15586a2c5b9832854f95defd71e1f') 64 | CommitEvent.trigger(ambition, 'be190d59e13327b1f251e3984f8d59adc5d0c847', '909e4d4f706c11cafbe35fd9729dc6cce24d6d6f') 65 | 66 | guide = Guide.create(:user => mojombo, :body => 'MyText', :title => 'MyString') 67 | guide.update_attributes(:user => defunkt, :body => 'MyText2') 68 | 69 | Message.create(:from => mojombo, :to => defunkt, :subject => 'oh hai', :body => ('oh hai defunkt ' * 100)) 70 | 100.times { m = Message.create(:from => pj, :to => defunkt, :subject => 'hey dude', :body => 'please stop being so cool'); m.reply(:from => defunkt, :body => 'FINE') } 71 | Message.create(:from => mojombo, :to => defunkt, :subject => 'oh hai', :body => ('oh hai defunkt ' * 100), :to_deleted => true) 72 | 73 | 74 | PopularRepository.create(:repository => grit, :current => true, :diff => 0, :position => 1, :list => 'forked') 75 | PopularRepository.create(:repository => github, :current => true, :diff => 0, :position => 2, :list => 'forked') 76 | end 77 | 78 | scenario :test do 79 | users = %w( quentin aaron ) 80 | created_at = [ 5.days.ago.to_s(:db), 1.day.ago.to_s(:db) ] 81 | 82 | ActiveRecord::Base.skip_stamps do 83 | users.each do |user| 84 | user = User.new :login => user, :email => "#{user}@example.com" 85 | user.salt = '7e3041ebc2fc05a40c60028e2c4901a81035d3cd' 86 | user.created_at = created_at.shift 87 | user.crypted_password = '00742970dc9e6319f8019fd54864d3ea740f04b1' 88 | user.save 89 | BetaInvite.create :email => user.email, :user => user 90 | end 91 | end 92 | 93 | BetaInvite.create :email => 'quire@example.com', :inviter => User.find(:first) 94 | 95 | Repository.create :name => 'god', :owner => User.find(:first) 96 | end 97 | -------------------------------------------------------------------------------- /07bash.sh: -------------------------------------------------------------------------------- 1 | alias io='touch ~/.io_history && rm ~/.io_history && io' 2 | alias ike='io ike.io' 3 | 4 | # projects 5 | alias fs='fam; sudo ./script/server -p80' 6 | alias free='cd ~/ruby/rails/errfree' 7 | alias rr='cd ~/ruby/rails/projects/rewrite-it' 8 | alias fam='cd ~/ruby/rails/famspam' 9 | alias hub='cd ~/ruby/rails/github' 10 | alias srb='cd ~/Desktop/Training/sakerb' 11 | alias like='cd ~/ruby/rails/musiclike' 12 | alias ffs='cd ~/ruby/rails/ffs' 13 | alias grit='cd ~/ruby/projects/grit' 14 | alias err='cd ~/ruby/projects/err' 15 | alias amb='cd ~/ruby/projects/ambition' 16 | alias take='ruby ~/ruby/projects/svn/projects/sake/lib/sake.rb' 17 | alias fu='cd ~/ruby/projects/plugins/cache_fu' 18 | alias mofo='cd ~/ruby/projects/err/mofo' 19 | alias plugins='cd ~/ruby/projects/plugins' 20 | alias proj='cd ~/ruby/projects/' 21 | alias rock='cd ~/ruby/projects/svn/projects/rock' 22 | 23 | # slices 24 | alias samurai='ssh samurai' 25 | alias ninja='ssh ninja -p 53251' 26 | alias pirate='ssh pirate' 27 | alias ncp='scp -P 53251' 28 | alias github1='ssh ey02-s00098' 29 | alias github2='ssh ey02-s00099' 30 | alias ffs-staging='ssh ffs-staging' 31 | 32 | # shoes 33 | alias shoe='DYLD_LIBRARY_PATH=. PANGO_RC_FILE=pangorc /Users/chris/ruby/shoes/Shoes.app/Contents/MacOS/shoes' 34 | alias pmp='cd /Users/chris/ruby/shoes/Shoes.app/Contents/MacOS' 35 | alias s='./shoes-launch' 36 | 37 | # clients 38 | alias clients='cd ~/ruby/rails/clients' 39 | 40 | # rbx 41 | alias rbx='./shotgun/rubinius ' 42 | alias sirb='rbx bin/sirb.rb -y' 43 | alias rubin='cd ~/ruby/projects/rubinius' 44 | 45 | # gems 46 | alias gemi='gem install --no-rdoc --no-ri -y' 47 | export GEM_HOME="$HOME/.gems" 48 | export GEM_PATH="$GEM_HOME" 49 | export PATH="$HOME/.gems/bin:$PATH" 50 | 51 | # github 52 | alias gitgem='cd ~/ruby/projects/github' 53 | 54 | # general 55 | alias vtouch='rtouch $1 && vi $1' 56 | alias rcov_units='rake test:units:rcov SHOW_ONLY=m' 57 | alias rcov_funcs='rake test:functionals:rcov SHOW_ONLY=c,h' 58 | alias bashrc='vi ~/.bashrc && source ~/.bashrc' 59 | alias svnst='svn st | grep -v status | grep \s' 60 | alias ack='ack -a' 61 | alias dns='lookupd -flushcache' 62 | alias deploy='git push && cap production deploy' 63 | alias ls='ls -G' 64 | alias grep='grep --color' 65 | alias less='less -r' 66 | alias xterm='xterm -foreground white -background black -geometry 160x40' 67 | alias artest='AR_TX_FIXTURES=yes ruby -I connections/native_mysql' 68 | alias pi='piston import' 69 | alias rake='rake -s' 70 | alias sc='./script/console' 71 | alias ss='./script/server' 72 | alias sshproxy='ssh -D 8080 -f -C -q -N chris@errtheblog.com' 73 | alias screen='screen -e^Gg' 74 | alias sl='screen -ls' 75 | alias sr='screen -R' 76 | alias ns='screen -t' 77 | alias heel='heel --no-highlighting' 78 | alias m='mate' 79 | 80 | function hit { 81 | curl -s -o /dev/null http://localhost:3000/$1 82 | } 83 | 84 | function vimgrep { 85 | vim -O `egrep -r -l $1 * | grep -v .svn` 86 | } 87 | 88 | # git 89 | alias gb='git branch -a -v' 90 | alias gs='git status' 91 | alias gd='git diff' 92 | 93 | # gc => git checkout master 94 | # gc bugs => git checkout bugs 95 | function gc { 96 | if [ -z "$1" ]; then 97 | git checkout master 98 | else 99 | git checkout $1 100 | fi 101 | } 102 | 103 | function hubgrab { 104 | wget $1; 105 | tar xzvf *.tar.gz; 106 | rm *.tar.gz; 107 | } 108 | 109 | # Cache, and complete, Cheats 110 | if [ ! -r ~/.cheats ]; then 111 | echo "Rebuilding Cheat cache... " 112 | cheat sheets | egrep '^ ' | awk {'print $1'} > ~/.cheats 113 | fi 114 | complete -W "$(cat ~/.cheats)" cheat 115 | complete -C ~/.raketab -o default rake 116 | 117 | # environment 118 | export TERM=xterm-color 119 | export EDITOR=vim 120 | export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH 121 | export EVENT_NOKQUEUE=1 122 | export PATH=$PATH:/usr/local/bin:/usr/local/mysql/bin 123 | export PATH=$PATH:/opt/local/lib/postgresql81/bin:/Applications/Graphviz.app/Contents/MacOS 124 | 125 | # put current git branch in the prompt 126 | function parse_git_branch { 127 | git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' 128 | } 129 | 130 | function proml { 131 | local BLUE="\[\033[0;34m\]" 132 | local RED="\[\033[0;31m\]" 133 | local LIGHT_RED="\[\033[1;31m\]" 134 | local GREEN="\[\033[0;32m\]" 135 | local LIGHT_GREEN="\[\033[1;32m\]" 136 | local WHITE="\[\033[1;37m\]" 137 | local LIGHT_GRAY="\[\033[0;37m\]" 138 | case $TERM in 139 | xterm*) 140 | TITLEBAR='\[\033]0;\u@\h:\w\007\]' 141 | ;; 142 | *) 143 | TITLEBAR="" 144 | ;; 145 | esac 146 | 147 | PS1="${TITLEBAR}\ 148 | $BLUE[$RED\$(date +%H:%M)$BLUE]\ 149 | $BLUE[$RED\u@\h:\w$GREEN\$(parse_git_branch)$BLUE]\ 150 | $LIGHT_GRAY\$ " 151 | PS2='> ' 152 | PS4='+ ' 153 | } 154 | proml 155 | 156 | function prs { 157 | PS1="$ " 158 | } 159 | --------------------------------------------------------------------------------