Deleting stale Paperclip attachment styles from the server

Sometimes you add Paperclip image styles, sometimes you remove some. In order to only keep the files you actually need, you should remove stale Paperclip styles from your server.

This script has been used in production successfully. Use at your own risk.

# Config #######################################################################
delete_styles = [:gallery, :thumbnail, :whatever]
scope = YourModel # A scope on the class with #has_attached_file
attachment_name = :image # First argument of #has_attached_file
noop ...

Setup (and secure) an SSH server on Ubuntu

Install OpenSSH Server:

sudo apt-get install openssh-server

To check if the server is running you should get no error when you restart it:

sudo /etc/init.d/ssh restart

Now your ssh server is ready to use.

To add additional security edit your sshd_config (gksudo gedit /etc/ssh/sshd_config):

# Deny root login:
PermitRootLogin no

# To whitelist users:
AllowUsers USERNAME1 USERNAME2

# To disable interactive authentication (without SSH key)
PasswordAuthentication no

Don't forget to restart after editin...

Run multiple local webricks at the same time using different ports

If you specify different ports, you can run multiple local webricks with rails server --port=300X at the same time.

Fix [RubyODBC]Cannot allocate SQLHENV when connecting to MSSQL 2005 with Ruby 1.8.7. on Ubuntu 10.10

I followed this nice guide Connecting to MSSQL with Ruby on Ubuntu - lambie.org until I ran in the following errors:

irb(main):001:0> require "dbi"; dbh = DBI.connect('dbi:ODBC:MyLegacyServer', 'my_name', 'my_password')

DBI::DatabaseError: INTERN (0) [RubyODBC]Cannot allocate SQLHENV
  from /usr/lib/ruby/1.8/dbd/odbc/driver.rb:36:in `connect'
  from /usr/lib/ruby/1.8/dbi/handles/driver.rb:33:in `connect'
  from /usr/lib/ruby...

Request a gzipped response from a web server using Wget

To reduce download time, application servers usually serve content using gzip compression, if the browser supports it.

When using a tool like Wget to explicitly download an application's response, the server responds with the uncompressed version:

wget http://example.com/

If you are curious about the compressed file's size, pass the corresponding HTTP header:

wget --header="accept-encoding: gzip" http://example.com/

Run a POP3 server on a directory of mail files with popthis

popthis is awesome when used with inaction_mailer.

Setup inaction_mailer

Install the gem:
sudo gem install popthis

Start the server:
popthis tmp/sent_mails/ # e.g. the folder containing the .txt-mails generated by inaction_mailer

Now, configure your mail client as follows:
Server: localhost
Protocol: POP3
Port: 2220
Username: anything
Password: anything

How to make Capistrano not truncate server output

By default, Capistrano truncates server responses and places an ellipsis at the end of lines that are longer than your terminal. Error messages are never truncated.

While this can be helpful to make deployment output appear less noisy, it also hides information that could be helpful.
I believe you should prefer knowing what is going on, even if causes a few extra lines of output.

Capistrano by default uses Airbrussh which is where truncation happens. To disable truncation globally, place this into your deploy.rb:

set :format_options...

List keys stored in memcached / Amazon AWS ElastiCache server

Connect to your memcached host. (AWS elasticache is memcached)

telnet foohost23.cs2631.0001.euw1.cache.amazonaws.com 11211 

Once you're connected, find out which 'slabs' exist within the cache:

stats items
STAT items:1:number 3550
STAT items:1:age 5166393
STAT items:1:evicted 0
STAT items:1:evicted_nonzero 0
STAT items:1:evicted_time 0
STAT items:1:outofmemory 0
STAT items:1:tailrepairs 0
STAT items:1:reclaimed 0
STAT items:2:number 144886
STAT items:2:age 1375536
STAT items:2:evicted 0

...

How to fix: "500 Internal Server Error" after adding Rack::Bug

When Rack::Bug has been added to your project and your Apache2/Passenger only replies with an Error 500 (Internal Server Error) you won't get any love from both application and Apache logs.

You can start a script/server and try connecting there. It should also fail but you will most likely see this error:

