iron_mq 1.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,56 @@
1
+ IronMQ Ruby Client
2
+ -------------
3
+
4
+ Getting Started
5
+ ==============
6
+
7
+ Install the gem:
8
+
9
+ gem install ironmq
10
+
11
+ Create an IronMQ client object:
12
+
13
+ @ironmq = IronMQ::Client.new('token'=>'MYTOKEN', 'project_id'=>'MYPROJECTID')
14
+
15
+ You can get your `token` and `project_id` at http://www.iron.io .
16
+
17
+
18
+ The Basics
19
+ =========
20
+
21
+ **Push** a message on the queue:
22
+
23
+ msg = @ironmq.messages.post("hello world!")
24
+ p msg
25
+
26
+ **Pop** a message off the queue:
27
+
28
+ msg = @ironmq.messages.get()
29
+ p msg
30
+
31
+ When you pop/get a message from the queue, it will NOT be deleted. It will eventually go back onto the queue after
32
+ a timeout if you don't delete it (default timeout is 10 minutes).
33
+
34
+ **Delete** a message from the queue:
35
+
36
+ res = msg.delete # or @ironmq.messages.delete(msg["id"])
37
+ p res
38
+
39
+ Delete a message from the queue when you're done with it.
40
+
41
+ Queue Selection
42
+ ===============
43
+
44
+ One of the following:
45
+
46
+ 1. Pass `:queue_name=>'my_queue'` into IronMQ::Client.new
47
+ 1. `@client.queue_name = 'my_queue'`
48
+ 1. Pass `:queue_name=>'my_queue'` into any post(), get(), or delete()
49
+
50
+ Queue Information
51
+ =================
52
+
53
+ queue = @client.queues.get(:name=>@client.queue_name)
54
+ puts "size: #{queue.size}"
55
+
56
+
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler2'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "iron_mq"
8
+ gem.summary = "Ruby client for IronMQ"
9
+ gem.description = "Ruby client for IronMQ"
10
+ gem.email = "travis@iron.io"
11
+ gem.homepage = "http://www.iron.io"
12
+ gem.authors = ["Travis Reeder"]
13
+ gem.add_dependency 'typhoeus'
14
+ gem.required_ruby_version = '>= 1.9'
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler2"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ task :default => :test
29
+
30
+ require 'rdoc/task'
31
+ Rake::RDocTask.new do |rdoc|
32
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
33
+
34
+ rdoc.rdoc_dir = 'doc'
35
+ rdoc.title = "iron_mq #{version}"
36
+ rdoc.rdoc_files.include('README*')
37
+ rdoc.rdoc_files.include('lib/**/*.rb')
38
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 2
4
+ :patch: 7
5
+ :build:
data/ironmq.gemspec ADDED
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "ironmq"
8
+ s.version = "1.2.7"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Travis Reeder"]
12
+ s.date = "2011-12-08"
13
+ s.description = "Ruby client for IronMQ"
14
+ s.email = "travis@iron.io"
15
+ s.extra_rdoc_files = [
16
+ "README.markdown"
17
+ ]
18
+ s.files = [
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "ironmq.gemspec",
23
+ "lib/ironmq.rb",
24
+ "lib/ironmq/client.rb",
25
+ "lib/ironmq/messages.rb",
26
+ "lib/ironmq/queues.rb",
27
+ "test/long_run.rb",
28
+ "test/quick_run.rb",
29
+ "test/test_base.rb",
30
+ "test/test_ironmq.rb"
31
+ ]
32
+ s.homepage = "http://www.iron.io"
33
+ s.require_paths = ["lib"]
34
+ s.required_ruby_version = Gem::Requirement.new(">= 1.9")
35
+ s.rubygems_version = "1.8.11"
36
+ s.summary = "Ruby client for IronMQ"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<typhoeus>, [">= 0"])
43
+ s.add_runtime_dependency(%q<typhoeus>, [">= 0"])
44
+ else
45
+ s.add_dependency(%q<typhoeus>, [">= 0"])
46
+ s.add_dependency(%q<typhoeus>, [">= 0"])
47
+ end
48
+ else
49
+ s.add_dependency(%q<typhoeus>, [">= 0"])
50
+ s.add_dependency(%q<typhoeus>, [">= 0"])
51
+ end
52
+ end
53
+
@@ -0,0 +1,126 @@
1
+ require 'json'
2
+ require 'typhoeus'
3
+ require 'logger'
4
+
5
+ module IronMQ
6
+
7
+ class Client
8
+
9
+ attr_accessor :token, :project_id, :queue_name, :logger
10
+
11
+ def initialize(options={})
12
+ @logger = Logger.new(STDOUT)
13
+ @logger.level=Logger::INFO
14
+
15
+ @token = options[:token] || options['token']
16
+ @project_id = options[:project_id] || options['project_id']
17
+ @queue_name = options[:queue_name] || options['queue_name'] || "default"
18
+ @scheme = options[:scheme] || options['scheme'] || "https"
19
+ @host = options[:host] || options['host'] || "mq-aws-us-east-1.iron.io"
20
+ @port = options[:port] || options['port'] || 443
21
+
22
+ @base_url = "#{@scheme}://#{@host}:#{@port}/1"
23
+
24
+ end
25
+
26
+ def messages
27
+ return Messages.new(self)
28
+ end
29
+
30
+ def queues
31
+ return Queues.new(self)
32
+ end
33
+
34
+ def base_url
35
+ #"#{scheme}://#{host}:#{port}/1"
36
+ @base_url
37
+ end
38
+
39
+ def full_url(path)
40
+ url = "#{base_url}#{path}"
41
+ url
42
+ end
43
+
44
+ def common_req_hash
45
+ {
46
+ :headers=>{"Content-Type" => 'application/json',
47
+ "Authorization"=>"OAuth #{@token}",
48
+ "User-Agent"=>"IronMQ Ruby Client"}
49
+ }
50
+ end
51
+
52
+ def post(path, params={})
53
+ url = full_url(path)
54
+ @logger.debug 'url=' + url
55
+ #response = @http_sess.post(path + "?oauth=#{@token}", {'oauth' => @token}.merge(params).to_json, {"Content-Type" => 'application/json'})
56
+ req_hash = common_req_hash
57
+ req_hash[:body] = params.to_json
58
+ response = Typhoeus::Request.post(url, req_hash)
59
+ @logger.debug 'typhoeus response=' + response.inspect
60
+ res = check_response(response)
61
+ #@logger.debug 'response: ' + res.inspect
62
+ #body = response.body
63
+ #res = JSON.parse(body)
64
+ return res, response.code
65
+ end
66
+
67
+ def get(path, params={})
68
+ url = full_url(path)
69
+ @logger.debug 'url=' + url
70
+ req_hash = common_req_hash
71
+ req_hash[:params] = params
72
+ @logger.debug 'req_hash=' + req_hash.inspect
73
+ response = Typhoeus::Request.get(url, req_hash)
74
+ res = check_response(response)
75
+ return res, response.code
76
+ end
77
+
78
+ def delete(path, params={})
79
+ url = "#{base_url}#{path}"
80
+ @logger.debug 'url=' + url
81
+ req_hash = common_req_hash
82
+ req_hash[:params] = params
83
+ response = Typhoeus::Request.delete(url, req_hash)
84
+ res = check_response(response)
85
+ #body = response.body
86
+ #res = JSON.parse(body)
87
+ #@logger.debug 'response: ' + res.inspect
88
+ return res, response.code
89
+ end
90
+
91
+ def check_response(response)
92
+ # response.code # http status code
93
+ #response.time # time in seconds the request took
94
+ #response.headers # the http headers
95
+ #response.headers_hash # http headers put into a hash
96
+ #response.body # the response body
97
+ status = response.code
98
+ body = response.body
99
+ # todo: check content-type == application/json before parsing
100
+ @logger.debug "response code=" + status.to_s
101
+ @logger.debug "response body=" + body.inspect
102
+ res = JSON.parse(body)
103
+ if status < 400
104
+
105
+ else
106
+ raise IronMQ::Error.new(res["msg"], :status=>status)
107
+ end
108
+ res
109
+ end
110
+
111
+ end
112
+
113
+ class Error < StandardError
114
+ def initialize(msg, options={})
115
+ super(msg)
116
+ @options = options
117
+ end
118
+
119
+ def status
120
+ @options[:status]
121
+ end
122
+ end
123
+
124
+
125
+ end
126
+
@@ -0,0 +1,78 @@
1
+ module IronMQ
2
+ class Messages
3
+
4
+ attr_accessor :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def path(options={})
11
+ path = "/projects/#{@client.project_id}/queues/#{options[:queue_name] || @client.queue_name}/messages"
12
+ end
13
+
14
+ # options:
15
+ # :queue_name => can specify an alternative queue name
16
+ # :timeout => amount of time before message goes back on the queue
17
+ def get(options={})
18
+ begin
19
+ params = nil
20
+ if options[:timeout]
21
+ params = {:timeout => options[:timeout]}
22
+ end
23
+ res, status = @client.get(path(options), params)
24
+ return Message.new(self, res)
25
+ rescue IronMQ::Error => ex
26
+ if ex.status == 404
27
+ return nil
28
+ end
29
+ raise ex
30
+ end
31
+
32
+
33
+ end
34
+
35
+ # options:
36
+ # :queue_name => can specify an alternative queue name
37
+ def post(payload, options={})
38
+ res, status = @client.post(path(options), :body=>payload)
39
+ return Message.new(self, res)
40
+ end
41
+
42
+ def delete(message_id, options={})
43
+ path2 = "#{self.path(options)}/#{message_id}"
44
+ res, status = @client.delete(path2)
45
+ res
46
+ end
47
+
48
+ end
49
+
50
+ class Message
51
+
52
+ def initialize(messages, res)
53
+ @messages = messages
54
+ @data = res
55
+ end
56
+
57
+ def raw
58
+ @data
59
+ end
60
+
61
+ def [](key)
62
+ raw[key]
63
+ end
64
+
65
+ def id
66
+ raw["id"]
67
+ end
68
+
69
+ def body
70
+ raw["body"]
71
+ end
72
+
73
+ def delete
74
+ @messages.delete(self.id)
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,72 @@
1
+ module IronMQ
2
+ class Queues
3
+
4
+ attr_accessor :client
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def path(options={})
11
+ path = "/projects/#{@client.project_id}/queues"
12
+ end
13
+
14
+ def list(options={})
15
+ ret = []
16
+ res, status = @client.get("#{path(options)}", options)
17
+ res.each do |q|
18
+ #p q
19
+ q = Queue.new(self, q)
20
+ ret << q
21
+ end
22
+ ret
23
+ end
24
+
25
+ # options:
26
+ # :name => can specify an alternative queue name
27
+ def get(options={})
28
+ res, status = @client.get("#{path(options)}/#{options[:name]}")
29
+ return Queue.new(self, res)
30
+ end
31
+
32
+
33
+ end
34
+
35
+ class Queue
36
+
37
+ def initialize(queues, res)
38
+ @queues = queues
39
+ @data = res
40
+ end
41
+
42
+ def raw
43
+ @data
44
+ end
45
+
46
+ def [](key)
47
+ raw[key]
48
+ end
49
+
50
+ def id
51
+ raw["id"]
52
+ end
53
+
54
+ def name
55
+ raw["name"]
56
+ end
57
+
58
+ def size
59
+ return raw["size"] if raw["size"]
60
+ return @size if @size
61
+ q = @queues.get(:name=>name)
62
+ @size = q.size
63
+ return @size
64
+ end
65
+
66
+ # def delete
67
+ # @messages.delete(self.id)
68
+ # end
69
+ end
70
+
71
+ end
72
+
data/lib/ironmq.rb ADDED
@@ -0,0 +1,3 @@
1
+ require_relative 'ironmq/queues'
2
+ require_relative 'ironmq/messages'
3
+ require_relative 'ironmq/client'
data/test/long_run.rb ADDED
@@ -0,0 +1,57 @@
1
+ require 'yaml'
2
+ require 'concur'
3
+ begin
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'ironmq')
5
+ rescue Exception => ex
6
+ puts "Could NOT load current ironmq: " + ex.message
7
+ raise ex
8
+ end
9
+
10
+ @config = YAML::load_file(File.expand_path(File.join("~", "Dropbox", "configs", "ironmq_ruby", "test", "config.yml")))
11
+ @client = IronMQ::Client.new(@config['ironmq'])
12
+ @client.queue_name = 'ironmq-gem-long'
13
+ @num_to_add = @config['count']
14
+
15
+ start = Time.now
16
+ puts "Queuing #{@num_to_add} items at #{start}..."
17
+ executor = Concur::Executor.new_thread_pool_executor(50)
18
+ @num_to_add.times do |i|
19
+ task = executor.execute do
20
+ begin
21
+ puts "POST #{i}..."
22
+ res = @client.messages.post("hello world! #{i}")
23
+ rescue => ex
24
+ puts "ERROR! #{ex.class.name}: #{ex.message} -- #{ex.backtrace.inspect}"
25
+ raise ex
26
+ end
27
+ end
28
+ end
29
+
30
+ i = 0
31
+ while executor.queue_size > 0 do
32
+ i += 1
33
+ puts "waiting #{i}, queue size=#{executor.queue_size}"
34
+ sleep 0.5
35
+ end
36
+
37
+
38
+ put_time = (Time.now.to_f - start.to_f)
39
+ puts "Finished pushing in #{put_time} seconds"
40
+
41
+ exit if true
42
+
43
+ start = Time.now
44
+ puts "Getting and deleting #{@num_to_add} items at #{start}..."
45
+ @num_to_add.times do |i|
46
+ puts "GET #{i}..."
47
+ res = @client.messages.get()
48
+ p res
49
+ puts "DELETE #{i}..."
50
+ res = @client.messages.delete(res["id"])
51
+ p res
52
+ end
53
+
54
+ puts "Finished pushing #{@num_to_add} items in #{put_time} seconds."
55
+ puts "Finished getting and deleting #{@num_to_add} items in #{(Time.now.to_f - start.to_f)} seconds."
56
+
57
+ executor.shutdown
data/test/quick_run.rb ADDED
@@ -0,0 +1,28 @@
1
+ # Put config.yml file in ~/Dropbox/configs/ironmq_gem/test/config.yml
2
+ require_relative 'test_base'
3
+
4
+ class QuickRun < TestBase
5
+
6
+ def setup
7
+ super
8
+ @client.queue_name = 'ironmq-gem-quick'
9
+ end
10
+
11
+ def test_basics
12
+ res = @client.messages.post("hello world!")
13
+ p res
14
+
15
+ res = @client.messages.get()
16
+ p res
17
+
18
+ res = @client.messages.delete(res["id"])
19
+ p res
20
+
21
+ res = @client.messages.get()
22
+ p res
23
+
24
+ end
25
+
26
+
27
+ end
28
+
data/test/test_base.rb ADDED
@@ -0,0 +1,21 @@
1
+ gem 'test-unit'
2
+ require 'test/unit'
3
+ require 'yaml'
4
+ begin
5
+ require File.join(File.dirname(__FILE__), '../lib/ironmq')
6
+ rescue Exception => ex
7
+ puts "Could NOT load current ironmq: " + ex.message
8
+ raise ex
9
+ end
10
+
11
+
12
+ class TestBase < Test::Unit::TestCase
13
+ def setup
14
+ puts 'setup'
15
+ @config = YAML::load_file(File.expand_path(File.join("~", "Dropbox", "configs", "ironmq_ruby", "test", "config.yml")))
16
+ @client = IronMQ::Client.new(@config['ironmq'])
17
+ @client.logger.level = Logger::DEBUG
18
+ @client.queue_name = 'ironmq-ruby-tests'
19
+
20
+ end
21
+ end
@@ -0,0 +1,111 @@
1
+ # Put config.yml file in ~/Dropbox/configs/ironmq_gem/test/config.yml
2
+
3
+ gem 'test-unit'
4
+ require 'test/unit'
5
+ require 'yaml'
6
+ require_relative 'test_base'
7
+
8
+ class IronMQTests < TestBase
9
+ def setup
10
+ super
11
+
12
+ puts 'clearing queue'
13
+ while res = @client.messages.get()
14
+ p res
15
+ puts res.body.to_s
16
+ res.delete
17
+ end
18
+ puts 'cleared.'
19
+
20
+ end
21
+
22
+ def test_basics
23
+
24
+ res = @client.messages.post("hello world!")
25
+ p res
26
+ queue = @client.queues.get(:name=>@client.queue_name)
27
+ assert queue.size == 1
28
+
29
+ res = @client.messages.get()
30
+ p res
31
+
32
+ res = @client.messages.delete(res["id"])
33
+ p res
34
+ puts "shouldn't be any more"
35
+ res = @client.messages.get()
36
+ p res
37
+ assert res.nil?
38
+
39
+ queue = @client.queues.get(:name=>@client.queue_name)
40
+ assert queue.size == 0
41
+
42
+ res = @client.messages.post("hello world 2!")
43
+ p res
44
+
45
+ msg = @client.messages.get()
46
+ p msg
47
+
48
+ res = msg.delete
49
+ p res
50
+
51
+ puts "shouldn't be any more"
52
+ res = @client.messages.get()
53
+ p res
54
+ assert res.nil?
55
+ end
56
+
57
+ # TODO: pass :timeout in post/get messages and test those
58
+ def test_timeout
59
+ res = @client.messages.post("hello world timeout!")
60
+ p res
61
+
62
+ msg = @client.messages.get()
63
+ p msg
64
+
65
+ msg4 = @client.messages.get()
66
+ p msg4
67
+ assert msg4.nil?
68
+
69
+ puts 'sleeping 45 seconds...'
70
+ sleep 45
71
+
72
+ msg3 = @client.messages.get()
73
+ p msg3
74
+ assert msg3.nil?
75
+
76
+ puts 'sleeping another 45 seconds...'
77
+ sleep 45
78
+
79
+ msg2 = @client.messages.get()
80
+ assert msg.id == msg2.id
81
+
82
+ msg2.delete
83
+
84
+ end
85
+
86
+ def test_queues
87
+ res = @client.queues.list()
88
+ puts "res.size=" + res.size.to_s
89
+ res.each do |q|
90
+ puts "queue_name: " + q.name
91
+ puts "queue size: " + q.size.to_s
92
+ assert q.size >= 0
93
+ end
94
+ assert res.size > 0
95
+
96
+ res = @client.queues.list(:page=>2)
97
+ puts "res.size=" + res.size.to_s
98
+ res.each do |q|
99
+ p q.name
100
+ end
101
+ assert res.size == 0
102
+
103
+
104
+ end
105
+
106
+ def test_delay
107
+ # TODO
108
+ end
109
+
110
+ end
111
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iron_mq
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.7
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Travis Reeder
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: typhoeus
16
+ requirement: &12788940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *12788940
25
+ - !ruby/object:Gem::Dependency
26
+ name: typhoeus
27
+ requirement: &12788040 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *12788040
36
+ description: Ruby client for IronMQ
37
+ email: travis@iron.io
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files:
41
+ - README.markdown
42
+ files:
43
+ - README.markdown
44
+ - Rakefile
45
+ - VERSION.yml
46
+ - ironmq.gemspec
47
+ - lib/ironmq.rb
48
+ - lib/ironmq/client.rb
49
+ - lib/ironmq/messages.rb
50
+ - lib/ironmq/queues.rb
51
+ - test/long_run.rb
52
+ - test/quick_run.rb
53
+ - test/test_base.rb
54
+ - test/test_ironmq.rb
55
+ homepage: http://www.iron.io
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '1.9'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.11
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Ruby client for IronMQ
79
+ test_files: []