class Artoo::Port

The Artoo::Port class represents port and/or host to be used to connect tp a specific individual hardware device.

Attributes

host[R]
port[R]

Public Class Methods

new(data=nil) click to toggle source

Create new port @param [Object] data

# File lib/artoo/port.rb, line 9
def initialize(data=nil)
  @is_tcp, @is_serial, @is_portless = false
  parse(data)
end

Public Instance Methods

is_portless?() click to toggle source

@return [Boolean] True if does not have real port

# File lib/artoo/port.rb, line 25
def is_portless?
  @is_portless == true
end
is_serial?() click to toggle source

@return [Boolean] True if serial port

# File lib/artoo/port.rb, line 15
def is_serial?
  @is_serial == true
end
is_tcp?() click to toggle source

@return [Boolean] True if tcp port

# File lib/artoo/port.rb, line 20
def is_tcp?
  @is_tcp == true
end
to_s() click to toggle source

@return [String] port

# File lib/artoo/port.rb, line 30
def to_s
  if is_portless?
    "none"
  elsif is_serial?
    port
  else
    "#{host}:#{port}"
  end
end

Private Instance Methods

parse(data) click to toggle source
# File lib/artoo/port.rb, line 42
def parse(data)
  case
  # portless
  when data.nil?
    @port = nil
    @is_portless = true

  # is TCP host/port?
  when m = /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(\d{1,5})/.match(data)
    @port = m[2]
    @host = m[1]
    @is_tcp = true

  # is it a numeric port for localhost tcp?
  when /^[0-9]{1,5}$/.match(data)
    @port = data
    @host = "localhost"
    @is_tcp = true

  # must be a serial port
  else
    @port = data
    @host = nil
    @is_serial = true
  end
end