edifact_rails 1.1.1 → 1.2.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: 1343d4c915c6df4b716c31560cf58d5b350dc18ad074c12c3c562b13b0ce3717
4
- data.tar.gz: 6067a62e4bdf24f2be92306a6aa4d4e21b9aa3b69ac6eff3eba8a8ae97229627
3
+ metadata.gz: 544b29b77bfc7ab9fd4ccb5bffaaae97bf236aab7b45e66c008297a76dfd7f62
4
+ data.tar.gz: 62df8594d976c3e38b90757c956ff1ee53a4e3404e8d2570f328b2dd2be34a5d
5
5
  SHA512:
6
- metadata.gz: 8e1ce36c70da1e16fce1596fd43b345f5a284a77b82645b901d6eb88f25ae45c45d152aae760477526bd1c0230f59890f602c40fbded3cd3be61a80a8926e1c7
7
- data.tar.gz: 2feffb89e9df203d37eb0f8aab6bcb673630dd637092a5fe406de9eff0d244d074fd964b1d10e7c68a75a48b9f75f58168240d42e7c2d900f469468be0a049d5
6
+ metadata.gz: 8892d1d110a2bd8157a0f97fbd4baf627e10fac408a2f85244eb8879a65bb58713d2c68cf9d16018694121a3edffd99b47ffcbec932dc9aecc53eacddbd3bb14
7
+ data.tar.gz: 89a289437212f3291ad9ba95b8b9412d62e925c24e161e61572e8a618bd270091a1723a2b849fcf8eac04fabb8c56061d6fd7eebddfb2af38c865dd84676f3d9
data/CHANGELOG.md CHANGED
@@ -7,3 +7,12 @@
7
7
  ## 1.1.0 (27/04/2023)
8
8
 
9
9
  * Added support for TRADACOMS input
10
+
11
+ ## 1.1.1 (4/05/2023)
12
+
13
+ * Fixed crash caused by running the gem in a production environment.
14
+
15
+ ## 1.2.0 (31/05/2024)
16
+
17
+ * Added support for UNA segments. Special characters different from the defaults can now be used.
18
+ * Added una_special_characters method that returns just the special characters.
data/README.md CHANGED
@@ -15,12 +15,12 @@ This gem has been tested on the following ruby versions:
15
15
  * 3.1.2
16
16
  * 3.2.2
17
17
 
18
- ## Getting started
18
+ ## Getting started
19
19
 
20
20
  In your `Gemfile`:
21
21
 
22
22
  ```ruby
23
- gem 'edifact_rails', '~> 1.0'
23
+ gem 'edifact_rails', '~> 1.1'
24
24
  ```
25
25
 
26
26
  Otherwise:
@@ -47,6 +47,18 @@ ruby_array = EdifactRails.parse_file("your/file/path")
47
47
  ruby_array = EdifactRails.parse("LIN+1+1+0764569104:IB'QTY+1:25'")
