Skip to content

Instantly share code, notes, and snippets.

#http://tools.ietf.org/html/rfc2397
img_path = '...pictures/img.jpg'
img = File.open(img_path, 'rb').read
img_type = case img[0..10]
when /^GIF8/
'gif'
when Regexp.new('^\x89PNG', nil, 'n')
'png'
when Regexp.new('^\xff\xd8\xff\xe0\x00\x10JFIF', nil, 'n'), Regexp.new('^\xff\xd8\xff\xe1(.*){2}Exif', nil, 'n')
'jpeg'
@wteuber
wteuber / intl_time.rb
Created February 1, 2013 10:31
Converts a german date string to a Time object The format of the german date is '%A, %d. %B %Y, %H:%M Uhr' e.g.: 'Dienstag, 29. Januar 2013, 19:27 Uhr'
require 'date'
# Converts a german date string to a Time object
# The format of the german date is '%A, %d. %B %Y, %H:%M Uhr'
# e.g.: 'Dienstag, 29. Januar 2013, 19:27 Uhr'
#
# @params [String] german date
# @return [Time]
def intl_time(de_date)
en_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
@wteuber
wteuber / digit_sum.rb
Created February 5, 2013 09:44
Calculates the digit sum of an integer number
class Integer
def digit_sum(base = 10)
result = 0
number = self
while(number > 0) do
result += number % base
number /= base
end
result
end
@wteuber
wteuber / contradicting_click_events.js
Last active December 15, 2015 06:58
Find contradicting click events on links and submits.
/*Run this code in your js console after the page has been loaded completely*/
/* "!== undefined" at the end simply prevents unwanted output in the js console.*/
$('a').each(function () {
var self = $(this);
if ((self.attr('href') && self.attr('href').indexOf('#') !== 0) && (self.data('events') !== undefined) && (self.data('events').click !== undefined)) {
console.log(this, self.attr('href'), self.data('events'))
}
}) !== undefined
$('[type="submit"]').each(function () {
@wteuber
wteuber / config_env.rb
Created April 2, 2013 07:17
Print full stacktrace from deprecation warnings
MyApp::Application.configure do
#...
# Print deprecation notices to the stderr
#config.active_support.deprecation = :stderr
# Print full stacktrace from deprecation warnings
config.active_support.deprecation = Proc.new { |message, callstack|
$stderr.puts message, callstack
@wteuber
wteuber / encrypt_decrypt.rb
Last active December 18, 2024 10:05
Simply encrypt and decrypt Strings in Ruby.
require 'openssl'
class String
def encrypt(key)
cipher = OpenSSL::Cipher.new('DES-EDE3-CBC').encrypt
cipher.key = Digest::SHA1.hexdigest key
s = cipher.update(self) + cipher.final
s.unpack('H*')[0].upcase
end
@wteuber
wteuber / bulk_rename.sh
Last active March 29, 2018 01:49
bulk rename: Replace all files, directories and occurrences in files of an old name with a new name
# HINT: This gists uses GNU commands. On Mac OS, you might want to adopt the script or use `grep`, `find` and `sed` "--with-default-names".
RENAME_FROM=old_name
RENAME_TO=new_name
# RENAME files, directories from an old name to a new name
# CHECK files manually: find . -type f -name "*$RENAME_FROM*" | grep -v '/\.'
# find . -type f -name "*$RENAME_FROM*" ## find all files that contain the old name
# grep -v '/\.' ## exclude hidden files
@wteuber
wteuber / bulk_replace.sh
Last active October 26, 2016 20:40
batch in-place replacement off content visible files, excluding dot files
# grep -l -R -I --exclude "*.log" -e "old_stuff" . ## list files that contain old_stuff, excluding .log files
# grep -v '/\.' ## don't list dot files
# xargs sed -i "s/old_stuff/new_stuff/g" ## replace (and save) old_stuff with new_stuff in those files.
grep -l -R -I --exclude "*.log" -e "old_stuff" . | grep -v '/\.' | xargs sed -i "s/old_stuff/new_stuff/g"
@wteuber
wteuber / ie8_inspect.js
Created May 2, 2013 08:00
== inspect for IE8 == For quick debugging in IE8's developer tools usage: inspect(window)
/*
because IE8 doesn't support Object.keys...
*/
Object.keys = Object.keys || function(o) {
var result = [];
for(var name in o) {
if (Object.prototype.hasOwnProperty.call(o,name)) {
result.push(name);
}
}
@wteuber
wteuber / breakout_bot.js
Last active March 5, 2021 16:06
minimalistic bot for Google's atari breakout
/* go to https://elgoog.im/breakout/ and run the snippet in your js console (works only for one level): */
hit = function($) { $('#breakout-paddle').style.left = (parseInt($('#breakout-ball').style.left, 10)-50).toString()+'px'; window.setTimeout(function(){ hit($); }, 10)}; hit($);