Permalink
Browse files

make RR methods more consistent and document them

  • Loading branch information...
1 parent b8fffde commit 0fc9fb8dc0dff3837d3376bf2adf7505be6a9981 @jakesgordon committed Aug 25, 2014
Showing with 46 additions and 24 deletions.
  1. +34 −2 README.md
  2. +3 −3 bin/rr
  3. +7 −13 lib/rack-rabbit/client.rb
  4. +2 −6 test/unit/test_client.rb
View
@@ -339,9 +339,41 @@ Communicating with a RackRabbit hosted server can be done using the `rr` binary:
## Client library
Posting a message to a RackRabbit hosted server from within your application can be done using
-the `RR` class...
+the `RR` helper methods...
-TODO: document RR
+ RR.get( "myqueue", "/path/to/resource")
+ RR.post( "myqueue", "/path/to/resource", "content")
+ RR.put( "myqueue", "/path/to/resource", "content")
+ RR.delete( "myqueue", "/path/to/resource")
+ RR.enqueue("myqueue", "/path/to/resource", "content")
+ RR.publish("myexchange", "/path/to/resource", "content")
+
+These methods are wrappers around a more detailed `RackRabbit::Client`:
+
+ client = RackRabbit::Client.new(:host => "127.0.0.1", :port => 5672, :adapter => :bunny)
+
+ client.get( "myqueue", "/path/to/resource")
+ client.post( "myqueue", "/path/to/resource", "content")
+ client.put( "myqueue", "/path/to/resource", "content")
+ client.delete("myqueue", "/path/to/resource")
+ client.enqueue("myqueue", "/path/to/resource", "content")
+ client.publish("myexchange", "/path/to/resource", "content")
+
+ client.disconnect
+
+More advanced options can be passed as the (optional) last parameter, e.g:
+
+ client.post("myqueue", "/path", content.to_json, {
+ :headers => { "additional" => "header" }, # available in the service's rack env
+ :priority => 5, # specify the rabbitMQ priority
+ :content_type => "application/json", # specify the body content_type
+ :content_encoding => "utf-8" # specify the body content_enoding
+ })
+
+ client.publish("myexchange", "/path", "content", {
+ :exchange_type => :topic, # specify the exchange type
+ :routing_key => "my.custom.topic", # specify a custom routing key
+ })
## Supported platforms
View
@@ -88,9 +88,9 @@ when options[:help] then puts parser.to_s
when options[:version] then puts RackRabbit::VERSION
else
case options[:command]
- when :request then puts RR.request(options[:queue], options)
- when :enqueue then puts RR.enqueue(options[:queue], options)
- when :publish then puts RR.publish(options[:exchange], options)
+ when :request then puts RR.request(options[:queue], options[:path], options[:body], options)
+ when :enqueue then puts RR.enqueue(options[:queue], options[:path], options[:body], options)
+ when :publish then puts RR.publish(options[:exchange], options[:path], options[:body], options)
else
puts "Invalid command"
end
@@ -30,32 +30,30 @@ def disconnect
#--------------------------------------------------------------------------
def get(queue, path, options = {})
- request(queue, options.merge(:method => :GET, :path => path))
+ request(queue, path, "", options.merge(:method => :GET))
end
def post(queue, path, body, options = {})
- request(queue, options.merge(:method => :POST, :path => path, :body => body))
+ request(queue, path, body, options.merge(:method => :POST))
end
def put(queue, path, body, options = {})
- request(queue, options.merge(:method => :PUT, :path => path, :body => body))
+ request(queue, path, body, options.merge(:method => :PUT))
end
def delete(queue, path, options = {})
- request(queue, options.merge(:method => :DELETE, :path => path))
+ request(queue, path, "", options.merge(:method => :DELETE))
end
#--------------------------------------------------------------------------
- def request(queue, options = {})
+ def request(queue, path, body, options = {})
id = options[:id] || SecureRandom.uuid # allow dependency injection for test purposes
lock = Mutex.new
condition = ConditionVariable.new
method = options[:method] || :GET
- path = options[:path] || ""
headers = options[:headers] || {}
- body = options[:body] || ""
response = nil
rabbit.with_reply_queue do |reply_queue|
@@ -95,12 +93,10 @@ def request(queue, options = {})
#--------------------------------------------------------------------------
- def enqueue(queue, options = {})
+ def enqueue(queue, path, body, options = {})
method = options[:method] || :POST
- path = options[:path] || ""
headers = options[:headers] || {}
- body = options[:body] || ""
rabbit.publish(body,
:routing_key => queue,
@@ -120,12 +116,10 @@ def enqueue(queue, options = {})
#--------------------------------------------------------------------------
- def publish(exchange, options = {})
+ def publish(exchange, path, body, options = {})
method = options[:method] || :POST
- path = options[:path] || ""
headers = options[:headers] || {}
- body = options[:body] || ""
rabbit.publish(body,
:exchange => exchange,
@@ -188,10 +188,8 @@ def test_ENQUEUE
client = build_client
rabbit = client.rabbit
- response = client.enqueue(QUEUE, {
- :path => "/path",
+ response = client.enqueue(QUEUE, "/path", BODY, {
:method => :PUT,
- :body => BODY,
:priority => PRIORITY,
:content_type => CONTENT::JSON,
:content_encoding => CONTENT::ASCII,
@@ -229,12 +227,10 @@ def test_PUBLISH
client = build_client
rabbit = client.rabbit
- response = client.publish(EXCHANGE, {
+ response = client.publish(EXCHANGE, "/path", BODY, {
:exchange_type => "topic",
:routing_key => ROUTE,
- :path => "/path",
:method => :PUT,
- :body => BODY,
:priority => PRIORITY,
:content_type => CONTENT::JSON,
:content_encoding => CONTENT::ASCII,

0 comments on commit 0fc9fb8

Please sign in to comment.