Module: MessageChannel::Base

Included in:
MessageChannel
Defined in:
lib/message_channel/base.rb

Instance Method Summary collapse

Instance Method Details

#new(uri = nil, type: nil, host: nil, port: nil, db: nil) ⇒ Object

uri:

"observer"
"druby://127.0.0.1:8787"
"mqtt://127.0.0.1:1883"
"redis://127.0.0.1:6379/0"


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/message_channel/base.rb', line 10

def new( uri = nil, type: nil, host: nil, port: nil, db: nil )
  if  uri
    uris  =  URI.parse( uri )
    if  uris.scheme.nil?  &&  uris.host.nil?  &&  uris.port.nil?  &&  uris.path
      type  =  uris.path
      params  =  []
    else
      type  =  uris.scheme  ||  type
      host  =  uris.host  ||  host
      port  =  uris.port  ||  port
      params  =  uris.path.gsub(/^\//, "").split('/')
      query  =  Hash[ URI::decode_www_form(uris.query) ]  rescue  {}
    end
  else
    parans  =  []
  end

  options  =  { host: host, port: port }

  case  type
  when  "observer", NilClass
    MessageChannel::Observer.new( **options )
  when  "druby"
    MessageChannel::Druby.new( **options )
  when  "mqtt"
    MessageChannel::Mqtt.new( **options )
  when  "redis"
    options[:db]  =  params.shift  ||  db
    MessageChannel::Redis.new( **options )
  end
end