wsdl-reader 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  require "active_support/core_ext/hash/keys"
2
2
  require "active_support/core_ext/module/delegation"
3
+ require "active_support/core_ext/string/inflections"
3
4
  require "wsdl-reader/version"
4
5
  require "wsdl-reader/error"
5
6
  require "wsdl-reader/parser"
@@ -20,7 +20,14 @@ module WSDL
20
20
  end
21
21
 
22
22
  def all_operations
23
- self.map { |_, binding| binding.operations.keys }.flatten
23
+ self.map { |_, binding| binding.operations.values }.flatten
24
+ end
25
+
26
+ def operations(binding=nil)
27
+ bindings = binding.nil? ? self : ({ a: self[binding] })
28
+ bindings.inject({ }) do |hash, (_, b)|
29
+ hash.merge b.operations
30
+ end
24
31
  end
25
32
 
26
33
  def operation?(operation_name)
@@ -32,53 +39,63 @@ module WSDL
32
39
  attr_reader :operations
33
40
  attr_reader :name
34
41
  attr_reader :type
42
+ attr_reader :type_nns
35
43
  attr_reader :style
36
44
  attr_reader :transport
37
45
 
38
46
  def initialize(element)
39
- @operations = Hash.new
47
+ @operations = Operations.new
40
48
  @name = element.attributes['name']
41
49
  @type = element.attributes['type'] # because of Object#type
50
+ @type_nns = @type.split(':').last rescue @type
42
51
  @style = nil
43
52
  @transport = nil
44
53
 
45
54
  # Process all binding and operation
46
- element.find_all { |e| e.class == REXML::Element }.each { |operation|
47
- case operation.name
55
+ element.find_all { |e| e.class == REXML::Element }.each do |operation_element|
56
+ case operation_element.name
48
57
  when "binding" # soap:binding
49
- store_style_and_transport(element, operation)
58
+ store_style_and_transport(operation_element)
50
59
 
51
60
  when "operation"
52
- store_operation_name(element, operation)
53
-
54
- operation.find_all { |e| e.class == REXML::Element }.each { |action|
55
- store_action(action, element, operation)
56
- }
61
+ append_operation(operation_element)
57
62
  else
58
63
  warn "Ignoring element `#{operation.name}' in binding `#{element.attributes['name']}'"
59
64
  end
60
- }
65
+ end
61
66
  end
62
67
 
63
68
  def operation?(name)
64
- operations.include? name
69
+ operations.include? camelize_operation(name)
70
+ end
71
+
72
+ def lookup_port_type(port_types)
73
+ port_types[type.split(':').last]
65
74
  end
66
75
 
67
76
  protected
68
77
 
69
- def store_style_and_transport(element, operation)
70
- operation.attributes.each do |name, value|
78
+ def append_operation(operation_element)
79
+ @operations << Operation.new(operation_element, self)
80
+ end
81
+
82
+ def store_style_and_transport(operation_element)
83
+ operation_element.attributes.each do |name, value|
71
84
  case name
72
85
  when 'style'
73
86
  @style = value
74
87
  when 'transport'
75
88
  @transport = value
76
- else
77
- warn "Ignoring attribute `#{name}' for wsdlsoap:binding in binding `#{element.attributes['name']}'"
78
89
  end
79
90
  end
80
91
  end
81
92
 
93
+ def camelize_operation(name)
94
+ name.to_s.tap do |name_string|
95
+ return name_string.camelize :lower if name_string.underscore == name_string
96
+ end
97
+ end
98
+
82
99
  def store_operation_name(element, operation)
83
100
  operation.attributes.each do |name, value|
84
101
  case name
@@ -90,7 +107,7 @@ module WSDL
90
107
  end
91
108
  end
92
109
 
93
- def fill_action(action, operation)
110
+ def fill_action(action, operation, element)
94
111
  filling_action = { }
95
112
 
96
113
  action.attributes.each do |name, value|
@@ -109,6 +126,12 @@ module WSDL
109
126
  filling_action[:body] = { }
110
127
 
111
128
  body.attributes.each { |name, value| filling_action[:body][name.to_sym] = value }
129
+
130
+ when "fault"
131
+ filling_action[:body] = { }
132
+
133
+ body.attributes.each { |name, value| filling_action[:body][name.to_sym] = value }
134
+
112
135
  else
113
136
  warn "Ignoring element `#{body.name}' in #{action.name} `#{action.attributes['name']}' in operation `#{operation.attributes['name']}' for binding `#{element.attributes['name']}'"
114
137
  end
@@ -130,13 +153,13 @@ module WSDL
130
153
  end
131
154
  end
132
155
  when "input"
