Sha256: 04fa099f7f74a4f3de5f5df3d14e3dc9e77207bf4ce6675d6d97fb1cdcda288e

Contents?: true

Size: 963 Bytes

Versions: 7

Compression:

Stored size: 963 Bytes

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

    def initialize(data)
      @is_tcp, @is_serial = false
      parse(data)
    end

    def is_serial?
      @is_serial == true
    end

    def is_tcp?
      @is_tcp == true
    end

    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

7 entries across 7 versions & 1 rubygems

Version Path
artoo-0.4.0 lib/artoo/port.rb
artoo-0.3.0 lib/artoo/port.rb
artoo-0.2.0 lib/artoo/port.rb
artoo-0.1.3 lib/artoo/port.rb
artoo-0.1.2 lib/artoo/port.rb
artoo-0.1.1 lib/artoo/port.rb
artoo-0.1.0 lib/artoo/port.rb