Method: OmfCommon::Comm.init

Defined in:
lib/omf_common/comm.rb

.init(opts) ⇒ Object

opts:

:type - pre installed comms provider
:provider - custom provider (opts)
  :require - gem to load first (opts)
  :constructor - Class implementing provider


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/omf_common/comm.rb', line 44

def self.init(opts)
  unless @@instance
    unless provider = opts[:provider]
      unless type = opts[:type]
        if url = opts[:url]
          type = url.split(':')[0].to_sym
        end
      end
      provider = @@providers[type]
    end
    unless provider
      raise ArgumentError, "Missing Comm provider declaration. Either define 'type', 'provider', or 'url'"
    end

    require provider[:require] if provider[:require]

    if class_name = provider[:constructor]
      provider_class = class_name.split('::').inject(Object) {|c,n| c.const_get(n) }
      inst = provider_class.new(opts)
    else
      raise ArgumentError, "Missing communicator creation info - :constructor"
    end
    @@instance = inst
    mopts = provider[:message_provider]
    mopts[:authenticate] = opts[:auth]
    Message.init(mopts)

    if aopts = opts[:auth]
      require 'omf_common/auth'
      OmfCommon::Auth.init(aopts)
    end

    inst.init(opts)
  end
end