Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interface for savon-multipart for Savon2 #434

Merged
merged 4 commits into from
May 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### master

* Feature: [#424](https://github.com/savonrb/savon/issues/424) After releasing Savon version 2, support for multipart SOAP messages was dropped. You can now specify `:multipart => true` for either your client constructor or `#call` method to indicate that you would like to use savon-multipart.

* Feature: [#416](https://github.com/savonrb/savon/pull/416) The global `namespace_identifier`
option can now be set to `nil` to not add a namespace identifier to the message tag.

Expand Down
22 changes: 21 additions & 1 deletion lib/savon/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,31 @@ def call(locals = {}, &block)

raise_expected_httpi_response! unless response.kind_of?(HTTPI::Response)

Response.new(response, @globals, @locals)
create_response(response)
end

private

def create_response(response)
if multipart_supported?
Multipart::Response.new(response, @globals, @locals)
else
Response.new(response, @globals, @locals)
end
end

def multipart_supported?
return false unless @globals[:multipart] || @locals[:multipart]

if Savon.const_defined?(:Multipart) && Savon::Multipart.const_defined?(:Response)
return true
else
raise RuntimeError.new('Could not find Savon::Multipart. Perhaps you did not require multipart-savon?')
end

return false
end

def set_locals(locals, block)
locals = LocalOptions.new(locals)
BlockInterface.new(locals).evaluate(block) if block
Expand Down
15 changes: 13 additions & 2 deletions lib/savon/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def initialize(options = {})
:pretty_print_xml => false,
:raise_errors => true,
:strip_namespaces => true,
:convert_response_tags_to => lambda { |tag| tag.snakecase.to_sym }
:convert_response_tags_to => lambda { |tag| tag.snakecase.to_sym},
:multipart => false,
}

options = defaults.merge(options)
Expand Down Expand Up @@ -246,6 +247,10 @@ def convert_request_keys_to(converter)
def convert_response_tags_to(converter = nil, &block)
@options[:convert_response_tags_to] = block || converter
end

def multipart(is_multipart)
@options[:multipart] = is_multipart
end
end

class LocalOptions < Options
Expand All @@ -255,7 +260,8 @@ def initialize(options = {})

defaults = {
:advanced_typecasting => true,
:response_parser => :nokogiri
:response_parser => :nokogiri,
:multipart => false
}

super defaults.merge(options)
Expand Down Expand Up @@ -302,5 +308,10 @@ def response_parser(parser)
@options[:response_parser] = parser
end

# Instruction Savon to create a Savon::Multipart::Response if available
def multipart(bool)
@options[:multipart] = bool
end

end
end
49 changes: 48 additions & 1 deletion spec/savon/operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,58 @@ def new_operation(operation_name, wsdl, globals)
actual_soap_action = inspect_request(response).soap_action
expect(actual_soap_action).to eq(%("authenticate"))
end

it "returns a Savon::Multipart::Response if available and requested globally" do
begin
module Savon::Multipart
class Response
def initialize(*args); end
end
end

globals_with_multipart = Savon::GlobalOptions.new(:multipart => true)
operation = new_operation(:authenticate, no_wsdl, globals_with_multipart)
response = operation.call
expect(response.is_a? Savon::Multipart::Response)
ensure
Savon.send(:remove_const, :Multipart) if Savon.const_defined?(:Multipart)
end
end

it "returns a Savon::Multipart::Response if available and requested locally" do
begin
module Savon::Multipart
class Response
def initialize(*args); end
end
end

operation = new_operation(:authenticate, no_wsdl, globals)
response = operation.call(:multipart => true)
expect(response.is_a? Savon::Multipart::Response)
ensure
Savon.send(:remove_const, :Multipart) if Savon.const_defined?(:Multipart)
end
end

it "raises a RuntimeError if savon-multipart is not available and it was requested globally" do
expect do
globals_with_multipart = Savon::GlobalOptions.new(:multipart => true)
operation = new_operation(:authenticate, no_wsdl, globals_with_multipart)
operation.call
end.to raise_error RuntimeError, /Could not find Savon::Multipart/
end

it "raises a RuntimeError if savon-multipart is not available and it was requested locally" do
expect do
operation = new_operation(:authenticate, no_wsdl, globals)
operation.call(:multipart => true)
end.to raise_error RuntimeError, /Could not find Savon::Multipart/
end
end

def inspect_request(response)
hash = JSON.parse(response.http.body)
OpenStruct.new(hash)
end

end