ironmq 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 1
3
3
  :minor: 2
4
- :patch: 2
4
+ :patch: 3
5
5
  :build:
data/ironmq.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ironmq}
8
- s.version = "1.2.1"
8
+ s.version = "1.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Travis Reeder}]
12
- s.date = %q{2011-10-12}
12
+ s.date = %q{2011-10-13}
13
13
  s.description = %q{Ruby client for IronMQ}
14
14
  s.email = %q{travis@iron.io}
15
15
  s.extra_rdoc_files = [
@@ -34,12 +34,12 @@ Gem::Specification.new do |s|
34
34
  s.specification_version = 3
35
35
 
36
36
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
37
- s.add_runtime_dependency(%q<rest-client>, [">= 0"])
37
+ s.add_runtime_dependency(%q<patron>, [">= 0"])
38
38
  else
39
- s.add_dependency(%q<rest-client>, [">= 0"])
39
+ s.add_dependency(%q<patron>, [">= 0"])
40
40
  end
41
41
  else
42
- s.add_dependency(%q<rest-client>, [">= 0"])
42
+ s.add_dependency(%q<patron>, [">= 0"])
43
43
  end
44
44
  end
45
45
 
data/lib/ironmq/client.rb CHANGED
@@ -1,14 +1,17 @@
1
1
  require 'json'
2
2
  require 'patron'
3
-
3
+ require 'logger'
4
4
 
5
5
  module IronMQ
6
6
 
7
7
  class Client
8
8
 
9
- attr_accessor :token, :project_id, :queue_name, :base_url
9
+ attr_accessor :token, :project_id, :queue_name, :base_url, :logger
10
10
 
11
11
  def initialize(options={})
12
+ @logger = Logger.new(STDOUT)
13
+ @logger.level=Logger::INFO
14
+
12
15
  @token = options[:token] || options['token']
13
16
  @project_id = options[:project_id] || options['project_id']
14
17
  @queue_name = options[:queue_name] || options['queue_name'] || "default"
@@ -34,40 +37,64 @@ module IronMQ
34
37
 
35
38
  def post(path, params={})
36
39
  url = "#{@base_url}#{path}"
37
- puts 'url=' + url
38
- response = @http_sess.post(path, {'oauth' => @token}.merge(params))
39
- #{:content_type => 'application/json'})
40
- puts 'response: ' + response.inspect
40
+ @logger.debug 'url=' + url
41
+ response = @http_sess.post(path + "?oauth=#{@token}", {'oauth' => @token}.merge(params).to_json, {"Content-Type" => 'application/json'})
42
+ check_response(response)
43
+ @logger.debug 'response: ' + response.inspect
41
44
  body = response.body
42
45
  res = JSON.parse(body)
43
- res
46
+ return res, response.status
44
47
  end
45
48
 
46
49
  def get(path, params={})
47
50
  url = "#{@base_url}#{path}"
48
- puts 'url=' + url
51
+ @logger.debug 'url=' + url
49
52
  response = @http_sess.request(:get, path,
50
53
  {},
51
54
  :query=>{'oauth'=>@token}.merge(params))
52
- body = response.body
53
- res = JSON.parse(body)
54
- res
55
+ res = check_response(response)
56
+
57
+ return res, response.status
55
58
  end
56
59
 
57
60
  def delete(path, params={})
58
61
  url = "#{@base_url}#{path}"
59
- puts 'url=' + url
62
+ @logger.debug 'url=' + url
60
63
  response = @http_sess.request(:delete, path,
61
64
  {},
62
65
  :query=>{'oauth'=>@token}.merge(params))
66
+ check_response(response)
63
67
  body = response.body
64
68
  res = JSON.parse(body)
65
- puts 'response: ' + res.inspect
69
+ @logger.debug 'response: ' + res.inspect
70
+ return res, response.status
71
+ end
72
+
73
+ def check_response(response)
74
+ status = response.status
75
+ body = response.body
76
+ res = JSON.parse(body)
77
+ if status < 400
78
+
79
+ else
80
+ raise IronMQ::Error.new(res["msg"], :status=>status)
81
+ end
66
82
  res
67
83
  end
68
84
 
69
85
  end
70
86
 
87
+ class Error < StandardError
88
+ def initialize(msg, options={})
89
+ super(msg)
90
+ @options = options
91
+ end
92
+
93
+ def status
94
+ @options[:status]
95
+ end
96
+ end
97
+
71
98
  class Messages
72
99
 
73
100
  attr_accessor :client
@@ -83,24 +110,59 @@ module IronMQ
83
110
  # options:
84
111
  # :queue_name => can specify an alternative queue name
85
112
  def get(options={})
86
- res = @client.get(path(options))
87
- res
113
+ begin
114
+ res, status = @client.get(path(options))
115
+ return Message.new(self, res)
116
+ rescue IronMQ::Error => ex
117
+ if ex.status == 404
118
+ return nil
119
+ end
120
+ end
121
+
122
+
88
123
  end
89
124
 
90
125
  # options:
91
126
  # :queue_name => can specify an alternative queue name
92
127
  def post(payload, options={})
93
- res = @client.post(path(options), :payload=>payload)
94
- res
128
+ res, status = @client.post(path(options), :body=>payload)
95
129
  end
96
130
 
97
131
  def delete(message_id, options={})
98
132
  path2 = "#{self.path(options)}/#{message_id}"
99
- res = @client.delete(path2)
133
+ res, status = @client.delete(path2)
100
134
  res
101
135
  end
102
136
 
103
137
  end
104
138
 
139
+ class Message
140
+
141
+ def initialize(messages, res)
142
+ @messages = messages
143
+ @data = res
144
+ end
145
+
146
+ def raw
147
+ @data
148
+ end
149
+
150
+ def [](key)
151
+ raw[key]
152
+ end
153
+
154
+ def id
155
+ raw["id"]
156
+ end
157
+
158
+ def body
159
+ raw["body"]
160
+ end
161
+
162
+ def delete
163
+ @messages.delete(self.id)
164
+ end
165
+ end
166
+
105
167
  end
106
168
 
data/test/ironmq_tests.rb CHANGED
@@ -15,6 +15,7 @@ class IronMQTests < Test::Unit::TestCase
15
15
  puts 'setup'
16
16
  @config = YAML::load_file(File.expand_path(File.join("~", "Dropbox", "configs", "ironmq_gem", "test", "config.yml")))
17
17
  @client = IronMQ::Client.new(@config['ironmq'])
18
+ @client.logger.level = Logger::DEBUG
18
19
  @client.queue_name = 'ironmq-gem-tests'
19
20
  end
20
21
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ironmq
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.2.2
5
+ version: 1.2.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Travis Reeder
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-12 00:00:00 Z
13
+ date: 2011-10-13 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: patron