Skip to content

Instantly share code, notes, and snippets.

@laser
laser / rabbitmq_server.rb
Last active August 29, 2015 14:00
Using RabbitMQ as Message Broker for RPC (Server)
#!/usr/bin/env ruby
require 'bunny'
require 'json'
require './calculator'
calculator = Calculator.new
# connect to RabbitMQ
conn = Bunny.new
conn.start
@laser
laser / rabbitmq_client.rb
Last active August 29, 2015 14:00
Using RabbitMQ as Message Broker for RPC (Client)
#!/usr/bin/env ruby
require 'bunny'
require 'securerandom'
require 'json'
conn = Bunny.new
conn.start
ch = conn.create_channel
q = ch.queue('calc', auto_delete: false)
@laser
laser / http_server.rb
Last active August 29, 2015 14:00
Using HTTP as Transport in RPC System (Server)
#!/usr/bin/env ruby
require 'sinatra'
require './calculator'
calculator = Calculator.new
post '/calc' do
req = JSON.parse(request.body.read)
# call a method on the calculator instance, passing along params
@laser
laser / http_client.rb
Last active August 29, 2015 14:00
RPC System Using HTTP
#!/usr/bin/env ruby
require 'net/http'
require 'securerandom'
require 'json'
uri = URI('http://localhost:4567/calc')
req = Net::HTTP::Post.new(uri.path)
req.body = {
'id' => SecureRandom.hex,
'jsonrpc' => '2.0',
@laser
laser / fancy_redis_client.rb
Last active August 29, 2015 14:00
Fancy Redis Client (using method_missing)
#!/usr/bin/env ruby
require 'redis'
require 'securerandom'
require 'json'
class RedisRpcClient
def initialize(redis_url, list_name)
@redis_client = Redis.connect(url: redis_url)
@list_name = list_name
@laser
laser / benchmark.log
Created April 28, 2014 16:36
RPC messaging through HTTP / Redis / RabbitMQ
Rehearsal --------------------------------------------
RabbitMQ 0.980000 0.570000 1.550000 ( 1.602346)
Redis 0.280000 0.040000 0.320000 ( 0.419955)
HTTP 0.500000 0.200000 0.700000 ( 2.436720)
----------------------------------- total: 2.570000sec
user system total real
RabbitMQ 0.980000 0.580000 1.560000 ( 1.604432)
Redis 0.270000 0.040000 0.310000 ( 0.416424)
HTTP 0.490000 0.190000 0.680000 ( 6.691767)
@laser
laser / rpc_benchmark.rb
Created April 28, 2014 16:39
RPC benchmark
#!/usr/bin/env ruby
require 'net/http'
require 'securerandom'
require 'json'
require 'benchmark'
require 'redis'
require 'bunny'
require 'pry'
uri = URI('http://localhost:4567/calc')
@laser
laser / contact_service.txt
Last active August 29, 2015 14:00
Contact Service API Documentation
# Contact Service API Reference
/api/v1/contacts.json
## Reading
GET /contacts.json HTTP/1.1
Host: example.com
### Fields
@laser
laser / success_curl.sh
Last active August 29, 2015 14:00
First Successful POST to Contact Service
curl -X POST \
-H "Content-Type: application/json" \
-d '{"first_name": "Erin", "last_name": "S-H", "age": 32, "importance_level": 100, "alias": "lzr"}' \
http://example.com/api/v1/contacts.json
@laser
laser / sad_curl.sh
Last active August 29, 2015 14:00
An Unsuccessful POST to Contact Service
curl -X POST \
-H "Content-Type: application/json" \
-d '{"full_name": "Erin S-H", "age": 32, "importance_level": 100, "alias": "laser"}' \
http://example.com/api/v1/contacts.json