Skip to content

Instantly share code, notes, and snippets.

View knzconnor's full-sized avatar

Kenzi Connor knzconnor

View GitHub Profile
use Rack::Bug
map '/my_sweet_rack_app'
run MySweetRackApp.new
end
map '/'
DenyAccessAndRecordRetinaApp.new
end
class MySweetRackApp
def initialize
Proc.new { |env| [200, {'Content-Type' => 'text/html'}, ['success']] }
end
end
def anon_access_denied
respond_to do |accepts|
accepts.html do
store_location
redirect_to login_path
end
accepts.xml do
headers["Status"] = "Unauthorized"
headers["WWW-Authenticate"] = %(Basic realm="Web Password")
render :text => "Could't authenticate you", :status => '401 Unauthorized'
@knzconnor
knzconnor / decomposed_movie_example.rb
Created March 28, 2010 20:00
decomposed methods versus implementation methods
def can_checkout_movie?
account_is_current? &&
available_rental_slots? &&
late_fees_within_limit?
end
#Just be careful. Safest to properly end color sequence, so instead of this
#export PS1="\[\033[01;32m\]\u\[\033[00m\]\[\033[01;31m\]\$(git_br)\[\033[01;32m\]\[\033[00m\]:\[\033[01;36m\]\$(git_pwd)\[\033[00m\]\$ "
#do this
USER_GREEN='\[\e[01;32m\]'
#NO_COLOR='\[\e[00m\]'
REPO_RED='\[\e[01;31m\]'
PATH_BLUE='\[\e[01;36m\]'
END_COLOR='\[\e[m\]'
#http://github.com/ahoward/open4 is an alternative that handles pid and exit status
#http://github.com/ahoward/session a more fleshed our shell utility
require 'open3'
Open3.popen3("dc") do |stdin, stdout, stderr|
t = Thread.new(stderr) do |terr|
while (line = terr.gets)
puts "stderr: #{line}"
end
end
class Wizard
# class << self
# self.class_eval do
# define_method :convene_council do
# puts "The council is in session!"
# end
# end
# end
def self.metaclass
@knzconnor
knzconnor / HelloWorld.java
Created February 16, 2010 07:37
Call a custom .jar from jruby
public class HelloWorld {
public String hello() {
return "Hello cruel world";
}
}
## require in config/environment.rb before init starts
#require 'config/add_explicit_path_to_config_gem'
#Rails::Initializer.run do |config|
#config.gem 'my_gem', :version => '0.0.1', :explicit_path => "#{RAILS_ROOT}/vendor/local_gemfiles/my_gem-0.0.1.gem" #or
#config.gem 'my_gem', :version => '0.0.1', :explicit_path => "http://github.com/myname/myrepo/raw/master/mygem.gem" #or
module Rails
class GemDependency < Gem::Dependency
def initialize(name, options = {})
require 'rubygems' unless Object.const_defined?(:Gem)
#a variation of this:http://ozmm.org/posts/z_cache_bang.html
#def rankings
# @rankings ||= rankings!
#end
#def rankings!
# # compute expensive rankings
#end
def rankings