class RabbitRPC::Config

Public Class Methods

client_rpc_path() click to toggle source
# File lib/rabbit_rpc/config.rb, line 12
def client_rpc_path
  @@rpc_path ||= File.join 'config', 'rabbit_rpc.yml'
end
client_rpc_path=(path) click to toggle source
# File lib/rabbit_rpc/config.rb, line 16
def client_rpc_path=(path)
  @@rpc_path = path
end
initialize!() click to toggle source

Traverses through the RPC YAML file and creates RPC invocations under the RabbitRPC::Client namespace.

UserService:

Authorization: auth
Friend: list, delete

RabbitRPC::Client::UserService::Friends.list TODO: Abstract this logic into a seperate class

# File lib/rabbit_rpc/config.rb, line 29
def initialize!
  YAML.load_file(client_rpc_path).each do |service_name, class_definitions|
    unless class_definitions.is_a?(Hash) && class_definitions.keys.sort == %w[address methods]
      raise InvalidFormatError, "Error parsing the structure of the RPC YAML"
    end

    mdule        = Module.new
    service_name = "#{service_name}".classify

    ::RabbitRPC::Client.const_set service_name, mdule
    class_definitions.each do |param, value|
      if is_address?(param)
        @@service_address ||= {}
        @@service_address[service_name] = value
      elsif is_method_declaration?(param)
        populate_rpc(service_name, value)
      end
    end
  end
end

Private Class Methods

is_address?(key) click to toggle source
# File lib/rabbit_rpc/config.rb, line 52
def is_address?(key)
  key == 'address'
end
is_method_declaration?(key) click to toggle source
# File lib/rabbit_rpc/config.rb, line 56
def is_method_declaration?(key)
  key == 'methods'
end
populate_rpc(service_name, class_and_methods) click to toggle source
# File lib/rabbit_rpc/config.rb, line 60
def populate_rpc(service_name, class_and_methods)
  if @@service_address[service_name].nil?
    raise InvalidFormatError, "The address declaration in the YAML file appears to be missing for #{service_name}"
  end

  class_and_methods.each do |klass_name, methods|
    klass_name = klass_name.classify

    klass = Class.new do
      methods.gsub(/\s+/, "").split(',').each do |method_name|
        define_singleton_method(method_name) do |*args|

          RabbitRPC::SynchronousConnection.new(
            service_name,
            "#{service_name}.callback",
            @@service_address[service_name]).publish!(RabbitRPC::Message.new("#{klass_name}Service.#{method_name}", *args))
        end
      end
    end

    "RabbitRPC::Client::#{service_name}".constantize.const_set(klass_name, klass)
  end
end