module Pione::Notification::Address

Nofitication::Address provides utility methods for notification addresses.

Constants

MULTICAST_ADDRESS

Public Class Methods

default_receiver_address() click to toggle source

Return default receiver address.

# File lib/pione/notification/address.rb, line 35
def default_receiver_address
  host_and_port_to_uri(
    Global.default_notification_receiver_host,
    Global.default_notification_receiver_port
  )
end
default_target_address() click to toggle source

Return default target address.

# File lib/pione/notification/address.rb, line 27
def default_target_address
  host_and_port_to_uri(
    Global.default_notification_target_host,
    Global.default_notification_target_port
  )
end
receiver_address_to_uri(address) click to toggle source

Convert the receiver address to URI.

# File lib/pione/notification/address.rb, line 18
def receiver_address_to_uri(address)
  address_to_uri(
    address,
    Global.default_notification_receiver_host,
    Global.default_notification_receiver_port
  )
end
target_address_to_uri(address) click to toggle source

Convert the target address to URI.

# File lib/pione/notification/address.rb, line 9
def target_address_to_uri(address)
  address_to_uri(
    address,
    Global.default_notification_target_host,
    Global.default_notification_target_port
  )
end

Private Class Methods

address_to_uri(address, default_host, default_port) click to toggle source

Convert the address to URI.

# File lib/pione/notification/address.rb, line 45
def address_to_uri(address, default_host, default_port)
  uri = URI.parse(address)
  unless ["pnb", "pnm", "pnu"].include?(uri.scheme)
    raise URI::InvalidURIError
  end
  uri.host = default_host if uri.host == "."
  uri.port = default_port if uri.port.nil?
  return uri
rescue URI::InvalidURIError
  if port_only?(address)
    return port_to_uri(default_host, address)
  end

  if host_only?(address)
    return host_to_uri(default_port, address)
  end

  host_and_port_to_uri(*address.split(":"))
end
host_and_port_to_uri(host, port) click to toggle source

Convert the host and port to URI. The scheme assumes “pnb” excluding multicast addresses.

# File lib/pione/notification/address.rb, line 97
def host_and_port_to_uri(host, port)
  scheme = multicast?(host) ? "pnm" : "pnb"
  URI.parse("%s://%s:%s" % [scheme, host, port])
end
host_only?(address) click to toggle source

Return true if the address conatains host only.

# File lib/pione/notification/address.rb, line 71
def host_only?(address)
  not(address.include?(":"))
end
host_to_uri(default_port, host) click to toggle source

Convert the host to URI. The scheme assumes “pnb” excluding multicast addresses.

# File lib/pione/notification/address.rb, line 83
def host_to_uri(default_port, host)
  scheme = multicast?(host) ? "pnm" : "pnb"
  URI.parse("%s://%s:%s" % [scheme, host, default_port])
end
multicast?(host) click to toggle source

Return true if the host is a multicast address.

# File lib/pione/notification/address.rb, line 76
def multicast?(host)
  _host = IPSocket.getaddress(host)
  MULTICAST_ADDRESS.include?(_host)
end
port_only?(address) click to toggle source

Return true if the address contains port only.

# File lib/pione/notification/address.rb, line 66
def port_only?(address)
  address[0] == ":"
end
port_to_uri(default_host, address) click to toggle source

Convert the port to URI. The scheme assumes “pnb” excluding multicast addresses.

# File lib/pione/notification/address.rb, line 90
def port_to_uri(default_host, address)
  scheme = multicast?(default_host) ? "pnm" : "pnb"
  URI.parse("%s://%s:%s" % [scheme, default_host, address.sub(":", "")])
end