Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: can't convert Builder::XmlMarkup to Array

Tags:

xml

ruby

builder

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.

like image 715
doremi Avatar asked Sep 18 '12 23:09

doremi


1 Answers

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

like image 134
Chrissy H Avatar answered Nov 15 '22 06:11

Chrissy H