wire_transfer 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/wire_transfer.rb +6 -2
- data/lib/wire_transfer/structured_communication.rb +31 -23
- data/lib/wire_transfer/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8046416c65495ffd67af77961a3f740533a27234
|
4
|
+
data.tar.gz: c81c34b96a40afe2fe8ee62ec1f5ad8aa2e69b39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdff5475f902727ff58931a2dca779567fd98b4d198a8d73d07b3f98a2fd7e7c9aabe953193244479975811d998f61456227365249cbf80c701bd18d19d91c77
|
7
|
+
data.tar.gz: f417ac92da07ef8671309f480a4127feb75c993b4d331bd10e333af2a75e9cd3babaf9b2fd03dcd144bc45e4549f5e5176aaf8555de368ea5f51579a1742e9d2
|
data/lib/wire_transfer.rb
CHANGED
@@ -11,8 +11,12 @@ require "wire_transfer/helper"
|
|
11
11
|
require "wire_transfer/invalid_number_error"
|
12
12
|
|
13
13
|
module WireTransfer
|
14
|
-
def self.generate_structured_communication(
|
15
|
-
StructuredCommunication.
|
14
|
+
def self.generate_structured_communication(number)
|
15
|
+
StructuredCommunication.new(number)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.parse_structured_communication(string)
|
19
|
+
StructuredCommunication.from_string(string)
|
16
20
|
end
|
17
21
|
end
|
18
22
|
|
@@ -1,40 +1,33 @@
|
|
1
1
|
module WireTransfer
|
2
2
|
class StructuredCommunication
|
3
|
-
|
4
|
-
@number = number.to_s
|
5
|
-
raise InvalidNumberError unless @number.match(/\A\d{12}\z/)
|
6
|
-
end
|
3
|
+
ALLOWED_NUMBERS = 0..9999999999
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
padded_reference = pad_reference_with_zeros(reference)
|
12
|
-
new(append_control_characters(padded_reference))
|
13
|
-
end
|
14
|
-
|
15
|
-
# Take a communication number and format it as it appears on a wire transfer.
|
16
|
-
def to_s
|
17
|
-
communication_number = @number.insert 3, "/"
|
18
|
-
communication_number = communication_number.insert 8, "/"
|
19
|
-
"+++#{communication_number}+++"
|
5
|
+
def initialize number
|
6
|
+
@number = number
|
7
|
+
raise InvalidNumberError unless ALLOWED_NUMBERS.include?(@number)
|
20
8
|
end
|
21
9
|
|
22
|
-
|
10
|
+
def self.from_string(string)
|
11
|
+
string = string.gsub(/[^\d]/, '')
|
12
|
+
raise InvalidNumberError unless string.size == 12
|
13
|
+
number = string[0..9]
|
14
|
+
control_characters = string[10..11]
|
23
15
|
|
24
|
-
|
25
|
-
|
16
|
+
structured_communication = new(number.to_i)
|
17
|
+
raise InvalidNumberError unless structured_communication.control_characters == control_characters
|
18
|
+
structured_communication
|
26
19
|
end
|
27
20
|
|
28
|
-
def
|
29
|
-
|
21
|
+
def padded_number
|
22
|
+
@number.to_s.rjust(10, "0")
|
30
23
|
end
|
31
24
|
|
32
25
|
# Take the remainder of the euclidian division from the padded
|
33
26
|
# reference by 97 (modulo 97) and pad it with zeros if necessary
|
34
27
|
# unless this remainder is equal to 0, in that case,
|
35
28
|
# the control characters are replaced by 97.
|
36
|
-
def
|
37
|
-
remainder =
|
29
|
+
def control_characters
|
30
|
+
remainder = padded_number.to_i % 97
|
38
31
|
|
39
32
|
if remainder > 0
|
40
33
|
remainder.to_s.rjust(2, "0")
|
@@ -42,5 +35,20 @@ module WireTransfer
|
|
42
35
|
"97"
|
43
36
|
end
|
44
37
|
end
|
38
|
+
|
39
|
+
def checksummed_padded_number
|
40
|
+
padded_number + control_characters
|
41
|
+
end
|
42
|
+
|
43
|
+
# Take a communication number and format it as it appears on a wire transfer.
|
44
|
+
def to_s
|
45
|
+
communication_number = checksummed_padded_number.insert 3, "/"
|
46
|
+
communication_number = communication_number.insert 8, "/"
|
47
|
+
"+++#{communication_number}+++"
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_i
|
51
|
+
@number
|
52
|
+
end
|
45
53
|
end
|
46
54
|
end
|