I'm having issues with getting access to the raw xml from a Builder::XmlMarkup object.
irb> xml = Builder::XmlMarkup.new(:target => '')
=> <pretty_inspect/>
irb> xml.foo("bar")
=> "<pretty_inspect/><foo>bar</foo>"
irb> puts xml
TypeError: can't convert Builder::XmlMarkup to Array (Builder::XmlMarkup#to_ary gives String)
from (pry):122:in `puts'
In a script where I'm using Builder to create the XML, I'm passing @xml to a POST:
response = HTTParty.post(API_ENDPOINT, :body => @xml)
This gives the same error:
TypeError (can't convert Builder::XmlMarkup to Array (Builder::XmlMarkup#to_ary gives String)):
Of course, if I do @xml.to_xml, it doesn't return an error, but it adds </to_xml>
to the xml, meaning it isn't actually converting the XML object to xml. That's not what I want.
So how can I get access access to the xml so that I can pass it to my post without it adding extra nodes to my xml?
Edit: possible solution
Doing @xml.target!
seems to resolve the issue, but I'm not sure I understand why.
response = HTTParty.post(API_ENDPOINT, :body => @xml.target!)
Perhaps someone can help me understand what is happening here.
Using
puts xml
is outputting the Builder::XmlMarkup object and hence give the error
Using
puts xml.target!
outputs the current xml string, which is what you want
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With