edifact_rails 2.0.0 → 2.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecc255d266d797eed361491b7643a6ca1a4e03205d214da5c447d4966d0cfb10
4
- data.tar.gz: e7aa5f7730944a6b1f6b5adf9809b5d097ae2daf0bef00cd4f84c7433515b122
3
+ metadata.gz: 617b14ce31edcecdb4d3c4a599f79612716680513a740a425f2bcd0cdb8f1d65
4
+ data.tar.gz: aa8d78ea8759f43a1465e2036c55ea704c1dce3f4c67c30f943324d32525787c
5
5
  SHA512:
6
- metadata.gz: f0976e015521c0145fd8f4fcca85be2acd5206cf88a31a1bae6cc2d1b88068315930fadf37e1c189c04adab02720fe80b743b14e0818de33d9bbdba3b2d06d00
7
- data.tar.gz: 8f234c0556584570683113037fd3289cad26615fe03e933de3c3618957d0b6142c0a787cefcafa4f913b89564daf7c87bec56d24b61706320c54ea6f3f0800b6
6
+ metadata.gz: a7531b90d3f9d84223e3176fd6ebd012a26076d3f029e30c344c2ab2a26964c069e8a387f5c73c02445cccbc140e3bdba81bbb832ac08c07af7625bc56a4f686
7
+ data.tar.gz: ccd019d6bd9beac78be1c07563357a4b6f616bdb538e9a66b5b816a2a6ceadcd7abb05749940a8bf6277cc2d9507da0a003e6f8954fa0d90ef6588ade5327f71
data/CHANGELOG.md CHANGED
@@ -1,33 +1,45 @@
1
1
  # Changelog
2
2
 
3
- ## 1.0.0 (26/04/2023)
3
+ ## 2.1.0 (18/06/2024)
4
4
 
