Class: ActionSubscriber::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/action_subscriber/uri.rb

Constant Summary collapse

AMQP_PORTS =
{"amqp" => 5672, "amqps" => 5671}.freeze

Class Method Summary collapse

Class Method Details

.parse_amqp_url(connection_string) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/action_subscriber/uri.rb', line 9

def self.parse_amqp_url(connection_string)
  uri = ::URI.parse(connection_string)
  raise ArgumentError.new("Connection URI must use amqp or amqps schema (example: amqp://bus.megacorp.internal:5766), learn more at http://bit.ly/ks8MXK") unless %w{amqp amqps}.include?(uri.scheme)

  opts = {}

  opts[:username]   = ::CGI::unescape(uri.user) if uri.user
  opts[:password]   = ::CGI::unescape(uri.password) if uri.password
  opts[:host]   = uri.host if uri.host
  opts[:port]   = uri.port || AMQP_PORTS[uri.scheme]

  if uri.path =~ %r{^/(.*)}
    raise ArgumentError.new("#{uri} has multiple-segment path; please percent-encode any slashes in the vhost name (e.g. /production => %2Fproduction). Learn more at http://bit.ly/amqp-gem-and-connection-uris") if $1.index('/')
    opts[:virtual_host] = ::CGI::unescape($1)
  end

  opts
end