diff --git a/lib/easy/api/block_wrapper.rb b/lib/easy/api/block_wrapper.rb
index 5bdf451..676a24c 100644
--- a/lib/easy/api/block_wrapper.rb
+++ b/lib/easy/api/block_wrapper.rb
@@ -25,10 +25,16 @@ def initialize(controller)
# use the controller to render the response
def render_result(render_params)
format = (render_params[:format] || 'json').try(:to_sym)
+ formatted_result = if format == :xml
+ @result.to_xml(render_params[:options] || {})
+ else
+ @result
+ end
+
if render_params[:callback].blank?
- @controller.render(format => @result, :status => @result.status_code)
+ @controller.render(format => formatted_result, :status => @result.status_code)
else
- @controller.render(format => @result, :status => @result.status_code, :callback => render_params[:callback], :content_type => 'application/javascript')
+ @controller.render(format => formatted_result, :status => @result.status_code, :callback => render_params[:callback], :content_type => 'application/javascript')
end
end
diff --git a/lib/easy/api/result.rb b/lib/easy/api/result.rb
index 4acdf48..6b35919 100644
--- a/lib/easy/api/result.rb
+++ b/lib/easy/api/result.rb
@@ -1,4 +1,5 @@
require 'ostruct'
+require "active_support/core_ext/hash/conversions"
module Easy::Api
# Encapsulates the response data of an API call
@@ -42,6 +43,9 @@ def as_json(options={})
# Will always contain 'success', the error if there is one, and any dynamic attributes.
# @return [Hash]
def to_xml(options={})
+ options = options.dup
+ options[:root] ||= 'response'
+ options[:skip_types] ||= true
convert_to_hash.to_xml(options)
end
diff --git a/spec/lib/easy/api/customers_controller_spec.rb b/spec/lib/easy/api/customers_controller_spec.rb
index 86ea40d..7c35d29 100644
--- a/spec/lib/easy/api/customers_controller_spec.rb
+++ b/spec/lib/easy/api/customers_controller_spec.rb
@@ -28,7 +28,7 @@
it "gets the index in xml format" do
get :index, :format => 'xml'
- expect(response.body).to eql("\n\n \n \n fred\n 19\n \n \n jackie\n 21\n \n \n true\n\n")
+ expect(response.body).to eql("\n\n \n \n fred\n 19\n \n \n jackie\n 21\n \n \n true\n\n")
end
end
@@ -47,7 +47,7 @@
it "gets show in xml format" do
get :show, :format => 'xml', id: 1
- expect(response.body).to eql("\n\n \n fred\n 21\n \n true\n\n")
+ expect(response.body).to eql("\n\n \n fred\n 21\n \n true\n\n")
end
end
diff --git a/spec/lib/easy/api/result_spec.rb b/spec/lib/easy/api/result_spec.rb
index 14c05c3..7fefa01 100644
--- a/spec/lib/easy/api/result_spec.rb
+++ b/spec/lib/easy/api/result_spec.rb
@@ -93,4 +93,38 @@
end
end
end
+
+ describe "#to_xml" do
+
+ let(:result) { Easy::Api::Result.new }
+ subject { result.to_xml }
+
+ context "when result is unsuccessful" do
+ let(:api_error) { Easy::Api::Error.new(:unauthorized) }
+
+ before do
+ result.error = api_error
+ end
+
+ it "renders the object as " do
+ expect(subject).to eql("\n\n false\n \n 401\n Unauthorized request\n \n\n")
+ end
+
+ end
+
+ context "when result is successful" do
+
+ before do
+ result.success = true
+ result.customer = "Bob Loblaw"
+ end
+
+ it "renders the object as " do
+ expect(subject).to eql("\n\n Bob Loblaw\n true\n\n")
+ end
+
+ end
+
+ end
+
end