Skip to content
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
10 changes: 8 additions & 2 deletions lib/easy/api/block_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/easy/api/result.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'ostruct'
require "active_support/core_ext/hash/conversions"

module Easy::Api
# Encapsulates the response data of an API call
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spec/lib/easy/api/customers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

it "gets the index in xml format" do
get :index, :format => 'xml'
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <customers type=\"array\">\n <customer>\n <name>fred</name>\n <age>19</age>\n </customer>\n <customer>\n <name>jackie</name>\n <age>21</age>\n </customer>\n </customers>\n <success type=\"boolean\">true</success>\n</hash>\n")
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <customers>\n <customer>\n <name>fred</name>\n <age>19</age>\n </customer>\n <customer>\n <name>jackie</name>\n <age>21</age>\n </customer>\n </customers>\n <success>true</success>\n</response>\n")
end

end
Expand All @@ -47,7 +47,7 @@

it "gets show in xml format" do
get :show, :format => 'xml', id: 1
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <customer>\n <name>fred</name>\n <age>21</age>\n </customer>\n <success type=\"boolean\">true</success>\n</hash>\n")
expect(response.body).to eql("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <customer>\n <name>fred</name>\n <age>21</age>\n </customer>\n <success>true</success>\n</response>\n")
end

end
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/easy/api/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <success>false</success>\n <error>\n <code>401</code>\n <message>Unauthorized request</message>\n </error>\n</response>\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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>\n <customer>Bob Loblaw</customer>\n <success>true</success>\n</response>\n")
end

end

end

end