match_point 0.0.13 → 0.0.14

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.
@@ -1,3 +1,3 @@
1
1
  module MatchPoint
2
- VERSION = '0.0.13'
2
+ VERSION = '0.0.14'
3
3
  end
@@ -4,8 +4,7 @@ module MatchPoint
4
4
  SPEC_NODE="MatchPointSpec"
5
5
 
6
6
  def self.from_xml template, xml, error_node
7
- instance = self.new template
8
- instance.xml= xml
7
+ instance = self.new template, xml
9
8
  instance.error_node = error_node
10
9
  instance
11
10
  end
@@ -20,10 +19,6 @@ module MatchPoint
20
19
  @error_node = error_node
21
20
  end
22
21
 
23
- def xml= an_xml
24
- @xml = Nokogiri::XML(an_xml)
25
- end
26
-
27
22
  def to_xml
28
23
  @xml.to_xml
29
24
  end
@@ -59,7 +54,9 @@ module MatchPoint
59
54
  !@xml.at_xpath(@error_node[:path]).has_attribute?(@error_node[:attribute])
60
55
  end
61
56
 
62
- def remove_unspecified_values
57
+ def inject_node(new_node, injection_path)
58
+ self_node = @xml.at_xpath(injection_path)
59
+ self_node.add_child(new_node)
63
60
 
64
61
  end
65
62
 
@@ -81,7 +78,7 @@ module MatchPoint
81
78
 
82
79
  def has_children? node
83
80
  return false if node.nil?
84
- !node.children.select { |child| !is_a? Nokogiri::XML::Text}.empty?
81
+ !node.children.select { |child| !is_a? Nokogiri::XML::Text }.empty?
85
82
  end
86
83
 
87
84
  def remove_parent_node parent
@@ -89,9 +86,9 @@ module MatchPoint
89
86
  remove_parent_node parent.parent if has_children? parent.parent
90
87
  end
91
88
 
92
- def initialize template
89
+ def initialize template, xml = nil
93
90
  @template= Nokogiri::XML(template)
94
- @xml = Nokogiri::XML(template)
91
+ @xml = Nokogiri::XML(xml || template)
95
92
  @nodes_paths = {}
96
93
 
97
94
  synchronize_object_to_xml
@@ -115,8 +112,6 @@ module MatchPoint
115
112
 
116
113
  def define_accessors_for node_in_template
117
114
  attr_path = remove_spec_node_from(node_in_template.path)
118
- attr_value = @xml.xpath(attr_path).text
119
-
120
115
  add_attr_reader_for node_in_template, attr_path
121
116
  add_attr_writer_for node_in_template
122
117
  end
@@ -126,9 +121,28 @@ module MatchPoint
126
121
  end
127
122
 
128
123
  def add_attr_reader_for node_in_template, attr_path
124
+ if @xml.xpath(attr_path).size == 1
125
+ add_unique_attr_reader_for(node_in_template, attr_path)
126
+ else
127
+ add_multiple_attr_reader_for(node_in_template, attr_path)
128
+ end
129
+ end
130
+
131
+ def add_unique_attr_reader_for node_in_template, attr_path
132
+ define_singleton_method node_in_template.text.to_sym, lambda {
133
+ @xml.at_xpath(attr_path).text
134
+ }
135
+ end
136
+
137
+ def add_multiple_attr_reader_for node_in_template, attr_path
129
138
  define_singleton_method node_in_template.text.to_sym, lambda {
130
- value = @xml.at_xpath(attr_path).text
139
+ raise StandardError.new 'Node is duplicated'
131
140
  }
141
+ define_singleton_method((node_in_template.text + '_with').to_sym, lambda { | hash |
142
+ key, value = hash.first
143
+ @xml.at_xpath(remove_spec_node_from(@nodes_paths[key])+"[text()=\" + value.to_s +\"]")
144
+ @xml.at_xpath(attr_path + '[' + key.to_s + '=' + value + ']').text
145
+ })
132
146
  end
133
147
 
