Permalink
Browse files

Merge branch 'pushq' into test_webhook

Conflicts:
	test/tmp.rb
  • Loading branch information...
2 parents 3134b6c + 8e2e143 commit a64d00d103d9ad9680fe81f884bef62117720514 @treeder treeder committed Dec 10, 2012
Showing with 431 additions and 102 deletions.
  1. +13 −16 Gemfile.lock
  2. +2 −2 iron_mq.gemspec
  3. +1 −0 lib/iron_mq.rb
  4. +1 −0 lib/iron_mq/client.rb
  5. +33 −28 lib/iron_mq/messages.rb
  6. +65 −23 lib/iron_mq/queues.rb
  7. +27 −0 lib/iron_mq/response.rb
  8. +33 −0 lib/iron_mq/subscribers.rb
  9. +39 −33 test/quick_run.rb
  10. +217 −0 test/test_push_queues.rb
View
@@ -2,34 +2,31 @@ PATH
remote: .
specs:
iron_mq (2.1.3)
- iron_core (>= 0.4.2)
+ iron_core (>= 0.4.4)
GEM
remote: https://rubygems.org/
specs:
beanstalk-client (1.1.1)
- concur (1.0.0)
- faraday
- faraday
- faraday (0.8.4)
- multipart-post (~> 1.1)
- ffi (1.1.5)
- iron_core (0.4.3)
- rest (>= 2.0.2)
+ concur (1.0.2)
+ ethon (0.5.3)
+ ffi (~> 1.0.11)
+ mime-types (~> 1.18)
+ ffi (1.0.11)
+ iron_core (0.4.4)
+ rest (>= 2.1.1)
mime-types (1.19)
- minitest (4.1.0)
- multipart-post (1.1.5)
+ minitest (4.2.0)
net-http-persistent (2.8)
- rake (0.9.2.2)
- rest (2.0.4)
+ rake (10.0.0)
+ rest (2.1.1)
net-http-persistent
rest-client (>= 0.3.0)
rest-client (1.6.7)
mime-types (>= 1.16)
test-unit (2.5.2)
- typhoeus (0.4.2)
- ffi (~> 1.0)
- mime-types (~> 1.18)
+ typhoeus (0.5.1)
+ ethon (= 0.5.3)
uber_config (1.0.5)
PLATFORMS
View
@@ -2,7 +2,7 @@ require File.expand_path('../lib/iron_mq/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Travis Reeder"]
- gem.email = ["treeder@gmail.com"]
+ gem.email = ["travis@iron.io"]
gem.description = "Ruby client for IronMQ by www.iron.io"
gem.summary = "Ruby client for IronMQ by www.iron.io"
gem.homepage = "https://github.com/iron-io/iron_mq_ruby"
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
gem.required_rubygems_version = ">= 1.3.6"
gem.required_ruby_version = Gem::Requirement.new(">= 1.9")
- gem.add_runtime_dependency "iron_core", ">= 0.4.2"
+ gem.add_runtime_dependency "iron_core", ">= 0.4.4"
gem.add_development_dependency "test-unit"
gem.add_development_dependency "minitest"
View
@@ -1,4 +1,5 @@
require 'rest'
+require_relative 'iron_mq/response'
require_relative 'iron_mq/queues'
require_relative 'iron_mq/messages'
require_relative 'iron_mq/client'
View
@@ -1,6 +1,7 @@
require 'yaml'
require 'iron_core'
+require_relative 'queues'
module IronMQ
View
@@ -3,20 +3,40 @@
module IronMQ
class Messages
- attr_accessor :client
+ attr_reader :client, :queue
- def initialize(client)
+ def initialize(client, queue=nil)
@client = client
+ @queue = queue
end
+
def path(options={})
- path = "projects/#{@client.project_id}/queues/#{CGI::escape(options[:queue_name] || options['queue_name'] || @client.queue_name)}/messages"
+ options[:queue_name] ||= ((@queue ? @queue.name : nil) || @client.queue_name)
+ options[:project_id] = @client.project_id
+ Messages.path(options)
+ end
+
+ def self.path(options)
+ path = "#{Queues.path(options)}/messages"
+ if options[:msg_id]
+ path << "/#{options[:msg_id]}"
+ end
+ path
end
# options:
# :queue_name => can specify an alternative queue name
# :timeout => amount of time before message goes back on the queue
def get(options={})
+ if options.is_a?(String)
+ # assume it's an id
+ puts 'get_by_id'
+ return Message.new(self, {'id'=>options}, options)
+ #r = @client.get(path(:msg_id=>options))
+ #p r
+ #return r
+ end
res = @client.parse_response(@client.get(path(options), options))
ret = []
res["messages"].each do |m|
@@ -72,30 +92,6 @@ def release(message_id, options={})
end
- class ResponseBase
-
- def initialize(res)
- @data = res
- end
-
- def raw
- @data
- end
-
- def [](key)
- raw[key]
- end
-
- def id
- raw["id"]
- end
-
- def msg
- raw["msg"]
- end
-
- end
-
class Message < ResponseBase
def initialize(messages, res, options={})
@@ -104,7 +100,6 @@ def initialize(messages, res, options={})
@options = options
end
-
def body
raw["body"]
end
@@ -118,6 +113,16 @@ def release(options={})
options2 = options.merge(@options) if @options
@messages.release(self.id, options2)
end
+
+ def subscribers(options={})
+ res = @messages.client.get(@messages.path(options.merge(msg_id: id)) + "/subscribers", options)
+ res = @messages.client.parse_response(res)
+ ret = []
+ res['subscribers'].each do |m|
+ ret << Subscriber.new(m, self, options)
+ end
+ ret
+ end
end
end
View
@@ -1,4 +1,5 @@
require 'cgi'
+require_relative 'subscribers'
module IronMQ
class Queues
@@ -9,14 +10,20 @@ def initialize(client)
@client = client
end
- def path(options={})
- path = "projects/#{@client.project_id}/queues"
- if options[:name]
- path << "/#{CGI::escape(options[:name])}"
+ def self.path(options)
+ path = "projects/#{options[:project_id]}/queues"
+ name = options[:name] || options[:queue_name] || options['queue_name']
+ if name
+ path << "/#{CGI::escape(name)}"
end
path
end
+ def path(options={})
+ options[:project_id] = @client.project_id
+ Queues.path(options)
+ end
+
def list(options={})
ret = []
r1 = @client.get("#{path(options)}", options)
@@ -48,27 +55,31 @@ def delete(options={})
# :name => can specify an alternative queue name
def get(options={})
options[:name] ||= @client.queue_name
- res = @client.parse_response(@client.get("#{path(options)}"))
+ r = @client.get("#{path(options)}")
+ puts "HEADERS"
+ p r.headers
+ res = @client.parse_response(r)
return Queue.new(@client, res)
end
# Update a queue
# options:
# :name => if not specified, will use global queue name
- # :subscriptions => url's to subscribe to
+ # :subscribers => url's to subscribe to
def post(options={})
options[:name] ||= @client.queue_name
res = @client.parse_response(@client.post(path(options), options))
- res
p res
-
+ res
end
end
class Queue
+ attr_reader :client
+
def initialize(client, res)
@client = client
@data = res
@@ -91,14 +102,20 @@ def name
end
def reload
- load_queue
+ load_queue(:force => true)
+ end
+
+ def messages
+ raw["messages"]
end
# Used if lazy loading
- def load_queue
+ def load_queue(options={})
+ return if @loaded && !options[:force]
q = @client.queues.get(:name => name)
@client.logger.debug "GOT Q: " + q.inspect
@data = q.raw
+ @loaded = true
q
end
@@ -107,23 +124,43 @@ def clear()
end
def delete_queue()
- @client.queues.delete(:name=>name)
+ @client.queues.delete(:name => name)
+ end
+
+ # updates the Queue object itself
+ def update_queue(options)
+ @client.queues.post(options.merge(:name => name))
end
def size
- return raw["size"] if raw["size"]
- return @size if @size
- q = load_queue()
- @size = q.size
- @size
+ load_queue()
+ return raw["size"]
end
def total_messages
- return raw["total_messages"] if raw["total_messages"]
- return @total_messages if @total_messages
- q = load_queue()
- @total_messages = q.total_messages
- @total_messages
+ load_queue()
+ return raw["total_messages"]
+ end
+
+ def subscribers
+ load_queue()
+ return raw["subscribers"]
+ end
+
+ def add_subscriber(subscriber_hash, options={})
+ puts 'add_subscriber'
+ res = @client.post("#{@client.queues.path(name: name)}/subscribers", subscribers: [subscriber_hash])
+ res = @client.parse_response(res)
+ p res
+ res
+ end
+
+ def remove_subscriber(subscriber_hash)
+ puts 'remove_subscriber'
+ res = @client.delete("#{@client.queues.path(name: name)}/subscribers", {subscribers: [subscriber_hash]}, {"Content-Type"=>@client.content_type})
+ res = @client.parse_response(res)
+ p res
+ res
end
def post(body, options={})
@@ -134,6 +171,11 @@ def get(options={})
@client.messages.get(options.merge(:queue_name => name))
end
+ def delete(id, options={})
+ @client.messages.delete(id, options.merge(:queue_name => name))
+ end
+
+
# This will continuously poll for a message and pass it to the block. For example:
#
# queue.poll { |msg| puts msg.body }
@@ -159,8 +201,8 @@ def poll(options={}, &blk)
end
end
- def delete(id, options={})
- @client.messages.delete(id, options.merge(:queue_name => name))
+ def messages
+ Messages.new(client, self)
end
end
View
@@ -0,0 +1,27 @@
+
+module IronMQ
+
+ class ResponseBase
+
+ attr_reader :raw
+
+ def initialize(raw)
+ @raw = raw
+ end
+
+ def [](key)
+ raw[key]
+ end
+
+ def id
+ raw["id"]
+ end
+
+ def msg
+ raw["msg"]
+ end
+
+ end
+
+
+end
Oops, something went wrong.

0 comments on commit a64d00d

Please sign in to comment.