133
- current_operation(operation)[:input] = fill_action(action, operation)
156
+ current_operation(operation)[:input] = fill_action(action, operation, element)
134
157
 
135
158
  when "output"
136
- current_operation(operation)[:output] = fill_action(action, operation)
159
+ current_operation(operation)[:output] = fill_action(action, operation, element)
137
160
 
138
161
  when "fault"
139
- current_operation(operation)[:fault] = fill_action(action, operation)
162
+ current_operation(operation)[:fault] = fill_action(action, operation, element)
140
163
 
141
164
  else
142
165
  warn "Ignoring element `#{action.name}' in operation `#{operation.attributes['name']}' for binding `#{element.attributes['name']}'"
@@ -149,4 +172,4 @@ module WSDL
149
172
 
150
173
  end
151
174
  end
152
- end
175
+ end
@@ -3,6 +3,30 @@ module WSDL
3
3
  class ParserError < StandardError
4
4
  end
5
5
 
6
+ class LookupError < StandardError
7
+ end
8
+
9
+ class ManyOperationsFoundError < LookupError
10
+ def initialize(type, element_name)
11
+ @type, @element_name = type, element_name
12
+ end
13
+
14
+
15
+ def message
16
+ "More than one operations found for element: [#@element_name]"
17
+ end
18
+ end
19
+
20
+ class OperationNotFoundError < LookupError
21
+ def initialize(type, element_name)
22
+ @type, @element_name = type, element_name
23
+ end
24
+
25
+ def message
26
+ "No operation matches for element: [#@element_name]"
27
+ end
28
+ end
29
+
6
30
  class FileOpenError < ParserError
7
31
  end
8
32
  end
@@ -1,6 +1,32 @@
1
1
  module WSDL
2
2
  module Reader
3
3
  class Messages < Hash
4
+
5
+ def lookup_operations_by_element(type, element_name, port_types)
6
+ messages = lookup_messages_by_element(element_name)
7
+ messages.map do |message|
8
+ port_types.lookup_operations_by_message(type, message)
9
+ end.flatten
10
+ end
11
+
12
+ def lookup_operation_by_element! (type, element_name, port_types)
13
+ messages = lookup_operations_by_element type, element_name, port_types
14
+ case messages.size
15
+ when 1
16
+ messages.first
17
+ when 0
18
+ raise OperationNotFoundError.new type, element_name
19
+ else
20
+ raise ManyOperationsFoundError.new type, element_name
21
+ end
22
+ end
23
+
24
+ def lookup_messages_by_element(element_name)
25
+ values.select do |message|
26
+ message.parts.values.find { |part| part[:element].split(':').last == element_name }
27
+ end
28
+ end
29
+
4
30
  end
5
31
 
6
32
  class Message
@@ -9,11 +35,15 @@ module WSDL
9
35
 
10
36
  def initialize(element)
11
37
  @parts = Hash.new
12
- @name = element.attributes['name']
38
+ @name = element.attributes['name']
13
39
 
14
40
  process_all_parts element
15
41
  end
16
42
 
43
+ def element
44
+ parts.map { |_, hash| hash[:element] }.first
45
+ end
46
+
17
47
  protected
18
48
 
19
49
  def process_all_parts(element)
@@ -36,7 +66,7 @@ module WSDL
36
66
  current_part[:name] = value
37
67
  when 'element'
38
68
  current_part[:element] = value
39
- current_part[:mode] = :element
69
+ current_part[:mode] = :element
40
70
  when 'type'
41
71
  current_part[:type] = value
42
72
  current_part[:mode] = :type
@@ -47,4 +77,4 @@ module WSDL
47
77
  end
48
78
  end
49
79
  end