134
148
  def add_attr_writer_for node_in_template
@@ -37,7 +37,7 @@ describe MatchPoint do
37
37
  </TIX>"
38
38
  end
39
39
 
40
- let(:uncomplete_xml) do
40
+ let(:uncomplete_template) do
41
41
  "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
42
42
  <TIX errCd=\"123\" version=\"TIX 4.11.0\">
43
43
  <AccessControl method=\"AccessControlAuthenticate\">
@@ -45,6 +45,28 @@ describe MatchPoint do
45
45
  <AuthID><MatchPointSpec>auth_id</MatchPointSpec></AuthID>
46
46
  <SubNode>
47
47
  <AnotherId><MatchPointSpec>another_id</MatchPointSpec></AnotherId>
48
+ <SomeSubNodeValue><MatchPointSpec>some_sub_node_value</MatchPointSpec></SomeSubNodeValue>
49
+ </SubNode>
50
+ </AuthRequest>
51
+ </AccessControl>
52
+ </TIX>"
53
+ end
54
+
55
+
56
+
57
+ let(:duplicated_nodes_xml) do
58
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
59
+ <TIX errCd=\"123\" version=\"TIX 4.11.0\">
60
+ <AccessControl method=\"AccessControlAuthenticate\">
61
+ <AuthRequest>
62
+ <AuthID>1234</AuthID>
63
+ <SubNode>
64
+ <AnotherId>18</AnotherId>
65
+ <SomeSubNodeValue>v18</SomeSubNodeValue>
66
+ </SubNode>
67
+ <SubNode>
68
+ <AnotherId>19</AnotherId>
69
+ <SomeSubNodeValue>v19</SomeSubNodeValue>
48
70
  </SubNode>
49
71
  </AuthRequest>
50
72
  </AccessControl>
@@ -109,10 +131,34 @@ describe MatchPoint do
109
131
  end
110
132
 
111
133
  it "should remove auth_id because was not specified" do
112
- some_operation = MatchPoint::XmlInstance.from_hash uncomplete_xml, {:auth_id => "1234" }
134
+ some_operation = MatchPoint::XmlInstance.from_hash uncomplete_template, {:auth_id => "1234"}
113
135
  some_operation.auth_id.should == "1234"
114
136
  some_operation.at_xpath("/TIX/AccessControl/AuthRequest/SubNode/AnotherId").nil?.should be_true
115
137
  some_operation.at_xpath("/TIX/AccessControl/AuthRequest/SubNode").nil?.should be_true
116
138
  end
139
+
140
+ it "should throw exception if there are multiple nodes with the same path" do
141
+ some_operation = MatchPoint::XmlInstance.from_xml uncomplete_template, duplicated_nodes_xml, error_node
142
+ expect { some_operation.some_sub_node_value }.to raise_error(StandardError, 'Node is duplicated')
143
+ end
144
+
145
+ it "should return a specific value on multiple equal nodes" do
146
+ some_operation = MatchPoint::XmlInstance.from_xml uncomplete_template, duplicated_nodes_xml, error_node
147
+ some_operation.some_sub_node_value_with(:another_id => '18').should == 'v18'
148
+ some_operation.some_sub_node_value_with(:another_id => '19').should == 'v19'
149
+ end
150
+
151
+ it "should be able to inject some node into an xml path" do
152
+ injection_path = "/TIX/AccessControl/AuthRequest/SubNode[AnotherId=18]"
153
+ auth_node = response.at_xpath("/TIX/AccessControl/AuthRequest")
154
+ some_operation = MatchPoint::XmlInstance.from_xml uncomplete_template, duplicated_nodes_xml, error_node
155
+
156
+ some_operation.inject_node(auth_node, injection_path)
157
+
158
+ some_operation.at_xpath(injection_path + "/AuthRequest").to_s.should == auth_node.to_s
159
+
160
+ end
161
+
117
162
  end
163
+
118
164
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: match_point
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.13
4
+ version: 0.0.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-17 00:00:00.000000000 Z
12
+ date: 2013-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec