class MovingsignApi::SenderReceiverAddress

Represents a sender or receiver identifier for a {Command}

A address can be represented as an integer between 0 and 255, or per the spec as a two character hex string: '00' - 'FF'.

Valid inputs include:

Some notes:

Attributes

identifier[RW]

@return [Integer] the address repsented as an integer

Public Class Methods

new(identifier) click to toggle source

@param identifier [String] Identifier hex string, or hex string with wildcard

# File lib/movingsign_api/commands/internal/sender_receiver_address.rb, line 25
def initialize(identifier)
  @identifier = identifier
end
parse(input) click to toggle source

Parses the specified input value returning an appropriate {SenderReceiver} instance or raises.

@raise [InvalidInputError] on invalid input @return [SenderReceiverAddress]

# File lib/movingsign_api/commands/internal/sender_receiver_address.rb, line 33
def self.parse(input)
  raise MovingsignApi::InvalidInputError, "nil not allowed" if input.nil?

  value = nil
  if input.kind_of? Fixnum
    if input.between?(0, 255)
      value = ('%02x' % input).upcase
    else
      raise MovingsignApi::InvalidInputError, "Integer #{input} is out of the valid range."
    end
  elsif input.kind_of? Symbol
    if input == :broadcast
      value = '00'
    elsif input == :pc
      value = 'FF'
    else
      raise MovingsignApi::InvalidInputError, "Symbol :#{input} isn't supported"
    end
  elsif input.kind_of? String
    if (value = input.upcase.match /\A[0-9,A-F\?]{2}\z/)
      value = value[0]
    else
      raise MovingsignApi::InvalidInputError, "Parsing string '#{input}' isn't supported"
    end
  else
    raise MovingsignApi::InvalidInputError, "Parsing '#{input.class}' isn't supported"
  end

  self.new(value)
end

Public Instance Methods

broadcast?() click to toggle source

Returns true if this represents the special 'broadcast' address

# File lib/movingsign_api/commands/internal/sender_receiver_address.rb, line 65
def broadcast?
  self.identifier == '00'
end
pc?() click to toggle source

Returns true if this represents the special 'pc' address

# File lib/movingsign_api/commands/internal/sender_receiver_address.rb, line 70
def pc?
  self.identifier = 'FF'
end
to_bytes() click to toggle source
# File lib/movingsign_api/commands/internal/sender_receiver_address.rb, line 78
def to_bytes
  string_to_ascii_bytes self.identifier
end
to_s() click to toggle source
# File lib/movingsign_api/commands/internal/sender_receiver_address.rb, line 74
def to_s
  self.identifier
end