Skip to content

Instantly share code, notes, and snippets.

View celldee's full-sized avatar

Chris Duncan celldee

View GitHub Profile
@celldee
celldee / class_creation.rb
Created May 1, 2009 22:22
Ruby - creating objects from strings
Create uninstantiated class from a string
=========================================
Object::const_get('String')
Create instantiated class from a string
=======================================
Object::const_get('String').new
@celldee
celldee / heart_monitor.rb
Created August 11, 2009 15:55
Testing heartbeat monitoring with RabbitMQ and Bunny
require 'bunny' # Change this to point to the test version of bunny.rb otherwise you will probably be asking for the gem
require 'timeout'
b = Bunny.new(:logging => true, :heartbeat => 30) # Set heartbeat to 30 seconds
# Create a new heartbeat frame
hb = Qrack::Transport::Heartbeat.new()
b.start
@celldee
celldee / simple_heartbeat.rb
Created August 16, 2009 00:35
Simple heartbeat example
require 'bunny'
b = Bunny.new(:logging => true, :heartbeat => 15)
b.start
q = b.queue('hb_test')
Thread.new do
q.subscribe {|msg| puts msg[:payload]}
end
@celldee
celldee / Experimental Subscription
Created September 11, 2009 07:27
Bunny Subscription Examples (versions <= 0.8.0)
# This is how Queue#subscribe will work in the next version of Bunny (v0.5.4).
# It's in the 'experimental' branch now.
#
# In order for subscription to work smoothly you need to use a combination of
# Queue#subscribe, Queue#unsubscribe, Queue#ack and Client#qos prefetch. The
# prefetch will allow a controlled number of messages to be released to a
# consumer, which means that Queue#unsubscribe and other methods can be run
# without errors due to out of sequence messages.
#
@celldee
celldee / teepee.rb
Created April 24, 2010 07:22
Camping framework - teepee.rb
# teepee.rb - For all of you camping users who can't find this elsewhere
#!/usr/bin/env ruby
$:.unshift File.dirname(__FILE__) + "/../../lib"
%w(rubygems camping camping/db redcloth acts_as_versioned).each { |lib| require lib }
Camping.goes :Tepee
module Tepee::Models
@celldee
celldee / gist:2313898
Created April 5, 2012 20:35
Handy JSON validation method
require 'json'
def valid_json?(str)
begin
JSON.parse(str)
return true
rescue Exception => e
return false
end
end
@celldee
celldee / bunny_connect.rb
Created December 8, 2012 17:12
Bunny - connect using SSL
require 'bunny'
# Usual arguments
conn = Bunny.new(:host => 'myhost', :ssl => true)
conn.start
puts conn.status # :open
puts conn.ssl? # true
# URI
conn2 = Bunny.new("amqps://myhost/%2F") # %2F is '/' URL encoded
@celldee
celldee / gist:4241084
Created December 8, 2012 17:19
Example RabbitMQ configuration file - rabbitmq.config
# /etc/rabbitmq/rabbitmq.config
#
# Follow the instructions on RabbitMQ website to create certificate authority and certificates -
#
# http://www.rabbitmq.com/ssl.html
#
[
{rabbit, [
{tcp_listeners,[{"127.0.0.1",5672}]},
@celldee
celldee / gist:4247130
Created December 9, 2012 21:33
Bunny - declare queue with queue TTL
q = b.queue('testq', :arguments => {"x-expires" => 120000}) # idle queue expires after 2 mins
@celldee
celldee / gist:4247204
Created December 9, 2012 21:58
Bunny - publish message with per-message TTL
q.publish('test message', :expiration => "60000") # message will reside in queue maximum 1 minute