baldr 0.3.6 → 0.3.7

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.
@@ -0,0 +1,3 @@
1
+ class Baldr::Error::ParsingError < Baldr::Error
2
+
3
+ end
@@ -0,0 +1,3 @@
1
+ class Baldr::Error::ValidationError < Baldr::Error
2
+
3
+ end
data/lib/baldr/parser.rb CHANGED
@@ -5,7 +5,12 @@ class Baldr::Parser
5
5
  def initialize(input, params = {})
6
6
  @params = params
7
7
  @input = input
8
+
8
9
  parse
10
+
11
+ if @params[:skip_validation] != true && successful?
12
+ validate
13
+ end
9
14
  end
10
15
 
11
16
  def successful?
@@ -28,25 +33,30 @@ class Baldr::Parser
28
33
  end
29
34
  end
30
35
 
36
+ def validate
37
+ if @roots
38
+ @roots.each { |e| Baldr::Validator.validate!(e, @grammar, @version) }
39
+ end
40
+ rescue Baldr::Error => e
41
+ @error = e.message
42
+ end
43
+
31
44
  protected
32
45
 
33
46
  def parse
34
47
  detect_first_segment
35
48
  if @root_type == :transaction
36
- raise Baldr::Error, "separators must be manually defined for parsing transactions" unless @params[:separators]
49
+ raise Baldr::Error::ParsingError, "separators must be manually defined for parsing transactions" unless @params[:separators]
37
50
  @separators = @params[:separators]
38
- grammar = @params[:grammar] || Baldr::Grammar::Transaction
39
- raise Baldr::Error, "grammar version must be manually defined for parsing transactions" unless @params[:version]
40
- version = @params[:version]
51
+ @grammar = @params[:grammar] || Baldr::Grammar::Transaction
52
+ raise Baldr::Error::ParsingError, "grammar version must be manually defined for parsing transactions" unless @params[:version]
53
+ @version = @params[:version]
41
54
  else
42
55
  detect_separators
43
- grammar = @params[:grammar] || Baldr::Grammar::Envelope
44
- version = nil
56
+ @grammar = @params[:grammar] || Baldr::Grammar::Envelope
57
+ @version = nil
45
58
  end
46
- @roots = build_tree(split_segments, grammar, version)
47
- @roots.each { |e| Baldr::Validator.validate!(e, grammar, version) }
48
-
49
- self
59
+ @roots = build_tree(split_segments, @grammar, @version)
50
60
  rescue Baldr::Error => e
51
61
  @error = e.message
52
62
  end
@@ -57,7 +67,7 @@ class Baldr::Parser
57
67
  elsif @input[0..1] == 'ST'
58
68
  @root_type = :transaction
59
69
  else
60
- raise Baldr::Error, "doesn't begin with ISA or ST..."
70
+ raise Baldr::Error::ParsingError, "doesn't begin with ISA or ST..."
61
71
  end
62
72
  end
63
73
 
@@ -103,7 +113,7 @@ class Baldr::Parser
103
113
  end
104
114
  end
105
115
 
106
- raise Baldr::Error, 'invalid characters in the end of interchange' if skip > 0
116
+ raise Baldr::Error::ParsingError, 'invalid characters in the end of interchange' if skip > 0
107
117
 
108
118
  segments
109
119
  end
@@ -11,7 +11,7 @@ module Baldr::Validator
11
11
 
12
12
  def validate_tree!(segment, grammar, structure, version)
13
13
  record_defs = grammar.record_defs
14
- raise Baldr::Error, "unknown segment #{segment.id}" unless record_defs[segment.id]
14
+ raise Baldr::Error::ValidationError, "unknown segment #{segment.id}" unless record_defs[segment.id]
15
15
 
16
16
  record_defs[segment.id].each.with_index do |r, i|
17
17
  element = segment.elements[i]
@@ -33,9 +33,9 @@ module Baldr::Validator
33
33
 
34
34
  l += 1
35
35
  elsif loop
36
- raise Baldr::Error, "segment #{s[:id]} is required, but #{loop.id} was found" if s[:min] > 0
36
+ raise Baldr::Error::ValidationError, "segment #{s[:id]} is required, but #{loop.id} was found" if s[:min] > 0
37
37
  else
38
- raise Baldr::Error, "segment #{s[:id]} is required, but nothing was found" if s[:min] > 0
38
+ raise Baldr::Error::ValidationError, "segment #{s[:id]} is required, but nothing was found" if s[:min] > 0
39
39
  end
40
40
  end
41
41
 
@@ -46,13 +46,13 @@ module Baldr::Validator
46
46
  end
47
47
 
48
48
  def check_loop_count(loop, grammar)
49
- raise Baldr::Error, "#{loop.id} loop is too long: #{loop.count} segments, maximum #{grammar[:max]}" if loop.count > grammar[:max]
50
- raise Baldr::Error, "#{loop.id} loop is too short: #{loop.count} segments, minimum #{grammar[:min]}" if loop.count < grammar[:min]
49
+ raise Baldr::Error::ValidationError, "#{loop.id} loop is too long: #{loop.count} segments, maximum #{grammar[:max]}" if loop.count > grammar[:max]
50
+ raise Baldr::Error::ValidationError, "#{loop.id} loop is too short: #{loop.count} segments, minimum #{grammar[:min]}" if loop.count < grammar[:min]
51
51
  end
52
52
 
53
53
  def check_required(r, element)
54
54
  if r[:required] && (element.nil? || element.size == 0)
55
- raise Baldr::Error, "element #{r[:id]} is required"
55
+ raise Baldr::Error::ValidationError, "element #{r[:id]} is required"
56
56
  end
57
57
  end
58
58
 
@@ -67,11 +67,11 @@ module Baldr::Validator
67
67
 
68
68
  def check_max_and_min_for_string(r, element)
69
69
  if r[:max] && element.length > r[:max]
70
- raise Baldr::Error, "#{r[:id]} is too long: #{element.length} characters, maximum #{r[:max]}"
70
+ raise Baldr::Error::ValidationError, "#{r[:id]} is too long: #{element.length} characters, maximum #{r[:max]}"
71
71
  end
72
72
 
73
73
  if r[:min] && element.length < r[:min]
74
- raise Baldr::Error, "#{r[:id]} is too short: #{element.length} characters, minimum #{r[:min]}"
74
+ raise Baldr::Error::ValidationError, "#{r[:id]} is too short: #{element.length} characters, minimum #{r[:min]}"
75
75
  end
76
76
  end
77
77
 
data/lib/baldr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Baldr
2
- VERSION = '0.3.6'
2
+ VERSION = '0.3.7'
3
3
  end
data/lib/baldr.rb CHANGED
@@ -10,6 +10,8 @@ require 'baldr/loop'
10
10
  require 'baldr/validator'
11
11
 
12
12
  require 'baldr/error'
13
+ require 'baldr/error/parsing_error'
14
+ require 'baldr/error/validation_error'
13
15
 
14
16
  require 'baldr/utils'
15
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baldr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -37,6 +37,8 @@ files:
37
37
  - lib/baldr/builder.rb
38
38
  - lib/baldr/envelope.rb
39
39
  - lib/baldr/error.rb
40
+ - lib/baldr/error/parsing_error.rb
41
+ - lib/baldr/error/validation_error.rb
40
42
  - lib/baldr/functional_group.rb
41
43
  - lib/baldr/grammar.rb
42
44
  - lib/baldr/grammar/envelope.rb