Skip to content

Instantly share code, notes, and snippets.

View francisco-rojas's full-sized avatar

Francisco Rojas francisco-rojas

  • San Jose, Costa Rica
View GitHub Profile
@francisco-rojas
francisco-rojas / chapter2.md
Last active August 29, 2015 14:03
Metaprogramming Ruby 2: Book Notes

Open Classes (also known as Monkey Patching)

Instead of creating a class that performs common operations on strings like this:

class StringUtils
  def self.to_alphanumeric(s)
    s.gsub(/[^\w\s]/, '')
  end
end
@francisco-rojas
francisco-rojas / Ubuntu Logrotate for Rails.md
Last active August 29, 2015 14:11
A basic guide on how to configure log rotation for a Rails app in Ubuntu.

To avoid the logs growing too large logrotation can be configured with Linux's logrotate tool. The logrotation configuration file is located at: /etc/logrotate.d/your_app_name Example of logrotate configuration file for a Rails app (remember to remove the comments!):

# monitor the /home/deploy/path/to/my/app/shared/log/*.log file
/home/deploy/path/to/my/app/shared/log/*.log {
        weekly # rotate weekly
        missingok #  avoids halting on any error and carries on with the next log file
        rotate 52 # 52 days worth of logs would be kept
 compress # compress using gzip
@francisco-rojas
francisco-rojas / Chapter1.md
Last active August 29, 2015 14:11
Ruby Best Practices: Book Notes

Chapter 1: Driving Code Through Tests

retry and redo

redo and retry are both used to re-execute parts of a loop. But they differ in how much they re-execute: redo only repeats the current iteration, while retry repeats the whole loop from the start.

redo example:

(0..5).each do |i|
@francisco-rojas
francisco-rojas / Redis Utils.md
Last active August 29, 2015 14:12
Redis Useful Commands

Redis Installation (Ubuntu)

  • sudo apt-get install -y python-software-properties
  • sudo add-apt-repository -y ppa:rwky/redis
  • sudo apt-get update
  • sudo apt-get install -y redis-server

Start Redis Server

For development

# CLOSURES IN RUBY Paul Cantrell https://innig.net
# Email: username "cantrell", domain name "pobox.com"
# I recommend executing this file, then reading it alongside its output.
#
# Alteratively, you can give yourself a sort of Ruby test by deleting all the comments,
# then trying to guess the output of the code!
# A closure is a block of code which meets three criteria:
#
Name Aliases Description
$! $ERROR_INFO The exception information message set by the last 'raise' (last exception thrown).
$@ $ERROR_POSITION Array of the backtrace of the last exception thrown.
$& $MATCH The string matched by the last successful pattern match in this scope.
$` $PREMATCH The string to the left of the last successful match.
$' $POSTMATCH The string to the right of the last successful match.
$+ $LAST_PAREN_MATCH The last group of the last successful match.
$1 to $9 The Nth group of the last successful regexp match.
$~ $LAST_MATCH_INFO The information about the last match in the current scope.
@francisco-rojas
francisco-rojas / rails_cache_permissions_for_nginx.md
Last active August 29, 2015 14:18
Permissions of tmp and cache folder in rails for nginx (To resolve access denied)

In order to have your cache always created with a group of www-data you need to do the following:

cd yourrailsapp
chgrp www-data ./tmp
sudo chmod g+s tmp

This sets the group id on your folder so that everything underneath tmp gets created with the correct permissions

References:

@francisco-rojas
francisco-rojas / curl.md
Created May 19, 2015 17:55
CURL recipies

Using curl to log in setting the cookie and extracting the CSRF token:

curl http://localhost:3000/users/sign_in --cookie-jar cookie | grep csrf

curl http://localhost:3000/users/sign_in -L --data "user[email]=user@users.com&user[password]=my_password&authenticity_token=oy4J2HVPezOc5JrTXMyaSjeCDHhN%2Bv5yVzlvShm%2FjFc%3D" --cookie cookie
@francisco-rojas
francisco-rojas / postgresql.md
Created May 19, 2015 17:56
PostgreSQL recipies

To create a Read only user

Access postgres console:

sudo -u postgres psql

Create user and grant read access to the database

CREATE USER user_name WITH ENCRYPTED PASSWORD 'your_password';
CREATE DATABASE user_name;

###du Summarize disk usage of each FILE, recursively for directories sorted by size

sudo du -x -h / | sort -n | tail -40

###truncate Truncate files to size 0MB

truncate -s 0 production.log