Skip to content

Instantly share code, notes, and snippets.

#Goes in /etc/init/<name>.conf
description "Unicorn w/Rails"
start on runlevel [2345]
stop on runlevel [016]
setuid <uid>
setgid <gid>
require 'ffi-rzmq'
require 'json'
class ZMQLogger
attr_reader :context
def initialize
@context = ZMQ::Context.new
@logger = @context.socket(ZMQ::PAIR)
@toretore
toretore / _etc_dbus-1_system.d_Upstart.conf
Last active December 15, 2015 00:28
Upstart config for running thin (or anything else, I guess) through rbenv Chances are your OS hasn't enables so-called user jobs by default and you'll have to edit the upstart config for this to work. I've included the upstart config file I've used. Upstart also won't run any user jobs by default because they won't hav been loaded into its syste…
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<!-- Only the root user can own the Upstart name -->
<policy user="root">
<allow own="com.ubuntu.Upstart" />
</policy>
@toretore
toretore / gist:4749992
Created February 10, 2013 15:54
Mongrel2 handlers
It seems I can't make a secret gist public, so here's the URL:
https://gist.github.com/toretore/35eb74a2cac3f214fd4b
@toretore
toretore / flatten.rb
Last active October 14, 2015 17:12 — forked from graingert/flatten.rb
module Fluent
class TextParser
class StringifiedValuesJSONParser < JSONParser
Plugin.register_parser("stringified_values_json", self)
def parse(text)
time, record = super(text)
# replace all Hash (like a dict) values with stringified JSON
record = record.inject({}) { | new_hash, (key, value) |
class JSFunction
def initialize(f)
@f = f
end
def to_json(*a, &b)
@f
end
end
puts JSON.generate({:foo => 'bar', :aFunction => JSFunction.new('function(){ return "foo"; }')})
@toretore
toretore / gist:1922637
Created February 27, 2012 09:02
something like this in the contract model
class Contract < ActiveRecord::Base
has_paper_trail
acts_as_restful_list
CHANGE_FIELDS_MAP = {
'contract_status_id' => {
:association => 'contract_status'
:field => 'name'
}
@toretore
toretore / explicit.rb
Created November 15, 2011 10:03 — forked from mikebaldry/gist:1366593
find subclasses
class PullSource
def self.implementations
@implementations ||= {}
end
def self.register(name)
implementations[name] << self
end
# Returns a set of hidden fields for params passed in the query string such that they are passed
# along with the form. Mostly for use with GET forms like search/filtering where you want to retain
# other state while adding another (<existing params>&q=pr0n)
#
# Options:
#
# :skip - An array of keys to leave out - Only works for first-level keys, i.e. not 'bar' in foo[bar]=baz
def hidden_fields_from_params(opts={})
#Use CGI.parse to get "raw" parameters that haven't been converted into arrays
#and hashes yet. This means it only has to concern itself with the one dimension
@toretore
toretore / README
Created April 9, 2011 23:23
Facebook Canvas apps, CSRF protection & session cookies
Facebook Canvas apps are loaded like this:
* User clicks link to app or tab ("page as tab")
* An iframe is inserted which loads content from the FB server (NOT yours, yet)
Something along the lines of:
<iframe src="http://facebook.com/page_proxy.php?appid=123">
* The FB server returns some HTML & JS with an automatically submitting form:
<form id="humbaba" target="http://you.com/facebook">
<input type="hidden" name="signed_request"/></form>