regenersis-savon 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.rspec +1 -0
- data/.travis.yml +12 -0
- data/CHANGELOG.md +639 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +42 -0
- data/Rakefile +7 -0
- data/lib/regenersis-savon.rb +1 -0
- data/lib/savon.rb +15 -0
- data/lib/savon/client.rb +168 -0
- data/lib/savon/core_ext/object.rb +14 -0
- data/lib/savon/core_ext/string.rb +23 -0
- data/lib/savon/error.rb +6 -0
- data/lib/savon/global.rb +115 -0
- data/lib/savon/hooks/group.rb +46 -0
- data/lib/savon/hooks/hook.rb +36 -0
- data/lib/savon/http/error.rb +42 -0
- data/lib/savon/model.rb +103 -0
- data/lib/savon/soap.rb +21 -0
- data/lib/savon/soap/fault.rb +59 -0
- data/lib/savon/soap/request.rb +71 -0
- data/lib/savon/soap/response.rb +109 -0
- data/lib/savon/soap/xml.rb +227 -0
- data/lib/savon/version.rb +5 -0
- data/lib/savon/wasabi/document.rb +41 -0
- data/regenersis-savon.gemspec +35 -0
- data/spec/fixtures/gzip/message.gz +0 -0
- data/spec/fixtures/response/another_soap_fault.xml +14 -0
- data/spec/fixtures/response/authentication.xml +14 -0
- data/spec/fixtures/response/header.xml +13 -0
- data/spec/fixtures/response/list.xml +18 -0
- data/spec/fixtures/response/multi_ref.xml +39 -0
- data/spec/fixtures/response/soap_fault.xml +8 -0
- data/spec/fixtures/response/soap_fault12.xml +18 -0
- data/spec/fixtures/response/taxcloud.xml +1 -0
- data/spec/fixtures/wsdl/authentication.xml +63 -0
- data/spec/fixtures/wsdl/lower_camel.xml +52 -0
- data/spec/fixtures/wsdl/multiple_namespaces.xml +61 -0
- data/spec/fixtures/wsdl/multiple_types.xml +60 -0
- data/spec/fixtures/wsdl/taxcloud.xml +934 -0
- data/spec/savon/client_spec.rb +461 -0
- data/spec/savon/core_ext/object_spec.rb +19 -0
- data/spec/savon/core_ext/string_spec.rb +37 -0
- data/spec/savon/http/error_spec.rb +52 -0
- data/spec/savon/model_spec.rb +194 -0
- data/spec/savon/savon_spec.rb +85 -0
- data/spec/savon/soap/fault_spec.rb +89 -0
- data/spec/savon/soap/request_spec.rb +57 -0
- data/spec/savon/soap/response_spec.rb +224 -0
- data/spec/savon/soap/xml_spec.rb +309 -0
- data/spec/savon/soap_spec.rb +16 -0
- data/spec/savon/wasabi/document_spec.rb +45 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/endpoint.rb +25 -0
- data/spec/support/fixture.rb +35 -0
- metadata +323 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
module Savon
|
2
|
+
module Hooks
|
3
|
+
|
4
|
+
# = Savon::Hooks::Hook
|
5
|
+
#
|
6
|
+
# A hook used somewhere in the system.
|
7
|
+
class Hook
|
8
|
+
|
9
|
+
HOOKS = [
|
10
|
+
|
11
|
+
# Replaces the POST request executed to call a service.
|
12
|
+
# See: Savon::SOAP::Request#response
|
13
|
+
#
|
14
|
+
# Receives the <tt>Savon::SOAP::Request</tt> and is expected to return an <tt>HTTPI::Response</tt>.
|
15
|
+
# It can change the request and return something falsy to still execute the POST request.
|
16
|
+
:soap_request
|
17
|
+
|
18
|
+
]
|
19
|
+
|
20
|
+
# Expects an +id+, the name of the +hook+ to use and a +block+ to be called.
|
21
|
+
def initialize(id, hook, &block)
|
22
|
+
self.id = id
|
23
|
+
self.hook = hook
|
24
|
+
self.block = block
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_accessor :id, :hook, :block
|
28
|
+
|
29
|
+
# Calls the +block+ with the given +args+.
|
30
|
+
def call(*args)
|
31
|
+
block.call(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "savon/error"
|
2
|
+
require "savon/soap/xml"
|
3
|
+
|
4
|
+
module Savon
|
5
|
+
module HTTP
|
6
|
+
|
7
|
+
# = Savon::HTTP::Error
|
8
|
+
#
|
9
|
+
# Represents an HTTP error. Contains the original <tt>HTTPI::Response</tt>.
|
10
|
+
class Error < Error
|
11
|
+
|
12
|
+
# Expects an <tt>HTTPI::Response</tt>.
|
13
|
+
def initialize(http)
|
14
|
+
self.http = http
|
15
|
+
end
|
16
|
+
|
17
|
+
# Accessor for the <tt>HTTPI::Response</tt>.
|
18
|
+
attr_accessor :http
|
19
|
+
|
20
|
+
# Returns whether an HTTP error is present.
|
21
|
+
def present?
|
22
|
+
http.error?
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the HTTP error message.
|
26
|
+
def to_s
|
27
|
+
return "" unless present?
|
28
|
+
|
29
|
+
@message ||= begin
|
30
|
+
message = "HTTP error (#{http.code})"
|
31
|
+
message << ": #{http.body}" unless http.body.empty?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Returns the HTTP response as a Hash.
|
36
|
+
def to_hash
|
37
|
+
@hash = { :code => http.code, :headers => http.headers, :body => http.body }
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/savon/model.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
module Savon
|
2
|
+
|
3
|
+
# = Savon::Model
|
4
|
+
#
|
5
|
+
# Model for SOAP service oriented applications.
|
6
|
+
module Model
|
7
|
+
|
8
|
+
def self.extended(base)
|
9
|
+
base.setup
|
10
|
+
end
|
11
|
+
|
12
|
+
def setup
|
13
|
+
class_action_module
|
14
|
+
instance_action_module
|
15
|
+
end
|
16
|
+
|
17
|
+
# Accepts one or more SOAP actions and generates both class and instance methods named
|
18
|
+
# after the given actions. Each generated method accepts an optional SOAP body Hash and
|
19
|
+
# a block to be passed to <tt>Savon::Client#request</tt> and executes a SOAP request.
|
20
|
+
def actions(*actions)
|
21
|
+
actions.each do |action|
|
22
|
+
define_class_action(action)
|
23
|
+
define_instance_action(action)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# Defines a class-level SOAP action method.
|
30
|
+
def define_class_action(action)
|
31
|
+
class_action_module.module_eval %{
|
32
|
+
def #{action.to_s.snakecase}(body = nil, &block)
|
33
|
+
response = client.request :wsdl, #{action.inspect}, :body => body, &block
|
34
|
+
Savon.hooks.select(:model_soap_response).call(response) || response
|
35
|
+
end
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
# Defines an instance-level SOAP action method.
|
40
|
+
def define_instance_action(action)
|
41
|
+
instance_action_module.module_eval %{
|
42
|
+
def #{action.to_s.snakecase}(body = nil, &block)
|
43
|
+
self.class.#{action.to_s.snakecase} body, &block
|
44
|
+
end
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
# Class methods.
|
49
|
+
def class_action_module
|
50
|
+
@class_action_module ||= Module.new do
|
51
|
+
|
52
|
+
# Returns the memoized <tt>Savon::Client</tt>.
|
53
|
+
def client(&block)
|
54
|
+
@client ||= Savon::Client.new(&block)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Sets the SOAP endpoint to the given +uri+.
|
58
|
+
def endpoint(uri)
|
59
|
+
client.wsdl.endpoint = uri
|
60
|
+
end
|
61
|
+
|
62
|
+
# Sets the target namespace.
|
63
|
+
def namespace(uri)
|
64
|
+
client.wsdl.namespace = uri
|
65
|
+
end
|
66
|
+
|
67
|
+
# Sets the WSDL document to the given +uri+.
|
68
|
+
def document(uri)
|
69
|
+
client.wsdl.document = uri
|
70
|
+
end
|
71
|
+
|
72
|
+
# Sets the HTTP headers.
|
73
|
+
def headers(headers)
|
74
|
+
client.http.headers = headers
|
75
|
+
end
|
76
|
+
|
77
|
+
# Sets basic auth +login+ and +password+.
|
78
|
+
def basic_auth(login, password)
|
79
|
+
client.http.auth.basic(login, password)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Sets WSSE auth credentials.
|
83
|
+
def wsse_auth(*args)
|
84
|
+
client.wsse.credentials(*args)
|
85
|
+
end
|
86
|
+
|
87
|
+
end.tap { |mod| extend(mod) }
|
88
|
+
end
|
89
|
+
|
90
|
+
# Instance methods.
|
91
|
+
def instance_action_module
|
92
|
+
@instance_action_module ||= Module.new do
|
93
|
+
|
94
|
+
# Returns the <tt>Savon::Client</tt> from the class instance.
|
95
|
+
def client(&block)
|
96
|
+
self.class.client(&block)
|
97
|
+
end
|
98
|
+
|
99
|
+
end.tap { |mod| include(mod) }
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
data/lib/savon/soap.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Savon
|
2
|
+
|
3
|
+
# = Savon::SOAP
|
4
|
+
#
|
5
|
+
# Contains various SOAP details.
|
6
|
+
module SOAP
|
7
|
+
|
8
|
+
# Default SOAP version.
|
9
|
+
DefaultVersion = 1
|
10
|
+
|
11
|
+
# Supported SOAP versions.
|
12
|
+
Versions = 1..2
|
13
|
+
|
14
|
+
# SOAP namespaces by SOAP version.
|
15
|
+
Namespace = {
|
16
|
+
1 => "http://schemas.xmlsoap.org/soap/envelope/",
|
17
|
+
2 => "http://www.w3.org/2003/05/soap-envelope"
|
18
|
+
}
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "savon/error"
|
2
|
+
require "savon/soap/xml"
|
3
|
+
|
4
|
+
module Savon
|
5
|
+
module SOAP
|
6
|
+
|
7
|
+
# = Savon::SOAP::Fault
|
8
|
+
#
|
9
|
+
# Represents a SOAP fault. Contains the original <tt>HTTPI::Response</tt>.
|
10
|
+
class Fault < Error
|
11
|
+
|
12
|
+
# Expects an <tt>HTTPI::Response</tt>.
|
13
|
+
def initialize(http)
|
14
|
+
self.http = http
|
15
|
+
end
|
16
|
+
|
17
|
+
# Accessor for the <tt>HTTPI::Response</tt>.
|
18
|
+
attr_accessor :http
|
19
|
+
|
20
|
+
# Returns whether a SOAP fault is present.
|
21
|
+
def present?
|
22
|
+
@present ||= http.body.include?("Fault>") && (soap1_fault? || soap2_fault?)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns the SOAP fault message.
|
26
|
+
def to_s
|
27
|
+
return "" unless present?
|
28
|
+
@message ||= message_by_version to_hash[:fault]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the SOAP response body as a Hash.
|
32
|
+
def to_hash
|
33
|
+
@hash ||= Nori.parse(http.body)[:envelope][:body]
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# Returns whether the response contains a SOAP 1.1 fault.
|
39
|
+
def soap1_fault?
|
40
|
+
http.body.include?("faultcode>") && http.body.include?("faultstring>")
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns whether the response contains a SOAP 1.2 fault.
|
44
|
+
def soap2_fault?
|
45
|
+
http.body.include?("Code>") && http.body.include?("Reason>")
|
46
|
+
end
|
47
|
+
|
48
|
+
# Returns the SOAP fault message by version.
|
49
|
+
def message_by_version(fault)
|
50
|
+
if fault[:faultcode]
|
51
|
+
"(#{fault[:faultcode]}) #{fault[:faultstring]}"
|
52
|
+
elsif fault[:code]
|
53
|
+
"(#{fault[:code][:value]}) #{fault[:reason][:text]}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "httpi"
|
2
|
+
require "savon/soap/response"
|
3
|
+
|
4
|
+
module Savon
|
5
|
+
module SOAP
|
6
|
+
|
7
|
+
# = Savon::SOAP::Request
|
8
|
+
#
|
9
|
+
# Executes SOAP requests.
|
10
|
+
class Request
|
11
|
+
|
12
|
+
# Content-Types by SOAP version.
|
13
|
+
ContentType = { 1 => "text/xml;charset=UTF-8", 2 => "application/soap+xml;charset=UTF-8" }
|
14
|
+
|
15
|
+
# Expects an <tt>HTTPI::Request</tt> and a <tt>Savon::SOAP::XML</tt> object
|
16
|
+
# to execute a SOAP request and returns the response.
|
17
|
+
def self.execute(http, soap)
|
18
|
+
new(http, soap).response
|
19
|
+
end
|
20
|
+
|
21
|
+
# Expects an <tt>HTTPI::Request</tt> and a <tt>Savon::SOAP::XML</tt> object.
|
22
|
+
def initialize(http, soap)
|
23
|
+
self.soap = soap
|
24
|
+
self.http = configure(http)
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_accessor :soap, :http
|
28
|
+
|
29
|
+
# Executes the request and returns the response.
|
30
|
+
def response
|
31
|
+
@response ||= SOAP::Response.new(
|
32
|
+
Savon.hooks.select(:soap_request).call(self) || with_logging { HTTPI.post(http) }
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# Configures a given +http+ from the +soap+ object.
|
39
|
+
def configure(http)
|
40
|
+
http.url = soap.endpoint
|
41
|
+
http.body = soap.to_xml
|
42
|
+
http.headers["Content-Type"] ||= ContentType[soap.version]
|
43
|
+
http.headers["Content-Length"] ||= soap.to_xml.length.to_s
|
44
|
+
|
45
|
+
http
|
46
|
+
end
|
47
|
+
|
48
|
+
# Logs the HTTP request, yields to a given +block+ and returns a <tt>Savon::SOAP::Response</tt>.
|
49
|
+
def with_logging
|
50
|
+
log_request http.url, http.headers, http.body
|
51
|
+
response = yield
|
52
|
+
log_response response.code, response.body
|
53
|
+
response
|
54
|
+
end
|
55
|
+
|
56
|
+
# Logs the SOAP request +url+, +headers+ and +body+.
|
57
|
+
def log_request(url, headers, body)
|
58
|
+
Savon.log "SOAP request: #{url}"
|
59
|
+
Savon.log headers.map { |key, value| "#{key}: #{value}" }.join(", ")
|
60
|
+
Savon.log body
|
61
|
+
end
|
62
|
+
|
63
|
+
# Logs the SOAP response +code+ and +body+.
|
64
|
+
def log_response(code, body)
|
65
|
+
Savon.log "SOAP response (status #{code}):"
|
66
|
+
Savon.log body
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require "savon/soap/xml"
|
2
|
+
require "savon/soap/fault"
|
3
|
+
require "savon/http/error"
|
4
|
+
|
5
|
+
module Savon
|
6
|
+
module SOAP
|
7
|
+
|
8
|
+
# = Savon::SOAP::Response
|
9
|
+
#
|
10
|
+
# Represents the SOAP response and contains the HTTP response.
|
11
|
+
class Response
|
12
|
+
|
13
|
+
# Expects an <tt>HTTPI::Response</tt> and handles errors.
|
14
|
+
def initialize(response)
|
15
|
+
self.http = response
|
16
|
+
raise_errors if Savon.raise_errors?
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_accessor :http
|
20
|
+
|
21
|
+
# Returns whether the request was successful.
|
22
|
+
def success?
|
23
|
+
!soap_fault? && !http_error?
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns whether there was a SOAP fault.
|
27
|
+
def soap_fault?
|
28
|
+
soap_fault.present?
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the <tt>Savon::SOAP::Fault</tt>.
|
32
|
+
def soap_fault
|
33
|
+
@soap_fault ||= Fault.new http
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns whether there was an HTTP error.
|
37
|
+
def http_error?
|
38
|
+
http_error.present?
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns the <tt>Savon::HTTP::Error</tt>.
|
42
|
+
def http_error
|
43
|
+
@http_error ||= HTTP::Error.new http
|
44
|
+
end
|
45
|
+
|
46
|
+
# Shortcut accessor for the SOAP response body Hash.
|
47
|
+
def [](key)
|
48
|
+
body[key]
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns the SOAP response header as a Hash.
|
52
|
+
def header
|
53
|
+
hash[:envelope][:header]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the SOAP response body as a Hash.
|
57
|
+
def body
|
58
|
+
hash[:envelope][:body]
|
59
|
+
end
|
60
|
+
|
61
|
+
alias to_hash body
|
62
|
+
|
63
|
+
# Traverses the SOAP response body Hash for a given +path+ of Hash keys and returns
|
64
|
+
# the value as an Array. Defaults to return an empty Array in case the path does not
|
65
|
+
# exist or returns nil.
|
66
|
+
def to_array(*path)
|
67
|
+
result = path.inject body do |memo, key|
|
68
|
+
return [] unless memo[key]
|
69
|
+
memo[key]
|
70
|
+
end
|
71
|
+
|
72
|
+
result.kind_of?(Array) ? result.compact : [result].compact
|
73
|
+
end
|
74
|
+
|
75
|
+
# Returns the complete SOAP response XML without normalization.
|
76
|
+
def hash
|
77
|
+
@hash ||= Nori.parse(to_xml)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Returns the SOAP response XML.
|
81
|
+
def to_xml
|
82
|
+
http.body
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns a <tt>Nokogiri::XML::Document</tt> for the SOAP response XML.
|
86
|
+
def doc
|
87
|
+
@doc ||= Nokogiri::XML(to_xml)
|
88
|
+
end
|
89
|
+
|
90
|
+
# Returns an Array of <tt>Nokogiri::XML::Node</tt> objects retrieved with the given +path+.
|
91
|
+
# Automatically adds all of the document's namespaces unless a +namespaces+ hash is provided.
|
92
|
+
def xpath(path, namespaces = nil)
|
93
|
+
doc.xpath(path, namespaces || xml_namespaces)
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def raise_errors
|
99
|
+
raise soap_fault if soap_fault?
|
100
|
+
raise http_error if http_error?
|
101
|
+
end
|
102
|
+
|
103
|
+
def xml_namespaces
|
104
|
+
@xml_namespaces ||= doc.collect_namespaces
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|