50
- end
80
+ end
@@ -0,0 +1,85 @@
1
+ module WSDL
2
+ module Reader
3
+
4
+ class Operations < Hash
5
+ def <<(operation)
6
+ self[operation.name] = operation
7
+ end
8
+ end
9
+
10
+ class Operation
11
+ attr_reader :soap_action, :style, :input, :output, :fault
12
+ attr_reader :name, :binding
13
+
14
+ def initialize(element, binding)
15
+ @name = element.attributes['name']
16
+ @binding = binding
17
+
18
+ parse! element
19
+ end
20
+
21
+ def method_name
22
+ @name.underscore
23
+ end
24
+
25
+ def message
26
+ @name
27
+ end
28
+
29
+ def lookup_element(port_types, messages)
30
+ message = port_types.lookup_operation_message :input, self, messages
31
+ message.element.split(':').last
32
+ end
33
+
34
+ private
35
+
36
+ def parse!(operation_element)
37
+ operation_element.find_all { |e| e.class == REXML::Element }.each do |action_element|
38
+ store_action(action_element)
39
+ end
40
+ end
41
+
42
+ def store_action(action_element)
43
+ case action_element.name
44
+ when "operation" # soap:operation
45
+ action_element.attributes.each do |name, value|
46
+ case name
47
+ when 'soapAction'
48
+ @soap_action = value
49
+ when 'style'
50
+ @style = value
51
+ end
52
+ end
53
+ when "input"
54
+ @input = fill_action(action_element)
55
+
56
+ when "output"
57
+ @output = fill_action(action_element)
58
+
59
+ when "fault"
60
+ @fault = fill_action(action_element)
61
+ end
62
+ end
63
+
64
+ def fill_action(action_element)
65
+ filling_action = { }
66
+
67
+ filling_action[:name] = action_element.attributes['name']
68
+
69
+ # Store body
70
+ action_element.find_all { |e| e.class == REXML::Element }.each do |body|
71
+ if body.name = "body"
72
+ filling_action[:body] = { }
73
+
74
+ body.attributes.each do |name, value|
75
+ filling_action[:body][name.to_sym] = value
76
+ end
77
+ end
78
+ end
79
+
80
+ filling_action
81
+ end
82
+
83
+ end
84
+ end
85
+ end
@@ -4,6 +4,7 @@ require 'rexml/document'
4
4
  require 'wsdl-reader/message'
5
5
  require 'wsdl-reader/port_type'
6
6
  require 'wsdl-reader/binding'
7
+ require 'wsdl-reader/operation'
7
8
  require 'wsdl-reader/service'
8
9
  require 'wsdl-reader/xsd'
9
10
 
@@ -14,7 +15,8 @@ module WSDL
14
15
  attr_reader :prefixes, :target_namespace
15
16
  attr_reader :document, :uri
16
17
 
17
- delegate :get_operations, to: :bindings
18
+ delegate :operations, :operation?, to: :bindings
19
+ delegate :lookup_operation_by_element!, to: :messages
18
20
 
19
21
  def initialize(uri)
20
22
  @uri = uri
@@ -104,4 +106,4 @@ module WSDL
104
106
  end
105
107
  end
106
108
  end
107
- end
109
+ end
@@ -1,6 +1,18 @@
1
1
  module WSDL
2
2
  module Reader
3
3
  class PortTypes < Hash
4
+ def lookup_operation_message(type, operation, messages) # TODO: lookup_message_by_operation
5
+ each do |_, port_type|
6
+ message = port_type.lookup_operation_message type, operation, messages
7
+ return message if message
8
+ end
9
+ end
10
+
11
+ def lookup_operations_by_message(type, message)
12
+ inject([]) do |array, (_, port_type)|
13
+ array + port_type.lookup_operations_by_message(type, message)
14
+ end
15
+ end
4
16
  end
5
17
 
6
18
  class PortType
@@ -9,11 +21,31 @@ module WSDL
9
21
 
10
22
  def initialize(element)
11
23
  @operations = Hash.new
12
- @name = element.attributes['name']
24
+ @name = element.attributes['name']
13
25
 
14
26
  process_all_operations(element)
15
27
  end
16
28
 
29
+ def lookup_operation_message(type, operation, messages)
30
+ operation_name = operation.is_a?(Operation) ? operation.name : operation.to_s
31
+ messages[@operations[operation_name][type][:message].split(':').last]
32
+ end
33
+
34
+ def lookup_operations_by_message(type, message)
35
+ ops = @operations.values.select do |operation|
36
+ operation_message? type, operation, message
37
+ end
38
+ ops.map do |operation_hash|
39
+ operation_hash[:name]
40
+ end
41
+ end
42
+
43
+ def operation_message?(type, operation, message)
44
+ message_name = message.is_a?(Message) ? message.name : message.to_s
45
+ operation[type][:message] == message_name ||
46
+ operation[type][:message].split(':').last == message_name
47
+ end
48
+
17
49
  protected
18
50
 
19
51
  def store_attributes(element, operation)
@@ -79,17 +111,17 @@ module WSDL
79
111
 
80
112
  operation.find_all { |e| e.class == REXML::Element }.each do |action|
81
113
  case action.name
82
- when "input"
83
- store_input(action, element, operation)
114
+ when "input"
115
+ store_input(action, element, operation)
84
116
 
85
- when "output"
86
- store_output(action, element, operation)
117
+ when "output"
118
+ store_output(action, element, operation)
87
119
 
88
- when "fault"
89
- store_fault(action, element, operation)
120
+ when "fault"
121
+ store_fault(action, element, operation)
90
122
 
