0

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.

Florian Eck
  • 495
  • 3
  • 13
  • GetShopProductsRequestRepresenter.new(GetShopProductsRequest.new(filter_value => 123, :filter_name => "test")) contains a syntax error at filter_value, needs to be :filter_value, is this causing your problem ? – Laurens Dec 21 '15 at 09:37
  • thanks for the info, but that not the problem, this is just a typo here. i updated it – Florian Eck Dec 21 '15 at 09:58

1 Answers1

0

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
Laurens
  • 2,420
  • 22
  • 39
  • Thanks for the response, but the problem is not to construct the XML, even though your suggested solution is nicer than what i did (i used Nokogiri before). The problem is, that calling `.to_xml` for the representer is escaping the XML string. I want to avoid that. – Florian Eck Dec 21 '15 at 10:32
  • sorry, but its not... it produces the same output as with a raw string, or nokogiri – Florian Eck Dec 21 '15 at 11:24
  • I think you forgot to 'include Roar::XML' in your openstruct, read: https://github.com/apotonick/roar#xml – Laurens Dec 21 '15 at 11:29
  • you mean in the `GetShopProductsRequest` class? – Florian Eck Dec 21 '15 at 11:31
  • `include Roar::XML` in the `GetShopProductsRequest < OpenStruct` does not change anything – Florian Eck Dec 21 '15 at 11:34
  • Are you sure you followed all instructions listed at https://github.com/apotonick/roar#xml, seems like you don't extend your object with a representer in your code ? – Laurens Dec 22 '15 at 08:38