Skip to content

Instantly share code, notes, and snippets.

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: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 / _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>
require 'ffi-rzmq'
require 'json'
class ZMQLogger
attr_reader :context
def initialize
@context = ZMQ::Context.new
@logger = @context.socket(ZMQ::PAIR)
#Goes in /etc/init/<name>.conf
description "Unicorn w/Rails"
start on runlevel [2345]
stop on runlevel [016]
setuid <uid>
setgid <gid>
rails_env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'production'
worker_processes (rails_env == 'production' ? 5 : 3)
preload_app true
timeout 30
listen 20000
@toretore
toretore / foo.sh
Last active December 18, 2015 04:18
Installing ruby-build, ruby, unicorn, bundling and running a rails app on a server
#sudo as necessary
#First, install ruby-build to /opt/ruby-build
git clone git://github.com/sstephenson/ruby-build.git /opt/ruby-build
#it's a good idea to make sure you have these installed before compiling ruby
#apt-get install libssl-dev libreadline6-dev zlib1g-dev
#All ruby installations/versions are going to live in /opt/rubies
mkdir /opt/rubies
@toretore
toretore / README.md
Last active December 19, 2015 10:49
Rails 0MQ logger

These files work together to extract useful data from a Rails application and publish it through a ZeroMQ PUSH socket as JSON.

The code in application_controller.rb will automatically log every request and exception, but you're free to log other things. If you're inside a controller, you can use log_message(atts) or emit_event(atts). These controller methods simply delegate to the global object $ZMQ_LOGGER, which responds to log(atts) and emit(atts). They add the data.http.request-id key if not already present.

The difference between logging and emitting is somewhat blurry, but I like to think of log messages as something that will be saved somewhere and emitted events as ephemeral.

All attributes are expected to use strings as keys. If you use symbols, you may experience issues.

You may use these files as you wish, with one exception: You may not package them into a reusable library and publish it. This is not easily reusable code, and any attempt to make it into a pre-packaged library will result

@toretore
toretore / HOWTO.md
Last active December 26, 2015 03:09

Here's a quick introduction to how to get the best help in #rubyonrails (or any other programming medium). There are competent people who want to help you, but if you follow a few pointers you will make it much easier for them to do so and will get answers to your question much faster.

1. Show us your code

This is the most critical part. You absolutely must show us the code you're having problems with. It is incredibly important that you show us the relevant code. Trying to figure out a programming problem without code is like trying to fix a car without a car.

Use Gist to paste your code. Do not paste your code directly in the channel. Do not use Pastebin. Read this handy guide to learn how to use Gist properly.

Again, just to make sure you understand how incredibly important it is, you must show us your code, no way around it.

@toretore
toretore / h.rb
Last active August 29, 2015 13:56
class Foo
attr_reader :v
def initialize(v)
@v = v
end
def hash
v.hash
end
def eql?(o)
puts "Foo#eql? called"