Class: CFRuntime::RabbitmqParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cf-runtime/parser/rabbitmq_parser.rb

Class Method Summary collapse

Class Method Details

.parse(svc) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cf-runtime/parser/rabbitmq_parser.rb', line 3

def self.parse(svc)
  serviceopts = {}
  cred = svc["credentials"]
  vhost = cred["vhost"] || "/" #The RabbitMQ default vhost
  if cred["url"]
    # The new "srs" credentials format
    uri=URI.parse(cred["url"])
    user=URI.unescape(uri.user) if uri.user
    passwd=URI.unescape(uri.password) if uri.password
    host=uri.host
    port=uri.port
    if uri.path =~ %r{^/(.*)}
      raise ArgumentError.new("multiple segments in path of amqp URI: #{uri}") if $1.index('/')
      vhost = URI.unescape($1)
    end
    serviceopts[:url] = cred["url"]
  else
    # The "old" credentials format
    user,passwd,host,port = %w(user pass hostname port).map {|key|
      cred[key]}
    serviceopts[:url]  = "amqp://#{user}:#{passwd}@#{host}:#{port}"
    serviceopts[:url] = "#{serviceopts[:url]}/#{vhost}" if vhost != "/"
  end
  serviceopts[:username] = user
  serviceopts[:password] = passwd
  serviceopts[:host] = host
  serviceopts[:port] = port
  serviceopts[:vhost] = vhost
  serviceopts
end