Skip to content

Instantly share code, notes, and snippets.

View kriansa's full-sized avatar

Daniel Pereira kriansa

View GitHub Profile
@kriansa
kriansa / exec_with_timeout.rb
Last active August 29, 2015 14:08
Tiny helper to execute a external command with a timeout.
# Open stdout and stderr streams and start external executable with timeout.
# The timeout is given in seconds.
#
# Block form:
#
# exec_with_timeout(timeout, [env,] cmd... [, opts]) {|stdout, stderr|
# stdout.read
# ...
# }
#
@kriansa
kriansa / CitiesController.rb
Created August 14, 2013 19:00
Automatizando a seleção de Municípios/UF
class CitiesController < ApplicationController
def all_by_state_id
uf = params[:id]
cities = City.joins(:state).where('states.id' => uf).select('cities.id, cities.name')
render :json => { data: { cities: cities } }
end
end
@kriansa
kriansa / string-normalize.js
Created July 25, 2013 21:00
Function to translate accented characters to ASCII equivalents. Stripped out from SugarJS. (https://github.com/andrewplummer/Sugar/)
/**
* Returns the string with accented and non-standard Latin-based characters converted into ASCII approximate equivalents.
*
* @returns {String}
*/
String.prototype.normalize = function() {
var NormalizeSource = {
'A': /[AⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ]/g,
'B': /[BⒷBḂḄḆɃƂƁ]/g,
'C': /[CⒸCĆĈĊČÇḈƇȻꜾ]/g,
@kriansa
kriansa / gist:5885902
Created June 28, 2013 16:12
Simple Javascript template parser
/**
* Replaces any ${variables} in the template string by the data object
*/
var parseTemplate = function(template, data) {
return template.replace(/\$\{([\w\d]+)\}/g, function(token, variable){
return data[variable];
});
}
@kriansa
kriansa / myezbkp.sh
Created October 31, 2011 15:33
Easy MySQL incremental backups
#!/bin/sh
# ========================================================= #
# MyEzBkp
# Easy and simple backup of your entire MySQL Server.
#
# Ver: 1.0
# By: Kriansa
#
# This script requires root privileges to run!
# Tested only in RHEL based distros (Fedora & CentOS)