91
- else
92
- warn "Ignoring element '#{action.name}' in operation '#{operation.attributes['name']}' for portType '#{element.attributes['name']}'"
123
+ else
124
+ warn "Ignoring element '#{action.name}' in operation '#{operation.attributes['name']}' for portType '#{element.attributes['name']}'"
93
125
  end
94
126
  end
95
127
  else
@@ -103,4 +135,4 @@ module WSDL
103
135
  end
104
136
  end
105
137
  end
106
- end
138
+ end
@@ -1,5 +1,5 @@
1
1
  module Wsdl
2
2
  module Reader
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -12,25 +12,25 @@ describe "WSDL Binding" do
12
12
  it "#get_operations with binding UserServicePortBinding" do
13
13
  operations = subject.get_operations "UserServicePortBinding"
14
14
  operations.should have(2).operation
15
- operations.should eq %w{getFirstName getLastName}
15
+ operations.should eq %w{getFirstNameOperation getLastNameOperation}
16
16
  end
17
17
 
18
18
  it "#get_binding_for_operation_name with binding = UserServicePortBinding and operation = getFirstName" do
19
- bindings_array = subject.get_binding_for_operation_name "UserServicePortBinding", "getFirstName"
19
+ bindings_array = subject.get_binding_for_operation_name "UserServicePortBinding", "getFirstNameOperation"
20
20
 
21
21
  bindings_array.should have(1).binding
22
22
  bindings_array.first.should be_a WSDL::Reader::Binding
23
23
  bindings_array.first.name.should eq "UserServicePortBinding"
24
- bindings_array.first.operations.keys.should include "getFirstName"
24
+ bindings_array.first.operations.keys.should include "getFirstNameOperation"
25
25
  end
26
26
 
27
27
  it "#get_binding_for_operation_name operation = getFirstName but without binding" do
28
- bindings_array = subject.get_binding_for_operation_name "getFirstName"
28
+ bindings_array = subject.get_binding_for_operation_name "getFirstNameOperation"
29
29
 
30
- bindings_array.should have(1).binding
30
+ bindings_array.should have(2).binding
31
31
  bindings_array.first.should be_a WSDL::Reader::Binding
32
32
  bindings_array.first.name.should eq "UserServicePortBinding"
33
- bindings_array.first.operations.keys.should include "getFirstName"
33
+ bindings_array.first.operations.keys.should include "getFirstNameOperation"
34
34
  end
35
35
 
36
36
  it "#all_operations should eql to #get_operations(nil)" do
@@ -38,7 +38,7 @@ describe "WSDL Binding" do
38
38
  end
39
39
 
40
40
  it "#operation? should return true if any binding include is operation_name" do
41
- subject.operation?("getFirstName").should be_true
41
+ subject.operation?("getFirstNameOperation").should be_true
42
42
  end
43
43
 
44
44
  it "#operation? should return false if all bindings dont include is operation_name" do
@@ -49,27 +49,27 @@ describe "WSDL Binding" do
49
49
  context "WSDL::Reader::Binding" do
50
50
 
51
51
  def operations
52
- { "getFirstName" => { :name => "getFirstName",
53
- :soapAction => "",
54
- :input => { :body => { :use => "literal" } },
55
- :output => { :body => { :use => "literal" } } },
56
-
57
- "getLastName" => { :name => "getLastName",
58
- :soapAction => "",
59
- :input => { :body => { :use => "literal" } },
60
- :output => { :body => { :use => "literal" } } }
61
- }
52
+ %w{getFirstNameOperation getLastNameOperation}
62
53
  end
63
54
 
55
+ let(:parser) { WSDL::Reader::Parser.new('spec/fixtures/UserService.wsdl') }
64
56
  subject do
65
- WSDL::Reader::Parser.new('spec/fixtures/UserService.wsdl').bindings.values.first
57
+ parser.bindings.values.first
66
58
  end
67
59
 
68
- its(:operations) { should eq operations }
60
+ its('operations.keys') { should eq operations }
69
61
  its(:name) { should eq "UserServicePortBinding" }
70
62
  its(:type) { should eq "tns:UserService" }
71
63
  its(:style) { should eq "document" }
72
64
  its(:transport) { should eq "http://schemas.xmlsoap.org/soap/http" }
65
+
66
+ it "#operation? should find for operation name" do
67
+ subject.operation?("getFirstNameOperation").should be_true
68
+ end
69
+
70
+ it "#lookup_port_type should lookup port_type from given port_types" do
71
+ subject.lookup_port_type(parser.port_types).name.should eq "UserService"
72
+ end
73
73
  end
74
74
 
75
- end
75
+ end