module Matchd::Glue::AsyncEndpoint

Wrapper for allowing a more flexible way of defining interfaces for Asyc-*

Constants

DEFAULT_PORT

Default port used when the port is omitted in Hash or String notation.

PROTOCOLS

Supported DNS protocols

Public Class Methods

parse(args) click to toggle source

Examples:

# classic triplet array
AsyncIntrface.parse([:udp, "0.0.0.0", 53])
AsyncIntrface.parse([[:udp, "0.0.0.0", 53], [:tcp, "0.0.0.0", 53]])

# Hash notation
AsyncIntrface.parse({"protocol" => :udp, "ip" => "0.0.0.0", "port" => 53})
AsyncIntrface.parse({protocol: :udp, ip: "0.0.0.0", port: 53})
AsyncIntrface.parse([{protocol: :udp, ip: "0.0.0.0", port: 53}, {protocol: :tcp, ip: "0.0.0.0", port: 53}])

# URI strings
AsyncIntrface.parse("udp://0.0.0.0:53")
AsyncIntrface.parse(["udp://0.0.0.0:53", "tcp://0.0.0.0:53"])
# File lib/matchd/glue/async_endpoint.rb, line 19
def parse(args)
  val =
    case args
    when Array
      if triplet = parse_array(args)
        [triplet]
      else
        args.flat_map { |e| parse(e) }
      end
    when Hash
      [parse_hash(args)]
    when String
      [parse_uri(args)]
    end

  return nil unless val

  val.compact!

  val.empty? ? nil : val
end

Private Class Methods

parse_array(args) click to toggle source

@private parses the classic triplet array notation `[:udp, “0.0.0.0”, 53]` and returns just the thing if all data is present. To ensure proper triplet detection, all values are required. No port default.

# File lib/matchd/glue/async_endpoint.rb, line 54
def parse_array(args)
  protocol, ip, port = args

  return nil unless PROTOCOLS.include?(protocol.to_s) && ip && port

  [protocol.to_sym, ip, port]
end
parse_hash(args) click to toggle source

@private parses a single Hash with named components in string or symbol keys `{“protocol” => :udp, “ip” => “0.0.0.0”, “port” => 53}` or `{protocol: :udp, ip: “0.0.0.0”, port: 53}` and returns it's triplet notation if all parts are present. there's no additional array wrapping.

# File lib/matchd/glue/async_endpoint.rb, line 68
def parse_hash(args)
  protocol = args["protocol"] || args[:protocol]
  ip       = args["ip"]       || args[:ip]
  port     = args["port"]     || args[:port] || DEFAULT_PORT

  return nil unless PROTOCOLS.include?(protocol.to_s) && ip

  [protocol.to_sym, ip, port]
end
parse_uri(args) click to toggle source

@private parses a URI string `“udp://0.0.0.0:53”` or `“tcp://0.0.0.0:53”`

# File lib/matchd/glue/async_endpoint.rb, line 81
def parse_uri(args)
  uri = URI.parse(args)
  protocol = uri.scheme
  ip       = uri.host
  port     = uri.port || DEFAULT_PORT

  return nil unless PROTOCOLS.include?(protocol.to_s) && ip

  [protocol.to_sym, ip, port]
end