Parsing Grape params in an XML request with complex nested body including attributes
While trying to build an Grape-based API with complex XML body requests in the requests, I came to the point of figuring out how to reach XML attributes in a sample request like this.
<Travelers>
<Traveler>
<AnonymousTraveler>
<PTC Quantity="1">ADT</PTC>
</AnonymousTraveler>
</Traveler>
</Travelers>
XML attributes when using Nokogiri are parsed as normal child elements, and the content (or text) of the element itself is parsed as an String named __content__
.
So Grape parameters block should look like this in order to parse properly params.
optional :Travelers, type: Array do
requires :Traveler, type: Hash do
optional :AnonymousTraveler, type: Hash do
requires :PTC, type: Hash do
requires :Quantity, type: Integer
requires :__content__, type: String
end
end
end
end
I hope it helps.
Written by Jorge Díaz
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#