Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View larsar's full-sized avatar

Lars Preben Sørsdahl larsar

View GitHub Profile
# config/initializers/char_converter.rb
require 'uri'
module Support
class CharConverter
SANITIZE_ENV_KEYS = [
"HTTP_COOKIE", # bad cookie encodings kill rack: https://github.com/rack/rack/issues/225
"HTTP_REFERER",
"PATH_INFO",
# config/initializers/char_converter.rb
require 'uri'
module Support
class CharConverter
def initialize(app)
@app = app
end
Steps to install and run PostgreSQL 9.3 using Homebrew (Mac OS X)
(if you aren't using version 9.2.4, change to the correct version)
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
mv /usr/local/var/postgres /usr/local/var/postgres92
brew upgrade postgresql
initdb /usr/local/var/postgres -E utf8
pg_upgrade -b /usr/local/Cellar/postgresql/9.2.4/bin -B /usr/local/Cellar/postgresql/9.3.0/bin -d /usr/local/var/postgres92 -D /usr/local/var/postgres
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
@larsar
larsar / authentications_controller.rb
Created November 21, 2013 12:33
FEIDE authentication controllers for Rails, using "omniauth". Code written mostly by Christian Martin. https://github.com/cmartin81
class AuthenticationsController < ApplicationController
def index
if current_user
@authentications = current_user.authentications
else
redirect_to root_path
end
end
def create
module Delayed
module Plugins
class Airbrake < Plugin
module Notify
def error(job, error)
::Airbrake.notify_or_ignore(error)
super
end
end
import os
import simplejson
import subprocess
import datetime
f = open('latitude.json', 'r')
locdata = simplejson.loads(f.read());
fd = open('latitude.gpx', 'w')
fd.write("""# <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
@larsar
larsar / wait_for_bg_process.sh
Created May 28, 2013 10:22
Example shell script for forking processes in parallell and then waiting for them to finish.
#!/bin/bash
pids=()
# Snooooze for two seconds
function do_stuff {
sleep 2
}
# Starts 10 processes
@larsar
larsar / dynamic_layout_application_controller.rb
Last active December 15, 2015 04:39
Multiple layouts based on request parameter in Rails. Layout in URL parameter overrides cookies.
class ApplicationController < ActionController::Base
layout :resolve_layout
# ....
private
def resolve_layout
layout ||= params[:layout]
layout ||= cookies[:layout]
@larsar
larsar / syslog.py
Created March 19, 2013 12:43
Syslogging from Python
import logging
from logging.handlers import SysLogHandler import socket
def syslogger(hostname, tag):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
syslog = SysLogHandler(address=('syslogserver', 514), facility="local4")
formatter = logging.Formatter(hostname + ' ' + tag + ' %(levelname)s %(message)s')
syslog.setFormatter(formatter)
logger.addHandler(syslog)
@larsar
larsar / add_sendgrid_recipient_list_with_non_duplicates.rb
Created March 8, 2013 21:59
Adds email,name from a csv file to a new Sendgrid recipient list. Only emails that are not already on an existing recipient list are added to the new one.
require 'CSV'
require 'rest_client'
require 'json'
USERNAME=""
PASSWORD=""
URL_POSTFIX = "&api_user=#{USERNAME}&api_key=#{PASSWORD}"
SOURCE_LIST=""
NEW_LIST=""
CSV_FILE=""