Internal Server Error  
undefined method `new' for "Rack::Bug":String

While the following is (for some reason) working on OSX...

config.middleware.use "Rack::Bug", :secret_key => '...'

...you need to do this so it wor...

thoughtbot/fake_stripe: A Stripe fake so that you can avoid hitting Stripe servers in tests.

fake_stripe spins up a local server that acts like Stripe’s and also serves a fake version of Stripe.js, Stripe’s JavaScript library that allows you to collect your customers’ payment information without ever having it touch your servers. It spins up when you run your feature specs, so that you can test your purchase flow without hitting Stripe’s servers or making any external HTTP requests.

We've also had tests actually hitting the testing sandbox of Stripe, which worked OK most of the time (can be flakey).

Forward HTTP through an intermediary server (Local Port Forwarding)

This will tunnel HTTP requests to one given domain and port through an intermediary SSH server:

ssh -L 8080:targethost:80 tunnelhost

http://localhost:8080 will now connect you to http://targethost:80, tunnelling all data through tunnelhost via SSH.

Note that the connection between tunnelhost and targethost will still be unencrypted in this example.

Run a script on the server

You have to specify the environment with -e env_name or RAILS_ENV=env_name if you want to run a script on the server.

at Rails 2 it's script/runner

bundle exec script/runner -e env_name path/to/script.rb argument1 argument2 ...

at Rails 3 it's rails runner

RAILS_ENV=env_name bundle exec rails runner path/to/script.rb argument1 argument2 ...

Listing all gems on a private gem server

You can use gem list to list all gems available from a remote gem server:

gem list -r --clear-sources -s 'https://user:password@gemserver.tld/'

This is useful to debug cases where Bundler complains of a gem existing in more than one gem source.

Start Rails console or server with debugger

When you require the Ruby debugger to be available from your Rails console (e.g. you want to inspect a method's magic), you need to enable it explicitly:
script/console --debugger

If you cannot access local variables etc, see this card.

WEBrick

For WEBrick, enable it similarly:
script/server --debugger

Highlighted prompt for production shells

Insert into ~/.bashrc and reload afterwards: source ~/.bashrc

PS1='${debian_chroot:+($debian_chroot)}\[\033[41;33m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Capistrano: Different usernames for each server

If you have different users for different servers, don't use set :user. Encode the username into the server definition instead:

server "username@servername.tld", :app, :web, :cron, :db, :primary => true

Install the Paperclip gem on Ubuntu servers

You need to install the following packages before you can build the Paperclip gem:

sudo apt-get install imagemagick librmagick-ruby

Install the Nokogiri gem on Ubuntu servers

You need to install the following packages before you can build the Nokogiri gem:

sudo apt-get install libxml2-dev libxslt1-dev

Netty - the Java NIO Client Server Socket Framework - JBoss Community

The Netty project is an effort to provide an asynchronous event-driven network application framework and tools for rapid development of maintainable high performance & high scalability protocol servers & clients.

Integrity | The easy and fun automated continuous integration server

Integrity is the angel watching over your shoulder while you code. As soon as you push your commits, it builds, runs your tests, and makes sure everything works fine.

DNS debug tools

There are several tools for DNS debugging which offer you more or less information. Most of the time the more simple ones, like host oder nslookup will be sufficient.

host

simple DNS lookup utility.

>host heise.de
heise.de has address 193.99.144.80
heise.de has IPv6 address 2a02:2e0:3fe:1001:302::
heise.de mail is handled by 10 relay.heise.de.

nslookup

query Internet domain name servers. Nslookup has two modes: interactive and non-interactive.

>nslookup heise.de
Server:		146.254.160.30
Address:	146.254.160.3...

Caching in Rails < 6.1 may down parts of your application when using public cache control

TL;DR When using Cache-Control on a Rails application, make sure the Vary: Accept header is set.

Proxy caching is a good feature to serve your publicly visible application content faster and reduce load on your servers. It is e.g. available in nginx, but also affects proxies delivered by ISPs.

Unfortunately, there is a little problem in Rails < 6.1 when delivering responses for different MIME-types. Say you have an arbitrary route in your Rails application that is able to respond with regular HTML and JSON. By sending the specific ...