/
options.rb
275 lines (219 loc) · 7.32 KB
/
options.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
require "logger"
require "httpi"
module Savon
class Options
def initialize(options = {})
@options = {}
assign options
end
def [](option)
@options[option]
end
def []=(option, value)
value = [value].flatten
self.send(option, *value)
end
def include?(option)
@options.key? option
end
private
def assign(options)
options.each do |option, value|
self.send(option, value)
end
end
end
class GlobalOptions < Options
def initialize(options = {})
defaults = {
:encoding => "UTF-8",
:soap_version => 1,
:logger => Logger.new($stdout),
:filters => [],
:pretty_print_xml => false,
:raise_errors => true,
:strip_namespaces => true,
:convert_response_tags_to => lambda { |tag| tag.snakecase.to_sym }
}
options = defaults.merge(options)
# these options are shortcuts on the logger which needs to be set
# before it can be modified to set these options.
delayed_log = options.delete(:log)
delayed_level = options.delete(:log_level)
super(options)
log(delayed_log) unless delayed_log.nil?
log_level(delayed_level) unless delayed_level.nil?
end
# Location of the local or remote WSDL document.
def wsdl(wsdl_address)
@options[:wsdl] = wsdl_address
end
# SOAP endpoint.
def endpoint(endpoint)
@options[:endpoint] = endpoint
end
# Target namespace.
def namespace(namespace)
@options[:namespace] = namespace
end
# The namespace identifer.
def namespace_identifier(identifier)
@options[:namespace_identifier] = identifier
end
# Proxy server to use for all requests.
def proxy(proxy)
@options[:proxy] = proxy
end
# A Hash of HTTP headers.
def headers(headers)
@options[:headers] = headers
end
# Open timeout in seconds.
def open_timeout(open_timeout)
@options[:open_timeout] = open_timeout
end
# Read timeout in seconds.
def read_timeout(read_timeout)
@options[:read_timeout] = read_timeout
end
# The encoding to use. Defaults to "UTF-8".
def encoding(encoding)
@options[:encoding] = encoding
end
# The global SOAP header. Expected to be a Hash.
def soap_header(header)
@options[:soap_header] = header
end
# Sets whether elements should be :qualified or unqualified.
# If you need to use this option, please open an issue and make
# sure to add your WSDL document for debugging.
def element_form_default(element_form_default)
@options[:element_form_default] = element_form_default
end
# Can be used to change the SOAP envelope namespace identifier.
# If you need to use this option, please open an issue and make
# sure to add your WSDL document for debugging.
def env_namespace(env_namespace)
@options[:env_namespace] = env_namespace
end
# Changes the SOAP version to 1 or 2.
def soap_version(soap_version)
@options[:soap_version] = soap_version
end
# Whether or not to raise SOAP fault and HTTP errors.
def raise_errors(raise_errors)
@options[:raise_errors] = raise_errors
end
# Whether or not to log.
def log(log)
if log
HTTPI.log = true
target = $stdout
else
HTTPI.log = false
windows = RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i
target = windows ? "NUL:" : "/dev/null"
end
@options[:logger] = Logger.new(target)
end
# The logger to use. Defaults to a Savon::Logger instance.
def logger(logger)
@options[:logger] = logger
end
# Changes the Logger's log level.
def log_level(level)
levels = { :debug => 0, :info => 1, :warn => 2, :error => 3, :fatal => 4 }
unless levels.include? level
raise ArgumentError, "Invalid log level: #{level.inspect}\n" \
"Expected one of: #{levels.keys.inspect}"
end
@options[:logger].level = levels[level]
end
# A list of XML tags to filter from logged SOAP messages.
def filters(*filters)
@options[:filters] = filters.flatten
end
# Whether to pretty print request and response XML log messages.
def pretty_print_xml(pretty_print_xml)
@options[:pretty_print_xml] = pretty_print_xml
end
# Used by Savon to store the last response to pass
# its cookies to the next request.
def last_response(last_response)
@options[:last_response] = last_response
end
# Specifies the SSL version to use.
def ssl_version(version)
@options[:ssl_version] = version
end
# Whether and how to to verify the connection.
def ssl_verify_mode(verify_mode)
@options[:ssl_verify_mode] = verify_mode
end
# HTTP basic auth credentials.
def basic_auth(*credentials)
@options[:basic_auth] = credentials.flatten
end
# HTTP digest auth credentials.
def digest_auth(*credentials)
@options[:digest_auth] = credentials.flatten
end
# WSSE auth credentials for Akami.
def wsse_auth(*credentials)
@options[:wsse_auth] = credentials.flatten
end
# Instruct Akami to enable wsu:Timestamp headers.
def wsse_timestamp(*timestamp)
@options[:wsse_timestamp] = timestamp.flatten
end
# Instruct Nori whether to strip namespaces from XML nodes.
def strip_namespaces(strip_namespaces)
@options[:strip_namespaces] = strip_namespaces
end
# Tell Gyoku how to convert Hash key Symbols to XML tags.
# Accepts one of :lower_camelcase, :camelcase, :upcase, or :none.
def convert_request_keys_to(converter)
@options[:convert_request_keys_to] = converter
end
# Tell Nori how to convert XML tags from the SOAP response into Hash keys.
# Accepts a lambda or a block which receives an XML tag and returns a Hash key.
# Defaults to convert tags to snakecase Symbols.
def convert_response_tags_to(converter = nil, &block)
@options[:convert_response_tags_to] = block || converter
end
end
class LocalOptions < Options
def initialize(options = {})
defaults = {
:advanced_typecasting => true,
:response_parser => :nokogiri
}
super defaults.merge(options)
end
# The SOAP message to send. Expected to be a Hash or a String.
def message(message)
@options[:message] = message
end
# SOAP message tag (formerly known as SOAP input tag). If it's not set, Savon retrieves the name from
# the WSDL document (if available). Otherwise, Gyoku converts the operation name into an XML element.
def message_tag(message_tag)
@options[:message_tag] = message_tag
end
# Value of the SOAPAction HTTP header.
def soap_action(soap_action)
@options[:soap_action] = soap_action
end
# The SOAP request XML to send. Expected to be a String.
def xml(xml)
@options[:xml] = xml
end
# Instruct Nori to use advanced typecasting.
def advanced_typecasting(advanced)
@options[:advanced_typecasting] = advanced
end
# Instruct Nori to use :rexml or :nokogiri to parse the response.
def response_parser(parser)
@options[:response_parser] = parser
end
end
end