wire_transfer 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ef8d0d72b82534d64e3ecba6d564e5155050670
4
- data.tar.gz: 50a021fcdc17ec6d70160598d0dc66d4966b5a8a
3
+ metadata.gz: 8046416c65495ffd67af77961a3f740533a27234
4
+ data.tar.gz: c81c34b96a40afe2fe8ee62ec1f5ad8aa2e69b39
5
5
  SHA512:
6
- metadata.gz: 5ba473c99aebbef74626c0bf48b362a5e78ab6635c41ff816cb5874a0072d22c7573fb7add7bba67decde33f0236a8105ed11c77b10c667ba6f71b13e703a502
7
- data.tar.gz: cad4d76c830a3cf9e5416c234cd3c4ef6d126ad3ffbc422488bffb0e91c4da26630d8532ced500561fdc568a523dc1dfe7f9237510c855f1a7d199c1351288f7
6
+ metadata.gz: fdff5475f902727ff58931a2dca779567fd98b4d198a8d73d07b3f98a2fd7e7c9aabe953193244479975811d998f61456227365249cbf80c701bd18d19d91c77
7
+ data.tar.gz: f417ac92da07ef8671309f480a4127feb75c993b4d331bd10e333af2a75e9cd3babaf9b2fd03dcd144bc45e4549f5e5176aaf8555de368ea5f51579a1742e9d2
@@ -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(reference)
15
- StructuredCommunication.compute(reference)
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
- def initialize number
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
- # Take a reference number and pad it with a certain amount of zeros
9
- # to make a total of 10 characters. Then it computes and appends 2 control characters.
10
- def self.compute(reference)
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
- protected
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
- def self.pad_reference_with_zeros(reference)
25
- reference.to_s.ljust(10, "0")
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 self.append_control_characters(padded_reference)
29
- padded_reference + compute_control_characters(padded_reference)
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 self.compute_control_characters(padded_reference)
37
- remainder = padded_reference.to_i % 97
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
@@ -1,3 +1,3 @@
1
1
  module WireTransfer
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wire_transfer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Romain Gérard