Building XML API with Grape
Asked Answered
C

2

6

I'm building a XML API with the help of the Grape gem. What is the best way to building XML for API actions? Since Grape isn't using standart rails controllers, i can't use views/../xml.builder. What did you suggest? Maybe there are some best practices for building xml api?

Calais answered 21/8, 2012 at 10:17 Comment(0)
C
2

Sinece there are no any answers, i will answer my own question. I ended up with modules where are i declare different kind of xml responses and include this modules into my Grape API. In API actions i just calling response methods from this modules. With this approach my code is clear and separated.

Calais answered 19/12, 2012 at 21:35 Comment(5)
Hi @Mikhail Nikalyukin I am running in a similar problem. And used same approach where in modules generate xml with builder. However when trying to return using Grape I get an cannot convert String to xml error. I make use of content_type :xml, "text/xml". Any suggestions? Thanks!Harriet
@Harriet It was a while ago and we used old grape version, it wasn't parsing our xml correctly to, i made a few changes in the grape. Take a look at my commits in the xml_formatter branches github.com/dpsk/grape/branches maybe it will help you.Calais
I have the same problem now and I'm using rabl to render xml but it's not as flexible as xml.builder by far... could you please explain your solution a little further?Hynes
@Hynes with which part you have troubles exactly?Calais
@Hynes take a look gist.github.com/dpsk/d426fbbadefbc74959c4, it's a simple example on how it can be done.Calais
P
0

It seems to be sample. Just provide format to tell grape your default format

format :xml
content_type :xml, "text/xml"

Then add .xml to the end of request url.

Postscript answered 17/2, 2013 at 3:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.