Class: WireTransfer::StructuredCommunication
- Inherits:
-
Object
- Object
- WireTransfer::StructuredCommunication
- Defined in:
- lib/wire_transfer/structured_communication.rb
Constant Summary collapse
- ALLOWED_NUMBERS =
0..9999999999
Class Method Summary collapse
Instance Method Summary collapse
- #checksummed_padded_number ⇒ Object
-
#control_characters ⇒ Object
Take the remainder of the euclidian division from the padded reference by 97 (modulo 97) and pad it with zeros if necessary unless this remainder is equal to 0, in that case, the control characters are replaced by 97.
-
#initialize(number) ⇒ StructuredCommunication
constructor
A new instance of StructuredCommunication.
- #padded_number ⇒ Object
- #to_i ⇒ Object
-
#to_s ⇒ Object
Take a communication number and format it as it appears on a wire transfer.
Constructor Details
#initialize(number) ⇒ StructuredCommunication
Returns a new instance of StructuredCommunication.
5 6 7 8 |
# File 'lib/wire_transfer/structured_communication.rb', line 5 def initialize number @number = number raise InvalidNumberError unless ALLOWED_NUMBERS.include?(@number) end |
Class Method Details
.from_string(string) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/wire_transfer/structured_communication.rb', line 10 def self.from_string(string) string = string.gsub(/[^\d]/, '') raise InvalidNumberError unless string.size == 12 number = string[0..9] control_characters = string[10..11] structured_communication = new(number.to_i) raise InvalidNumberError unless structured_communication.control_characters == control_characters structured_communication end |
Instance Method Details
#checksummed_padded_number ⇒ Object
39 40 41 |
# File 'lib/wire_transfer/structured_communication.rb', line 39 def checksummed_padded_number padded_number + control_characters end |
#control_characters ⇒ Object
Take the remainder of the euclidian division from the padded reference by 97 (modulo 97) and pad it with zeros if necessary unless this remainder is equal to 0, in that case, the control characters are replaced by 97.
29 30 31 32 33 34 35 36 37 |
# File 'lib/wire_transfer/structured_communication.rb', line 29 def control_characters remainder = padded_number.to_i % 97 if remainder > 0 remainder.to_s.rjust(2, "0") else "97" end end |
#padded_number ⇒ Object
21 22 23 |
# File 'lib/wire_transfer/structured_communication.rb', line 21 def padded_number @number.to_s.rjust(10, "0") end |
#to_i ⇒ Object
50 51 52 |
# File 'lib/wire_transfer/structured_communication.rb', line 50 def to_i @number end |
#to_s ⇒ Object
Take a communication number and format it as it appears on a wire transfer.
44 45 46 47 48 |
# File 'lib/wire_transfer/structured_communication.rb', line 44 def to_s communication_number = checksummed_padded_number.insert 3, "/" communication_number = communication_number.insert 8, "/" "+++#{communication_number}+++" end |