5
- * Initial release
5
+ * Added EDIFACT serialization. Contributed by [sallesma](https://github.com/sallesma)
6
6
 
7
- ## 1.1.0 (27/04/2023)
7
+ ## 2.0.0 (18/06/2024)
8
8
 
9
- * Added support for TRADACOMS input
9
+ * Added support for ANSIX12 format.
10
10
 
11
- ## 1.1.1 (4/05/2023)
11
+ ##### Breaking changes:
12
+ * `#una_special_characters` renamed to `#special_characters` (since it can now accept input of any supported format)
13
+ * New `UnrecognizedFormat` Error will now be thrown if the format of the input can not be detected.
14
+ * In essence, input must begin now with `UNA` or `UNB` (EDIFACT), `STX` (TRADACOMS), or `ISA` (ANSIX12)
12
15
 
13
- * Fixed crash caused by running the gem in a production environment.
16
+ ## 1.2.1 (4/06/2024)
17
+
18
+ * `#una_special_characters` method now also returns decimal notation character, default `.`.
19
+ * `#una_special_characters` method can now take no arguments, and will return the default special characters if so.
14
20
 
15
21
  ## 1.2.0 (31/05/2024)
16
22
 
17
23
  * Added support for UNA segments. Special characters different from the defaults can now be used.
18
24
  * Added `#una_special_characters` method that returns just the special characters.
19
25
 
20
- ## 1.2.1 (4/06/2024)
26
+ ## 1.1.1 (4/05/2023)
27
+
28
+ * Fixed crash caused by running the gem in a production environment.
29
+
30
+ ## 1.1.0 (27/04/2023)
31
+
32
+ * Added support for TRADACOMS input
33
+
34
+ ## 1.0.0 (26/04/2023)
35
+
36
+ * Initial release
37
+
38
+
39
+
40
+
21
41
 
22
- * `#una_special_characters` method now also returns decimal notation character, default `.`.
23
- * `#una_special_characters` method can now take no arguments, and will return the default special characters if so.
24
42
 
25
- ## 2.0.0 (18/06/2024)
26
43
 
27
- * Added support for ANSIX12 format.
28
44
 
29
- ##### Breaking changes:
30
- * `#una_special_characters` renamed to `#special_characters` (since it can now accept input of any supported format)
31
- * New `UnrecognizedFormat` Error will now be thrown if the format of the input can not be detected.
32
- * In essence, input must begin now with `UNA` or `UNB` (EDIFACT), `STX` (TRADACOMS), or `ISA` (ANSIX12)
33
45
 
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  This gem parses EDIFACT, TRADACOMS, or ANSIX12 input, and converts it into a ruby array structure for whatever further processing or validation you desire.
4
4
 
5
- It does not handle validation itself.
5
+ This gem can also take a ruby array input, and serialize it into EDIFACT.
6
6
 
7
7
  This gem is heavily inspired by [edifact_parser](https://github.com/pvdvreede/edifact_parser)
8
8
 
@@ -20,7 +20,7 @@ This gem has been tested on the following ruby versions:
20
20
  In your `Gemfile`:
21
21
 
22
22
  ```ruby
23
- gem 'edifact_rails', '~> 2.0.0'
23
+ gem 'edifact_rails', '~> 2.1.0'
24
24
  ```
25
25
 
26
26
  Otherwise:
@@ -36,6 +36,7 @@ If you don't have the gem in your `Gemfile`, you will need to:
36
36
  ```ruby
37
37
  require 'edifact_rails'
38
38
  ```
39
+ ### Parsing
39
40
 
40
41
  You can parse a string input with `#parse`, or a file with `#parse_file`
41
42
 
@@ -47,6 +48,24 @@ ruby_array = EdifactRails.parse("UNB+UNOA:3+TESTPLACE:1+DEP1:1+20051107:1159+600
47
48
  ruby_array = EdifactRails.parse_file("your/file/path")
48
49
  ```
49
50
 
51
+ ### Serialization
52
+
53
+ You can convert a ruby input into EDIFACT with `#serialize`. Use the `with_service` option to insert the UNA segment.
54
+
55
+ ```ruby
56
+ edifact_output = Edifact.serialize(
57
+ [
58
+ ["LIN", [1], [1], ["0764569104", "IB"]],
59
+ ["QTY", [1, 25]]
60
+ ],
61
+ with_service: true
62
+ )
63
+ # edifact_output =>
64
+ "UNA:+.? 'LIN+1+1+0764569104:IB'QTY+1:25'"
65
+ ```
66
+
67
+ ### Special Characters
68
+
50
69
  You can return the special characters of your input with `#special_characters`.
51
70
  ```ruby
52
71
  special_characters = EdifactRails.special_characters(example_edifact_input)
@@ -60,7 +79,7 @@ special_characters = EdifactRails.special_characters(example_edifact_input)
60
79
  }
61
80
  ```
62
81
 
63
- ## Output
82
+ ## Parse Output
64
83
 
65
84
  ### EDIFACT
66
85
 
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EdifactRails
4
+ class Serializer
5
+ # with_service is used to force the service segment (UNA) at the beginning of the message
6
+ def serialize(segments, with_service:)
7
+ # Serialize and join the segments
8
+ output = segments.map { |segment| serialize_segment(segment) }
9
+ .join(EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:segment_seperator])
10
+
11
+ # Add the UNA segment
12
+ output.insert(0, "UNA:+.? '") unless segments.first.first == "UNA" || !with_service
13
+ output + EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:segment_seperator]
14
+ end
15
+
16
+ private
17
+
18
+ # Serialize the data elements and join them to serialize the segment
19
+ def serialize_segment(segment)
20
+ return if segment.empty?
21
+
22
+ # Get the tag
23
+ tag = segment.first
24
+ data_elements = segment[1..]
25
+
26
+ # Serialize the data elements
27
+ serialized_elements = data_elements.map { |element| serialize_data_element(element) }
28
+
29
+ # Join tag and data elements
30
+ serialized_elements.prepend(tag).join(EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:data_element_seperator])
31
+ end
32
+
33
+ def serialize_data_element(element)
34
+ element.map { |component| treat_component(component) }
35
+ .join(EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:component_data_element_seperator])
36
+ end
37
+
38
+ # Strip, remove escape characters, convert to nil where needed, convert to integer where needed
39
+ def treat_component(component)
40
+ return component unless component.is_a? String
41
+
42
+ # Prepare regex
43
+ all_special_characters = [
44
+ EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:segment_seperator],
45
+ EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:data_element_seperator],
46
+ EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:component_data_element_seperator],
47
+ EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:escape_character]
48
+ ].join
49
+
50
+ # If the component has escaped characters in it, prepend the escape character "+" -> "?+", "?" -> "??"
51
+ component.gsub(/([#{Regexp.quote(all_special_characters)}])/) do |match|
52
+ EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:escape_character] + match
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EdifactRails
4
- VERSION = "2.0.0"
4
+ VERSION = "2.1.0"
5
5
  end
data/lib/edifact_rails.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "edifact_rails/parser"
4
+ require "edifact_rails/serializer"
4
5
  require "edifact_rails/formats"
5
6
  require "edifact_rails/exceptions"
6
7
 
@@ -26,4 +27,9 @@ module EdifactRails
26
27
  parser = EdifactRails::Parser.new
27
28
  parser.special_characters(string)
28
29
  end
30
+
31
+ def self.serialize(array, with_service: true)
32
+ serializer = EdifactRails::Serializer.new
33
+ serializer.serialize array, with_service: with_service
34
+ end
29
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: edifact_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Blackwood
@@ -127,6 +127,7 @@ files:
127
127
  - lib/edifact_rails/exceptions.rb
128
128
  - lib/edifact_rails/formats.rb
129
129
  - lib/edifact_rails/parser.rb
130
+ - lib/edifact_rails/serializer.rb
130
131
  - lib/edifact_rails/version.rb
131
132
  homepage: https://github.com/david-blackwood/edifact_rails
132
133
  licenses: