Skip to content

Commit c1caf2d

Browse files
Merge pull request #3 from AbleTech/feature/default_xml_options
Feature/default xml options
2 parents d842096 + 45d6ae4 commit c1caf2d

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

lib/easy/api/block_wrapper.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ def initialize(controller)
2525
# use the controller to render the response
2626
def render_result(render_params)
2727
format = (render_params[:format] || 'json').try(:to_sym)
28+
formatted_result = if format == :xml
29+
@result.to_xml(render_params[:options] || {})
30+
else
31+
@result
32+
end
33+
2834
if render_params[:callback].blank?
29-
@controller.render(format => @result, :status => @result.status_code)
35+
@controller.render(format => formatted_result, :status => @result.status_code)
3036
else
31-
@controller.render(format => @result, :status => @result.status_code, :callback => render_params[:callback], :content_type => 'application/javascript')
37+
@controller.render(format => formatted_result, :status => @result.status_code, :callback => render_params[:callback], :content_type => 'application/javascript')
3238
end
3339
end
3440

lib/easy/api/result.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'ostruct'
2+
require "active_support/core_ext/hash/conversions"
23

34
module Easy::Api
45
# Encapsulates the response data of an API call
@@ -42,6 +43,9 @@ def as_json(options={})
4243
# Will always contain 'success', the error if there is one, and any dynamic attributes.
4344
# @return [Hash]
4445
def to_xml(options={})
46+
options = options.dup
47+
options[:root] ||= 'response'
48+
options[:skip_types] ||= true
4549
convert_to_hash.to_xml(options)
4650
end
4751

spec/lib/easy/api/customers_controller_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
it "gets the index in xml format" do
3030
get :index, :format => 'xml'
31-
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")
31+
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")
3232
end
3333

3434
end
@@ -47,7 +47,7 @@
4747

4848
it "gets show in xml format" do
4949
get :show, :format => 'xml', id: 1
50-
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")
50+
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")
5151
end
5252

5353
end

spec/lib/easy/api/result_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,38 @@
9393
end
9494
end
9595
end
96+
97+
describe "#to_xml" do
98+
99+
let(:result) { Easy::Api::Result.new }
100+
subject { result.to_xml }
101+
102+
context "when result is unsuccessful" do
103+
let(:api_error) { Easy::Api::Error.new(:unauthorized) }
104+
105+
before do
106+
result.error = api_error
107+
end
108+
109+
it "renders the object as " do
110+
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")
111+
end
112+
113+
end
114+
115+
context "when result is successful" do
116+
117+
before do
118+
result.success = true
119+
result.customer = "Bob Loblaw"
120+
end
121+
122+
it "renders the object as " do
123+
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")
124+
end
125+
126+
end
127+
128+
end
129+
96130
end

0 commit comments

Comments
 (0)