Skip to content

Commit c406b1b

Browse files
committed
Merge pull request ruby-grape#61 from jwillis/master
Call #to_xml if the content type is xml
2 parents 83762d0 + a9a3153 commit c406b1b

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/grape/middleware/base.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ module Formats
5454
FORMATTERS = {
5555
:json => :encode_json,
5656
:txt => :encode_txt,
57+
:xml => :encode_xml
5758
}
5859
PARSERS = {
5960
:json => :decode_json
@@ -120,6 +121,10 @@ def encode_json(object)
120121
def encode_txt(object)
121122
object.respond_to?(:to_txt) ? object.to_txt : object.to_s
122123
end
124+
125+
def encode_xml(object)
126+
object.respond_to?(:to_xml) ? object.to_xml : object.to_s
127+
end
123128
end
124129

125130
end

spec/grape/middleware/formatter_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ def serializable_hash
3535

3636
subject.call({'PATH_INFO' => '/somewhere'}).last.each{|b| b.should == '{"abc":"def"}'}
3737
end
38+
39+
it 'should call #to_xml if the content type is xml' do
40+
@body = "string"
41+
@body.instance_eval do
42+
def to_xml
43+
"<bar/>"
44+
end
45+
end
46+
47+
subject.call({'PATH_INFO' => '/somewhere.xml'}).last.each{|b| b.should == '<bar/>'}
48+
end
3849
end
3950

4051
context 'detection' do

0 commit comments

Comments
 (0)