48
48
  ```
49
49
 
50
+ You can pull just the special characters from the UNA segment (or the defaults if no UNA segment is present):
51
+ ```ruby
52
+ una_special_characters = EdifactRails.una_special_characters(your_string_input)
53
+ # una_special_characters =>
54
+ {
55
+ component_data_element_seperator: ":",
56
+ data_element_seperator: "+",
57
+ escape_character: "?",
58
+ segment_seperator: "'"
59
+ }
60
+ ```
61
+
50
62
  ## Output
51
63
 
52
64
  ### EDIFACT
@@ -2,21 +2,21 @@
2
2
 
3
3
  module EdifactRails
4
4
  class Parser
5
- ESCAPE_CHARACTER = "?"
6
- SEGMENT_SEPARATOR = "'"
7
- DATA_ELEMENT_SEPARATOR = "+"
8
- COMPONENT_DATA_ELEMENT_SEPARATOR = ":"
9
-
10
5
  def initialize
11
- # Escape the special characters for use in regex later on
12
- @escape_char_rx = Regexp.quote(EdifactRails::Parser::ESCAPE_CHARACTER)
13
- @segment_separator_rx = Regexp.quote(EdifactRails::Parser::SEGMENT_SEPARATOR)
14
- @data_element_separator_rx = Regexp.quote(EdifactRails::Parser::DATA_ELEMENT_SEPARATOR)
15
- @component_data_element_separator_rx = Regexp.quote(EdifactRails::Parser::COMPONENT_DATA_ELEMENT_SEPARATOR)
6
+ # Set default separators
7
+ # They can be overridden by the UNA segment in #detect_special_characters
8
+ set_special_characters
16
9
  end
17
10
 
18
- # Treat the input a little, split the input string into segments, parse them
11
+ # Treat the input, split the input string into segments, parse those segments
19
12
  def parse(string)
13
+ # Trim newlines and excess spaces around those newlines
14
+ string = string.gsub(/\s*\n\s*/, "")
15
+
16
+ # Check for UNA segment, update special characters if so
17
+ detect_special_characters(string)
18
+
19
+ # Does some funky regex maniulation to handle escaped special characters
20
20
  string = treat_input(string)
21
21
 
22
22
  # Split the input string into segments
@@ -25,25 +25,73 @@ module EdifactRails
25
25
  # Detect if the input is a tradacoms file
26
26
  @is_tradacoms = segments.map { |s| s[3] }.uniq == ["="]
27
27
 
28
- # Drop the UNA segment, if present
28
+ # Drop the UNA segment, if present (we have already dealt with it in #detect_special_characters)
29
29
  segments.reject! { |s| s[0..2] == "UNA" }
30
30
 
31
31
  # Parse the segments
32
32
  segments.map { |segment| parse_segment(segment) }
33
33
  end
34
34
 
35
+ # Given an input string, return the special characters as defined by the UNA segment
36
+ # If no UNA segment is present, returns the default special characters
37
+ def una_special_characters(string)
38
+ detect_special_characters(string)
39
+
40
+ {
41
+ component_data_element_seperator: @component_data_element_seperator,
42
+ data_element_seperator: @data_element_seperator,
43
+ escape_character: @escape_character,
44
+ segment_seperator: @segment_seperator
45
+ }
46
+ end
47
+
35
48
  private
36
49
 
37
- def treat_input(string)
38
- # Trim newlines and excess spaces around those newlines
39
- string = string.gsub(/\s*\n\s*/, "")
50
+ def set_special_characters(
51
+ component_data_element_seperator =
52
+ EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:component_data_element_seperator],
53
+ data_element_seperator = EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:data_element_seperator],
54
+ escape_character = EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:escape_character],
55
+ segment_seperator = EdifactRails::DEFAULT_SPECIAL_CHARACTERS[:segment_seperator]
56
+ )
57
+ # Set the special characters
58
+ @component_data_element_seperator = component_data_element_seperator
59
+ @data_element_seperator = data_element_seperator
60
+ @escape_character = escape_character
61
+ @segment_seperator = segment_seperator
62
+
63
+ # Escape the special characters for use in regex later on
64
+ @component_data_element_separator_rx = Regexp.quote(@component_data_element_seperator)
65
+ @data_element_separator_rx = Regexp.quote(@data_element_seperator)
66
+ @escape_char_rx = Regexp.quote(@escape_character)
67
+ @segment_separator_rx = Regexp.quote(@segment_seperator)
68
+ end
69
+
70
+ def detect_special_characters(string)
71
+ # UNA tags must be at the start of the input otherwise they are ignored
72
+ return unless string[0..2] == "UNA"
40
73
 
74
+ # UNA segments look like this:
75
+ #
76
+ # UNA:+.? '
77
+ #
78
+ # UNA followed by 6 special characters which are, in order:
79
+ # 1. Component data element separator
80
+ # 2. Data element separator
81
+ # 3. Decimal notation (must be . or ,)
82
+ # 4. Release character (aka escape character)
83
+ # 5. Reserved for future use, so always a space for now
84
+ # 6. Segment terminator
85
+ set_special_characters(string[3], string[4], string[6], string[8])
86
+ end
87
+
88
+ def treat_input(string)
41
89
  # Prepare regex
42
90
  other_specials_rx = Regexp.quote(
43
91
  [
44
- EdifactRails::Parser::SEGMENT_SEPARATOR,
45
- EdifactRails::Parser::DATA_ELEMENT_SEPARATOR,
46
- EdifactRails::Parser::COMPONENT_DATA_ELEMENT_SEPARATOR
92
+ @segment_seperator,
93
+ @data_element_seperator,
94
+ @component_data_element_seperator
47
95
  ].join
48
96
  )
49
97
 
@@ -72,7 +120,7 @@ module EdifactRails
72
120
  # If the input is a tradacoms file, the segment tag will be proceeded by '=' instead of '+'
73
121
  # 'QTY=1+A:B' instead of 'QTY+1+A:B'
74
122
  # Fortunately, this is easily handled by simply changing these "="s into "+"s before the split
75
- segment[3] = EdifactRails::Parser::DATA_ELEMENT_SEPARATOR if @is_tradacoms && segment.length >= 4
123
+ segment[3] = @data_element_seperator if @is_tradacoms && segment.length >= 4
76
124
 
77
125
  # Segments are made up of data elements
78
126
  data_elements = segment.split(/(?<!#{@escape_char_rx})#{@data_element_separator_rx}/)
@@ -99,16 +147,16 @@ module EdifactRails
99
147
  component.strip!
100
148
 
101
149
  # Prepare regex
102
- all_special_characters = [
103
- EdifactRails::Parser::SEGMENT_SEPARATOR,
104
- EdifactRails::Parser::DATA_ELEMENT_SEPARATOR,
105
- EdifactRails::Parser::COMPONENT_DATA_ELEMENT_SEPARATOR,
106
- EdifactRails::Parser::ESCAPE_CHARACTER
150
+ all_special_characters_string = [
151
+ @segment_seperator,
152
+ @data_element_seperator,
153
+ @component_data_element_seperator,
154
+ @escape_character
107
155
  ].join
108
156
 
109
157
  # If the component has escaped characters in it, remove the escape character and return the character as is
110
158
  # "?+" -> "+", "??" -> "?"
111
- component.gsub!(/#{@escape_char_rx}([#{Regexp.quote(all_special_characters)}])/, '\1')
159
+ component.gsub!(/#{@escape_char_rx}([#{Regexp.quote(all_special_characters_string)}])/, '\1')
112
160
 
113
161
  # Convert empty strings to nils
114
162
  component = nil if component.empty?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EdifactRails
4
- VERSION = "1.1.1"
4
+ VERSION = "1.2.0"
5
5
  end
data/lib/edifact_rails.rb CHANGED
@@ -3,12 +3,24 @@
3
3
  require "edifact_rails/parser"
4
4
 
5
5
  module EdifactRails
6
+ DEFAULT_SPECIAL_CHARACTERS = {
7
+ component_data_element_seperator: ":",
8
+ data_element_seperator: "+",
9
+ escape_character: "?",
10
+ segment_seperator: "'"
11
+ }.freeze
12
+
6
13
  def self.parse(string)
7
14
  parser = EdifactRails::Parser.new
8
- parser.parse string
15
+ parser.parse(string)
9
16
  end
10
17
 
11
18
  def self.parse_file(file_path)
12
19
  parse(File.read(file_path).split("\n").join)
13
20
  end
21
+
22
+ def self.una_special_characters(string)
23
+ parser = EdifactRails::Parser.new
24
+ parser.una_special_characters(string)
25
+ end
14
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: edifact_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Blackwood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-04 00:00:00.000000000 Z
11
+ date: 2024-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug