Module: MessageQueue

Extended by:
MessageQueue
Included in:
MessageQueue
Defined in:
lib/message_queue.rb,
lib/message_queue/adapter.rb,
lib/message_queue/version.rb,
lib/message_queue/serializer.rb,
lib/message_queue/adapters/bunny.rb,
lib/message_queue/serializers/plain.rb,
lib/message_queue/serializers/message_pack.rb

Defined Under Namespace

Modules: Adapters, Serializers

Constant Summary collapse

ADAPTERS =
[:bunny]
SERIALIZERS =
[:plain, :message_pack]
VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#klass_name_for(name) ⇒ Object

Internal: Return the class name for name

Returns the class name for specified string



78
79
80
# File 'lib/message_queue.rb', line 78

def klass_name_for(name)
  name.to_s.split("_").map(&:capitalize) * ""
end

#load_adapter(name) ⇒ Object

Internal: Load an adapter by name

Returns the adapter or nil if it can’t find it



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/message_queue.rb', line 48

def load_adapter(name)
  ADAPTERS.each do |a|
    if a.to_s == name.to_s
      require_relative "message_queue/adapters/#{name}"
      klass_name = klass_name_for(name)
      return MessageQueue::Adapters.const_get(klass_name)
    end
  end

  nil
end

#load_serializer(name) ⇒ Object

Internal: Load a serializer by name

Returns the serializer or nil if it can’t find it



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/message_queue.rb', line 63

def load_serializer(name)
  SERIALIZERS.each do |s|
    if s.to_s == name.to_s
      require_relative "message_queue/serializers/#{name}"
      klass_name = klass_name_for(name)
      return MessageQueue::Serializers.const_get(klass_name)
    end
  end

  nil
end

#new_connection(options = {}) ⇒ Object

Public: Initialize a connection to a message queue.

options - The Hash options used to initialize a connection

:adapter - The Symbol adapter, currently only :bunny is supported.
           Detailed options see individual adapter implementation.
:serializer - The Symbol serializer for serialization.

Returns the connection for the specified message queue. Raises a RuntimeError if an adapter can’t be found.



20
21
22
23
24
25
26
27
28
# File 'lib/message_queue.rb', line 20

def new_connection(options = {})
  adapter = load_adapter(options[:adapter])
  raise "Missing adapter #{options[:adapter]}" unless adapter

  serializer = load_serializer(options[:serializer])
  raise "Missing serializer #{options[:serializer]}" unless serializer

  adapter.new_connection(serializer, options)
end

#with_connection(options = {}, &block) ⇒ Object

Public: Initialize a connection to a message queue, connect and execute code in a block.

options - The Hash options used to initialize a connection

:adapter - The Symbol adapter, currently only :bunny is supported.
           Detailed options see individual adapter implementation.
:serializer - The Symbol serializer for serialization.

block - The code to execute. The connection object with be passed in.

Returns nothing Raises a RuntimeError if an adapter can’t be found.



40
41
42
43
# File 'lib/message_queue.rb', line 40

def with_connection(options = {}, &block)
  connection = new_connection(options)
  connection.with_connection(&block)
end