Sha256: 8537356f7f6fca71e56e99d831e212eeb89505fcf55a42b5da445ceb8181969c

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

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

    # Create new port
    # @param [Object] data
    def initialize(data)
      @is_tcp, @is_serial = false
      parse(data)
    end

    # @return [Boolean] True if serial port
    def is_serial?
      @is_serial == true
    end

    # @return [Boolean] True if tcp port
    def is_tcp?
      @is_tcp == true
    end

    # @return [String] port
    def to_s
      if is_serial?
        port
      else
        "#{host}:#{port}"
      end
    end

    private

    def parse(data)
      # is TCP host/port?
      if 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?
      elsif /^[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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
artoo-1.0.0.rc3 lib/artoo/port.rb
artoo-1.0.0.rc2 lib/artoo/port.rb
artoo-1.0.0.rc1 lib/artoo/port.rb
artoo-1.0.0.pre lib/artoo/port.rb
artoo-0.5.0 lib/artoo/port.rb
artoo-0.4.1 lib/artoo/port.rb