-
Notifications
You must be signed in to change notification settings - Fork 617
/
envelope.rb
92 lines (69 loc) · 2.02 KB
/
envelope.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require 'builder'
require 'savon/message'
class Savon
class Envelope
NSID = 'lol'
def initialize(operation, header, body)
@logger = Logging.logger[self]
@operation = operation
@header = header || {}
@body = body || {}
@nsid_counter = -1
@namespaces = {}
end
def register_namespace(namespace)
@namespaces[namespace] ||= create_nsid
end
def to_s
build_envelope(build_header, build_body)
end
private
def create_nsid
@nsid_counter += 1
"#{NSID}#{@nsid_counter}"
end
def build_header
return "" if @header.empty?
Message.new(self, @operation.input.header_parts).build(@header)
end
def build_body
return "" if @body.empty?
body = Message.new(self, @operation.input.body_parts).build(@body)
if rpc_call?
build_rpc_wrapper(body)
else
body
end
end
def build_envelope(header, body)
builder = Builder::XmlMarkup.new(indent: 2)
builder.tag! :env, :Envelope, collect_namespaces do |xml|
xml.tag!(:env, :Header) { |xml| xml << header }
xml.tag!(:env, :Body) { |xml| xml << body }
end
builder.target!
end
def build_rpc_wrapper(body)
name = @operation.name
namespace = @operation.binding_operation.input_body[:namespace]
nsid = register_namespace(namespace) if namespace
tag = [nsid, name].compact.join(':')
'<%{tag}>%{body}</%{tag}>' % { tag: tag, body: body }
end
def rpc_call?
@operation.binding_operation.style == 'rpc'
end
def collect_namespaces
# registered namespaces
namespaces = @namespaces.each_with_object({}) { |(namespace, nsid), memo|
memo["xmlns:#{nsid}"] = namespace
}
# envelope namespace
namespaces['xmlns:env'] = case @operation.soap_version
when '1.1' then 'http://schemas.xmlsoap.org/soap/envelope/'
when '1.2' then 'http://www.w3.org/2003/05/soap-envelope'
end
namespaces
end
end
end