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?

dB.
  • 4,700
  • 2
  • 46
  • 51
Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70

2 Answers2

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.

Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70
  • 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! – PSR Oct 06 '15 at 12:26
  • @PSR 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 https://github.com/dpsk/grape/branches maybe it will help you. – Mikhail Nikalyukin Oct 06 '15 at 15:56
  • 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? – zapico Feb 02 '16 at 19:48
  • @zapico with which part you have troubles exactly? – Mikhail Nikalyukin Feb 03 '16 at 17:49
  • 1
    @zapico take a look https://gist.github.com/dpsk/d426fbbadefbc74959c4, it's a simple example on how it can be done. – Mikhail Nikalyukin Feb 03 '16 at 17:55
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.

big-circle
  • 547
  • 2
  • 11