Skip to content

Instantly share code, notes, and snippets.

View sauloperez's full-sized avatar

Pau Pérez Fabregat sauloperez

View GitHub Profile
@sauloperez
sauloperez / toc.rb
Created October 21, 2016 10:44
Markdown TOC generator
#!/usr/bin/env ruby
FILENAME = ARGV[0]
File.open(FILENAME, 'r') do |f|
f.each_line do |line|
forbidden_words = ['Table of contents', 'define', 'pragma']
next if !line.start_with?("#") || forbidden_words.any? { |w| line =~ /#{w}/ }
title = line.gsub("#", "").strip
# Some required stuff
sudo apt-get install -y git build-essential curl
# Install RVM and Ruby 2.1.2
gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 2.1.2
rvm use --default 2.1.2
@sauloperez
sauloperez / gist:771cfba97cf6e2397e95
Created May 2, 2014 08:54
IEEE locally-assigned MAC addresses
# Generates IEEE locally-assigned MAC addresses
# @return [String] following the pattern [0-9A-Fa-f][26AEae][0-9A-Fa-f]{10}
def mac
mac = ('%0.2X' % rand(256))[0, 1] + %w(2 6 A E).sample
mac << (1..5).map { "%0.2X" % rand(256) }.join
end
description "start passenger stand-alone"
# When to start the service
start on filesystem or runlevel [2345]
# When to stop the service
stop on runlevel [!2345]
# Automatically restart process if crashed
respawn
@sauloperez
sauloperez / gist:8971804
Created February 13, 2014 08:42
Get Rails callback names
User._destroy_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).each { |c| puts "#{c}\n" }
@sauloperez
sauloperez / https
Created February 4, 2014 09:13
Test HTTPS connection from Ruby
#!/usr/bin/env ruby
# Usage: $ https <host> [path]
require 'net/https'
h = Net::HTTP.new(ARGV[0], 443)
h.use_ssl = true
h.ssl_version = :SSLv3
h.verify_mode = OpenSSL::SSL::VERIFY_PEER
@sauloperez
sauloperez / gist:7026493
Created October 17, 2013 15:01
JavaScript WebSockets adapter
// WebSocket Adapter
// -------------------
// It forwards all WebSocket events to the application
// event aggregator (or event bus). The intention of
// this is to decouple the specific websockets implementation
// from the application's use of it.
var WSAdapter = function(options) {
if (options && !options.eventBus) {
@sauloperez
sauloperez / signal_catching.rb
Last active November 11, 2020 11:25
How to catch SIGINT and SIGTERM signals in Ruby
# Signal catching
def shut_down
puts "\nShutting down gracefully..."
sleep 1
end
puts "I have PID #{Process.pid}"
# Trap ^C
Signal.trap("INT") {
@sauloperez
sauloperez / gist:5788542
Created June 15, 2013 15:51
Latex graphicx package configuration to allow figures span two columns
\ifCLASSINFOpdf
\usepackage[pdftex]{graphicx}
% declare the path(s) where your graphic files are
\graphicspath{{../pdf/}{../jpeg/}{./image/}}
% and their extensions so you won't have to specify these with
% every instance of \includegraphics
\DeclareGraphicsExtensions{.pdf,.jpeg,.png,.jpg}
\else
% or other class option (dvipsone, dvipdf, if not using dvips). graphicx
% will default to the driver specified in the system graphics.cfg if no