rabbitmq 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,62 @@
1
+
2
+ module RabbitMQ
3
+ class ServerError < RuntimeError
4
+ class << self
5
+ attr_reader :lookup_table
6
+ end
7
+
8
+ def self.from(event)
9
+ properties = event.fetch(:properties)
10
+ kls = case event.fetch(:method)
11
+ when :channel_close
12
+ ChannelError.lookup_table[properties.fetch(:reply_code)]
13
+ when :connection_close
14
+ ConnectionError.lookup_table[properties.fetch(:reply_code)]
15
+ else
16
+ return
17
+ end
18
+ exc = kls.new(properties.fetch(:reply_text))
19
+ exc.event = event
20
+ exc
21
+ end
22
+
23
+ attr_writer :event
24
+
25
+ class ChannelError < ServerError
26
+ @lookup_table = {}
27
+ {
28
+ 311 => :content_too_large,
29
+ 313 => :no_consumers,
30
+ 403 => :access_refused,
31
+ 404 => :not_found,
32
+ 405 => :resource_locked,
33
+ 406 => :precondition_failed,
34
+ }.each do |code, name|
35
+ kls = Class.new(self) { define_method(:code) { code } }
36
+ @lookup_table[code] = kls
37
+ const_set Util.const_name(name), kls
38
+ end
39
+ end
40
+
41
+ class ConnectionError < ServerError
42
+ @lookup_table = {}
43
+ {
44
+ 320 => :connection_forced,
45
+ 402 => :invalid_path,
46
+ 501 => :frame_error,
47
+ 502 => :syntax_error,
48
+ 503 => :command_invalid,
49
+ 504 => :channel_error,
50
+ 505 => :unexpected_frame,
51
+ 506 => :resource_error,
52
+ 530 => :not_allowed,
53
+ 540 => :not_implemented,
54
+ 541 => :internal_error,
55
+ }.each do |code, name|
56
+ kls = Class.new(self) { define_method(:code) { code } }
57
+ @lookup_table[code] = kls
58
+ const_set Util.const_name(name), kls
59
+ end
60
+ end
61
+ end
62
+ end
data/lib/rabbitmq/util.rb CHANGED
@@ -1,8 +1,11 @@
1
1
 
2
- class RabbitMQ::FFI::Error < RuntimeError; end
3
-
4
- module RabbitMQ::Util
5
- class << self
2
+ module RabbitMQ
3
+ module Util; end
4
+ class << Util
5
+
6
+ def const_name(lowercase_name)
7
+ lowercase_name.to_s.gsub(/((?:\A\w)|(?:_\w))/) { |x| x[-1].upcase }
8
+ end
6
9
 
7
10
  def error_check action, status
8
11
  return if status == :ok
@@ -20,11 +23,6 @@ module RabbitMQ::Util
20
23
  ptr
21
24
  end
22
25
 
23
- def arg_ptr type, **kwargs
24
- type = ::FFI::TypeDefs[type] if type.is_a?(Symbol)
25
- mem_ptr(type.size, clear: false, **kwargs)
26
- end
27
-
28
26
  def strdup_ptr str, **kwargs
29
27
  str = str + "\x00"
30
28
  ptr = mem_ptr(str.bytesize, **kwargs)
@@ -32,5 +30,24 @@ module RabbitMQ::Util
32
30
  ptr
33
31
  end
34
32
 
33
+ def connection_info url=nil, **overrides
34
+ info = FFI::ConnectionInfo.new
35
+
36
+ if url
37
+ url_ptr = Util.strdup_ptr(url)
38
+ Util.error_check :"parsing connection URL",
39
+ FFI.amqp_parse_url(url_ptr, info)
40
+
41
+ # We must copy ConnectionInfo before the url_ptr is freed.
42
+ result = info.to_h
43
+ url_ptr.free
44
+ else
45
+ FFI.amqp_default_connection_info(info)
46
+ result = info.to_h
47
+ end
48
+
49
+ result.merge(overrides)
50
+ end
51
+
35
52
  end
36
53
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabbitmq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-01 00:00:00.000000000 Z
11
+ date: 2015-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -119,10 +119,12 @@ files:
119
119
  - README.md
120
120
  - ext/rabbitmq/Rakefile
121
121
  - lib/rabbitmq.rb
122
+ - lib/rabbitmq/channel.rb
122
123
  - lib/rabbitmq/connection.rb
123
124
  - lib/rabbitmq/ffi.rb
124
125
  - lib/rabbitmq/ffi/error.rb
125
126
  - lib/rabbitmq/ffi/ext.rb
127
+ - lib/rabbitmq/server_error.rb
126
128
  - lib/rabbitmq/util.rb
127
129
  homepage: https://github.com/jemc/rabbitmq/
128
130
  licenses: