How to render plain XML properties with Roar::Decorator

111 views Asked by At

I am trying to render a plain XML output within a Roar::Decorator. For some reason it is not possible to get unescaped output.

I have this class:

class GetShopProductsRequest < OpenStruct

  def data_filter
    xml_s = []
    xml_s << "<Filter>"
    xml_s << "  <FilterName>#{self.filter_name}</FilterName>"
    xml_s << "  <FilterValues>"
    xml_s << "    <FilterValue>#{self.filter_value}</FilterValue>"
    xml_s << "  </FilterValues>"
    xml_s << "</Filter>"

    xml_s.join("\n")
  end

end

and the following representer:

class GetShopProductsRequestRepresenter < RequestRepresenter

  property :data_filter, as: :DataFilter

end

which inherits from Roar::Decorator and includes Roar::XML

However, when I create my representer instance with

GetShopProductsRequestRepresenter.new(GetShopProductsRequest.new(:filter_value => 123, :filter_name => "test"))

and create the XML Output .to_xml the output is

<DataFilter>&lt;Filter&gt;
       &lt;FilterName&gt;bla&lt;/FilterName&gt;
       &lt;FilterValues&gt;
         &lt;FilterValue&gt;test&lt;/FilterValue&gt;
       &lt;/FilterValues&gt;
     &lt;/Filter&gt;</DataFilter>

I tried to use html_safe at serveral spots in the code but nothing changed the result.

1

There are 1 answers

6
Laurens On

I would suggest using something like https://github.com/jimweirich/builder

This allows you to do:

require 'builder'

def filter_xml
  xml = Builder::XmlMarkup.new( :indent => 2 )
  xml.instruct! :xml, :encoding => "ASCII"
  xml.Filter do
    xml.FilterName do
      self.filter_name                             
    end
  end
end

puts filter_xml