Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
require 'rbconfig'
RUBY = Config::CONFIG['bindir'] + '/' + Config::CONFIG['RUBY_INSTALL_NAME'] + Config::CONFIG['EXEEXT']
BOLD = "\e[1m"
WHITE = "\e[37m"
RED = "\e[31m"
GREEN = "\e[32m"
YELLOW = "\e[33m"
BLACK_BG = "\e[40m"
BLUE_BG = "\e[44m"
@andhapp
andhapp / webapp.rb
Created November 18, 2010 23:11 — forked from igrigorik/webapp.rb
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
desc "creates database & database user"
task :create_database do
set :root_password, Capistrano::CLI.password_prompt("MySQL root password: ")
set :db_user, Capistrano::CLI.ui.ask("Application database user: ")
set :db_pass, Capistrano::CLI.password_prompt("Password: ")
set :db_name, Capistrano::CLI.ui.ask("Database name: ")
run "mysql --user=root --password=#{root_password} -e \"CREATE DATABASE IF NOT EXISTS #{db_name}\""
run "mysql --user=root --password=#{root_password} -e \"GRANT ALL PRIVILEGES ON #{db_name}.* TO '#{db_user}'@'localhost' IDENTIFIED BY '#{db_pass}' WITH GRANT OPTION\""
@andhapp
andhapp / can_destroy.rb
Created February 7, 2012 18:24 — forked from spalladino/can_destroy.rb
How to check if object can be destroyed if it has dependent restrict associations
class ActiveRecord::Base
def can_destroy?
self.class.reflect_on_all_associations.all? do |assoc|
assoc.options[:dependent] != :restrict || (assoc.macro == :has_one && self.send(assoc.name).nil?) || (assoc.macro == :has_many && self.send(assoc.name).empty?)
end
end
end
@andhapp
andhapp / callbacks.rb
Created March 26, 2012 09:40
capistrano pre-compile assets for rails 3.1
desc "precompile the assets"
task :precompile_assets, :roles => :web, :except => { :no_release => true } do
run "cd #{current_path}; rm -rf public/assets/*"
run "cd #{current_path}; RAILS_ENV=production bundle exec rake assets:precompile"
end
@andhapp
andhapp / gist:2231347
Created March 28, 2012 23:07
Extract emails from google account
# Gemfile
source "http://rubygems.org"
gem 'mechanize'
gem 'hpricot'
# Ruby code
@andhapp
andhapp / README.md
Created April 25, 2012 05:38 — forked from scottwb/README.md
Monkey patches for a couple Rails Mime::Type.parse bugs.

Rails Mime::Type.parse Patches

There are two Rails issues in it's handling of the HTTP Accept header which cause a number of spurious exception emails via Airbrake. I am encountering this on Rails 3.0.7. One of these is fixed in a later version of Rails, but for other reasons I can't upgrade right now. The other bug is still present in Rails 3.2 and in master at the time of this writing. This gist includes some monkey patches you can apply to fix these issues until such time that they are fixed in Rails properly.

Rails Issue #736

Issue #736 is that Rails does not correctly parse a q-value in an Accept header when there is only one content-type specified. For example:

Accept: text/html;q=0.9
@andhapp
andhapp / installation.sh
Created May 11, 2012 13:58 — forked from mikhailov/installation.sh
Nginx+passenger application config: ssl redirection, http headers, passenger optimal settings. see details: http://mikhailov.posterous.com/nginx
$ cd /usr/src
$ wget http://nginx.org/download/nginx-0.8.52.tar.gz
$ tar xzvf ./nginx-0.8.52.tar.gz
$ rm ./nginx-0.8.52.tar.gz
$ gem install s3sync capistrano capistrano-ext passenger --no-ri --no-rdoc
$ passenger-install-nginx-module
# Automatically download and install Nginx? 2. No: I want to customize my Nginx installation
# Where is your Nginx source code located?: /usr/src/nginx-0.8.52
# Where do you want to install Nginx to?: /opt/nginx
@andhapp
andhapp / pr.md
Created December 3, 2012 17:33 — forked from piscisaureus/pr.md
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

@andhapp
andhapp / nginx.conf
Created October 17, 2015 20:50 — forked from plentz/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048