Search code examples
ruby-on-rails-3prestashopactiveresource

Non rails style format of XML response how to parse with ActiveResource correctly?


Using Rails and ActiveResource i am getting non-rails style of XML response form third-party API. Object which i like to map is basically wrapped in prestahop element. What should i override to get rid of that element to map the object correctly?

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<order>
    <id></id> 

Solution

  • You should use custom formatter for your needs, something like this may be good solution.

    class PrestaXMLFormatter
      include ActiveResource::Formats::XmlFormat
    
      def decode(xml)
        ActiveResource::Formats::XmlFormat.decode(xml)['prestashop']
      end
    end
    
    class Order < ActiveResource::Base
      self.format = PrestaXMLFormatter.new
    end