Permalink
Browse files

barefoot examples

  • Loading branch information...
0 parents commit a4e0158e0bb0bc39121841c787d15bbcfe0a0e27 @defunkt committed Mar 17, 2008
Showing with 522 additions and 0 deletions.
  1. +13 −0 01model.rb
  2. +11 −0 02controller.rb
  3. +12 −0 03controller.rb
  4. +12 −0 04helper.rb
  5. +9 −0 05rack.rb
  6. +90 −0 06yield.rb
  7. +158 −0 07bash.sh
  8. +96 −0 08scenarios.rb
  9. +19 −0 09irbrc.rb
  10. +63 −0 10railsrc.rb
  11. +39 −0 11god.rb
@@ -0,0 +1,13 @@
+class RandomUser
+ acts_as_cached
+
+ # Grab X random users in network Y
+ def self.grab_from_network(id, options = {})
+ sex = options[:sex] || ''
+ key = regional_key(id, sex)
+ ids = fetch_cache(key) || []
+
+ limit = options[:limit] || 10
+ (0...limit).to_a.map { ids.rand }
+ end
+end
@@ -0,0 +1,11 @@
+class PostController < ApplicationController
+ before_filter :set_top_posts
+
+ def index
+ @posts = Post.find(:all)
+ end
+
+ def set_top_posts
+ @top_posts = Post.find_top_posts
+ end
+end
@@ -0,0 +1,12 @@
+class PostController < ApplicationController
+ helper_method :current_posts, :top_posts
+
+private
+ def current_posts
+ @current_posts ||= Post.find(:all)
+ end
+
+ def top_posts
+ @top_posts ||= Post.find_top_posts
+ end
+end
@@ -0,0 +1,12 @@
+class PostController < ApplicationController
+end
+
+module PostsHelper
+ def current_posts
+ @current_posts ||= Post.find(:all)
+ end
+
+ def top_posts
+ @top_posts ||= Post.find_top_posts
+ end
+end
@@ -0,0 +1,9 @@
+class Barefoot
+ def call(env)
+ [ 200, 'Yes sir!', { 'Content-Type' => 'text/plain'} ]
+ end
+end
+
+Rack::Handler::Mongrel.run(Barefoot.new, :Port => 8000) do |server|
+ trap("INT") { server.stop }
+end
@@ -0,0 +1,90 @@
+%w( rubygems fileutils json rack thin yaml ostruct ).each { |f| require f }
+OpenStruct.class_eval { undef_method :id } rescue nil
+
+module Err
+ class Web
+ def self.routes; @@routes ||= [] end
+
+ def self.route(path, &block)
+ routes << [ Regexp.new(path), block ]
+ end
+
+ def call(env)
+ action, params = recognize(env['PATH_INFO'])
+ [ 200, action.calls(*params), { 'Content-Type' => 'text/javascript' } ]
+ end
+
+ def recognize(path)
+ if found = self.class.routes.detect { |regexp,| path =~ regexp }
+ [ found.last, path.scan(found.first).first ]
+ end
+ end
+ end
+end
+
+class Yield < Err::Web
+ Port = ARGV[0] || 4567
+ Host = 'y.errfree.com'
+
+ def self.load_ads
+ const_set(:Ads, YAML.load_file('ads.yml').map { |ad| OpenStruct.new(ad) })
+ end
+
+ def self.stats_file
+ today = Date.today
+ year, month, day = today.year.to_s, today.month.to_s, today.day.to_s
+ File.join(File.dirname(__FILE__), 'stats', year, month, day)
+ end
+
+ def self.count_for_ad(id)
+ `grep -c ' #{id}$' #{stats_file}`.strip
+ end
+
+ def self.hit!(site, ad)
+ Thread.new do
+ file = stats_file
+ FileUtils.mkdir_p(file.sub(/\d+$/,'')) unless File.exists? file
+ File.open(file, 'a') { |file| file.write_nonblock([ site, ad.id ].join(' ') + "\n") }
+ end
+ end
+
+ def self.render_ad(ad)
+ <<-javascript
+ var ad = ''
+ ad += '<a href="#{ad.url}"><img src="http://#{Host}/i/#{ad.image}" alt="#{ad.name}: #{ad.blurb}" /></a>'
+ ad += '<p class="blurb">'
+ ad += '<a href="#{ad.url}">#{ad.name}</a>: '
+ ad += '#{ad.blurb}'
+ ad += '</p>'
+ document.write(ad)
+ javascript
+ end
+
+ load_ads
+
+ route '/reload' do
+ load_ads
+ Ads.to_yaml
+ end
+
+ route '/honk.js' do
+ counts = Ads.map { |ad| { :name => ad.name, :value => count_for_ad(ad.id) } }
+ "yield_stats(#{counts.to_json})"
+ end
+
+ route '/all.js' do
+ Ads.map { |ad| render_ad(ad) }.to_s
+ end
+
+ route %r{\/(\w+)\.js} do |site|
+ ad = Ads[rand(Ads.size)]
+
+ hit! site, ad
+
+ render_ad ad
+ end
+end
+
+Rack::Handler::Thin.run(Yield.new, :Port => Yield::Port) do |server|
+ trap("INT") { server.stop }
+end
@@ -0,0 +1,158 @@
+alias io='touch ~/.io_history && rm ~/.io_history && io'
+alias ike='io ike.io'
+
+# projects
+alias fs='fam; sudo ./script/server -p80'
+alias free='cd ~/ruby/rails/errfree'
+alias rr='cd ~/ruby/rails/projects/rewrite-it'
+alias fam='cd ~/ruby/rails/famspam'
+alias hub='cd ~/ruby/rails/github'
+alias srb='cd ~/Desktop/Training/sakerb'
+alias like='cd ~/ruby/rails/musiclike'
+alias ffs='cd ~/ruby/rails/ffs'
+alias grit='cd ~/ruby/projects/grit'
+alias err='cd ~/ruby/projects/err'
+alias amb='cd ~/ruby/projects/ambition'
+alias take='ruby ~/ruby/projects/svn/projects/sake/lib/sake.rb'
+alias fu='cd ~/ruby/projects/plugins/cache_fu'
+alias mofo='cd ~/ruby/projects/err/mofo'
+alias plugins='cd ~/ruby/projects/plugins'
+alias proj='cd ~/ruby/projects/'
+alias rock='cd ~/ruby/projects/svn/projects/rock'
+
+# slices
+alias samurai='ssh samurai'
+alias ninja='ssh ninja -p 53251'
+alias pirate='ssh pirate'
+alias ncp='scp -P 53251'
+alias github1='ssh ey02-s00098'
+alias github2='ssh ey02-s00099'
+alias ffs-staging='ssh ffs-staging'
+
+# shoes
+alias shoe='DYLD_LIBRARY_PATH=. PANGO_RC_FILE=pangorc /Users/chris/ruby/shoes/Shoes.app/Contents/MacOS/shoes'
+alias pmp='cd /Users/chris/ruby/shoes/Shoes.app/Contents/MacOS'
+alias s='./shoes-launch'
+
+# clients
+alias clients='cd ~/ruby/rails/clients'
+
+# rbx
+alias rbx='./shotgun/rubinius '
+alias sirb='rbx bin/sirb.rb -y'
+alias rubin='cd ~/ruby/projects/rubinius'
+
+# gems
+alias gemi='gem install --no-rdoc --no-ri -y'
+export GEM_HOME="$HOME/.gems"
+export GEM_PATH="$GEM_HOME"
+export PATH="$HOME/.gems/bin:$PATH"
+
+# github
+alias gitgem='cd ~/ruby/projects/github'
+
+# general
+alias vtouch='rtouch $1 && vi $1'
+alias rcov_units='rake test:units:rcov SHOW_ONLY=m'
+alias rcov_funcs='rake test:functionals:rcov SHOW_ONLY=c,h'
+alias bashrc='vi ~/.bashrc && source ~/.bashrc'
+alias svnst='svn st | grep -v status | grep \s'
+alias ack='ack -a'
+alias dns='lookupd -flushcache'
+alias deploy='git push && cap production deploy'
+alias ls='ls -G'
+alias grep='grep --color'
+alias less='less -r'
+alias xterm='xterm -foreground white -background black -geometry 160x40'
+alias artest='AR_TX_FIXTURES=yes ruby -I connections/native_mysql'
+alias pi='piston import'
+alias rake='rake -s'
+alias sc='./script/console'
+alias ss='./script/server'
+alias sshproxy='ssh -D 8080 -f -C -q -N chris@errtheblog.com'
+alias screen='screen -e^Gg'
+alias sl='screen -ls'
+alias sr='screen -R'
+alias ns='screen -t'
+alias heel='heel --no-highlighting'
+alias m='mate'
+
+function hit {
+ curl -s -o /dev/null http://localhost:3000/$1
+}
+
+function vimgrep {
+ vim -O `egrep -r -l $1 * | grep -v .svn`
+}
+
+# git
+alias gb='git branch -a -v'
+alias gs='git status'
+alias gd='git diff'
+
+# gc => git checkout master
+# gc bugs => git checkout bugs
+function gc {
+ if [ -z "$1" ]; then
+ git checkout master
+ else
+ git checkout $1
+ fi
+}
+
+function hubgrab {
+ wget $1;
+ tar xzvf *.tar.gz;
+ rm *.tar.gz;
+}
+
+# Cache, and complete, Cheats
+if [ ! -r ~/.cheats ]; then
+ echo "Rebuilding Cheat cache... "
+ cheat sheets | egrep '^ ' | awk {'print $1'} > ~/.cheats
+fi
+complete -W "$(cat ~/.cheats)" cheat
+complete -C ~/.raketab -o default rake
+
+# environment
+export TERM=xterm-color
+export EDITOR=vim
+export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH
+export EVENT_NOKQUEUE=1
+export PATH=$PATH:/usr/local/bin:/usr/local/mysql/bin
+export PATH=$PATH:/opt/local/lib/postgresql81/bin:/Applications/Graphviz.app/Contents/MacOS
+
+# put current git branch in the prompt
+function parse_git_branch {
+ git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
+}
+
+function proml {
+ local BLUE="\[\033[0;34m\]"
+ local RED="\[\033[0;31m\]"
+ local LIGHT_RED="\[\033[1;31m\]"
+ local GREEN="\[\033[0;32m\]"
+ local LIGHT_GREEN="\[\033[1;32m\]"
+ local WHITE="\[\033[1;37m\]"
+ local LIGHT_GRAY="\[\033[0;37m\]"
+ case $TERM in
+ xterm*)
+ TITLEBAR='\[\033]0;\u@\h:\w\007\]'
+ ;;
+ *)
+ TITLEBAR=""
+ ;;
+ esac
+
+PS1="${TITLEBAR}\
+$BLUE[$RED\$(date +%H:%M)$BLUE]\
+$BLUE[$RED\u@\h:\w$GREEN\$(parse_git_branch)$BLUE]\
+$LIGHT_GRAY\$ "
+PS2='> '
+PS4='+ '
+}
+proml
+
+function prs {
+ PS1="$ "
+}
Oops, something went wrong.

0 comments on commit a4e0158

Please sign in to comment.