Skip to content

Instantly share code, notes, and snippets.

View basgys's full-sized avatar

Bastien Gysler basgys

View GitHub Profile
@basgys
basgys / person.java
Created September 23, 2011 21:31
Person class
package ch.hegarc.ig.person.business;
public class Person {
private Integer number;
private String firstName;
private String lastName;
public Person() {
this(null, null, null);
@basgys
basgys / main.java
Created September 23, 2011 21:35
Main class
package ch.hegarc.ig.person;
import ch.hegarc.ig.person.business.Person;
public class Main {
public static void main(String[] args) {
Person p1 = new Person(1, "Bastien", "Gysler");
p1.print();
@basgys
basgys / gist:1482038
Created December 15, 2011 17:47
Trigger call to all passenger instances
#!/bin/bash
if [ -z $1 ]; then
echo "Usage : "$0" http://passenger_address"
else
echo "address to call: "$1
passenger-status --verbose | grep 'Password:' | cut -d ':' -f2 | while read line; do
echo "Calling : "$line # Passenger instance password
curl --header "X-Passenger-Connect-Password: "$line $1 > /dev/null 2>&1
@basgys
basgys / gist:2192992
Created March 25, 2012 11:19
Roman to Arabic number converter
input = "MCMXCIX" # 1999
# Roman to Arabic mapper
r2a = {"I" => 1, "V" => 5, "X" => 10, "L" => 50, "C" => 100, "D" => 500, "M" => 1000}
# Translate input to an array of arabic numbers
numbers = input.split(//).map {|c| r2a[c]}
count = numbers.count-1
# Compute result (more infos @ http://en.wikipedia.org/wiki/Roman_numerals)
@basgys
basgys / gist:2193099
Created March 25, 2012 11:48
Morse to english
map = {
'.-' => 'A',
'-...' => 'B',
'-.-.' => 'C',
'-..' => 'D',
'.' => 'E',
'..-.' => 'F',
'--.' => 'G',
'....' => 'H',
'..' => 'I',
@basgys
basgys / logs.rake
Created May 11, 2012 14:21
Rake task to tail multiple log files
desc "Tail logs"
task :logs => :environment do |t, args|
log_files = ['log/searchd.log', 'log/searchd.query.log', 'log/resque.log', 'log/development.log']
system "tail -f -n 2 #{log_files.map {|f| "#{Rails.root}/#{f}"}.join(' ')} | perl -pe 's/^(==>)(.*)(<==)$/\e[1;33;40m$&\e[0m/g'"
end
@basgys
basgys / gist:2823166
Created May 29, 2012 07:48
Creates an accessible URL to your local machine via SSH
# config/tunnel.yml
public_host_username: username_ssh
public_host: my_public_domain
public_port: 8000
local_host: 0.0.0.0
local_port: 3000
# lib/tasks/tunnel.rake
namespace :tunnel do
desc "Start a ssh tunnel"
@basgys
basgys / gist:3181680
Last active October 7, 2015 14:48
Retryable transaction
class ActiveRecord::Base
# Retryable transaction
# =====================
#
# Author : Bastien Gysler (http://www.bastiengysler.com)
#
# ActiveRecord::Base.retryable_transaction(tries: 1, :when => ActiveRecord::RecordNotUnique) do |on|
# on.transaction do
# # transaction code here
@basgys
basgys / gist:3358471
Created August 15, 2012 10:19
Reload image on failure with exponential backoff
$("img.thumb").error(function() {
var path = $(this).data('src');
var img = $(this);
// Put back mock image
$(img).attr('src', 'http://placehold.it/56x56');
// Increment loading counter
var attempt = ($(img).data('attempt') || 0);
attempt++;
@basgys
basgys / gist:3482915
Created August 26, 2012 19:31
Transfer BLOB to Amazon S3 with Paperclip
# loop on something .each do |my_model|
blob_to_file(my_model.blob_image) do |image_file|
my_model.update_attributes(s3_image: image_file)
end
# end
def blob_to_file(blob)
tfile = Tempfile.new('blob_to_file')
tfile.write(blob.force_encoding("UTF-8"))
yield(tfile) if block_given?