Sha256: 15674eaca7e2fa6d469996cf232eb9b15ae3394d90af3edc3cc924e97744ec82

Contents?: true

Size: 1.36 KB

Versions: 9

Compression:

Stored size: 1.36 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=nil)
      @is_tcp, @is_serial, @is_portless = 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 [Boolean] True if does not have real port
    def is_portless?
      @is_portless == true
    end

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

    private

    def parse(data)
      case
      # portless
      when data.nil?
        @port = "none"
        @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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
artoo-1.3.0 lib/artoo/port.rb
artoo-1.2.2 lib/artoo/port.rb
artoo-1.2.1 lib/artoo/port.rb
artoo-1.2.0 lib/artoo/port.rb
artoo-1.1.1 lib/artoo/port.rb
artoo-1.1.0 lib/artoo/port.rb
artoo-1.1.0.pre lib/artoo/port.rb
artoo-1.0.0 lib/artoo/port.rb
artoo-1.0.0.rc4 lib/artoo/port.rb