Class: MovingsignApi::SenderReceiverAddress
- Inherits:
-
Object
- Object
- MovingsignApi::SenderReceiverAddress
- Includes:
- Utilities
- Defined in:
- lib/movingsign_api/commands/internal/sender_receiver_address.rb
Overview
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:
-
Integer -
0
-255
-
Symbol -
:broadcast
or:pc
-
String - ‘00’ - ‘FF’
Some notes:
-
Broadcast Address: (
:broadcast
,0
, or ‘00’) is a broadcast identifier and shouldn’t be used as a sender -
PC Address: (
:pc
,255
, or ‘FF”) is reserved to represent the “PC address” and shouldn’t be used as a recipient
Instance Attribute Summary collapse
-
#identifier ⇒ Integer
The address repsented as an integer.
Class Method Summary collapse
-
.parse(input) ⇒ SenderReceiverAddress
Parses the specified input value returning an appropriate SenderReceiver instance or raises.
Instance Method Summary collapse
-
#broadcast? ⇒ Boolean
Returns true if this represents the special ‘broadcast’ address.
-
#initialize(identifier) ⇒ SenderReceiverAddress
constructor
A new instance of SenderReceiverAddress.
-
#pc? ⇒ Boolean
Returns true if this represents the special ‘pc’ address.
- #to_bytes ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(identifier) ⇒ SenderReceiverAddress
Returns a new instance of SenderReceiverAddress.
25 26 27 |
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 25 def initialize(identifier) @identifier = identifier end |
Instance Attribute Details
#identifier ⇒ Integer
Returns the address repsented as an integer.
22 23 24 |
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 22 def identifier @identifier end |
Class Method Details
.parse(input) ⇒ SenderReceiverAddress
Parses the specified input value returning an appropriate SenderReceiver instance or raises.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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 |
Instance Method Details
#broadcast? ⇒ Boolean
Returns true if this represents the special ‘broadcast’ address
65 66 67 |
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 65 def broadcast? self.identifier == '00' end |
#pc? ⇒ Boolean
Returns true if this represents the special ‘pc’ address
70 71 72 |
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 70 def pc? self.identifier = 'FF' end |
#to_bytes ⇒ Object
78 79 80 |
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 78 def to_bytes string_to_ascii_bytes self.identifier end |
#to_s ⇒ Object
74 75 76 |
# File 'lib/movingsign_api/commands/internal/sender_receiver_address.rb', line 74 def to_s self.identifier end |