Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't manage to parse XML collection properly #143

Open
mhuggins opened this issue Aug 9, 2015 · 2 comments
Open

Can't manage to parse XML collection properly #143

mhuggins opened this issue Aug 9, 2015 · 2 comments

Comments

@mhuggins
Copy link

mhuggins commented Aug 9, 2015

This is probably an issue on my end that I'm doing something wrong, but I can't seem to parse this XML collection properly. The XML looks like:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns="http://www.SupermarketAPI.com">
  <string>Apples</string>
  <string>Gerber 100% Apple Juice - 32 Fl. Oz.</string>
  <string>Gerber Apple Juice - 4-4 Fl. Oz.</string>
  <string>Gerber Organic Apple Juice - 4-4 Fl. Oz.</string>
  <string>Pedialyte Tetra/Brick Pk Apple Flavor - 4-6.8 Fl. Oz.</string>
</ArrayOfString>

My representer looks like:

require 'representable/decorator'
require 'representable/xml'
require 'representable/xml/collection'

class StringsSerializer < Representable::Decorator
  include Representable::XML::Collection

  self.representation_wrap = :ArrayOfString

  # TODO: this belongs in `Representable::XML::Collection`
  # REF: https://github.com/apotonick/representable/issues/119
  def self.remove_namespaces!
    representable_attrs.options[:remove_namespaces] = true
  end

  remove_namespaces!

  items wrap: :string
end

And I'm parsing via:

StringsSerializer.new([]).from_xml(raw_xml)

The result is a collection where each entry is the Nokogiri::XML::Element rather than the String representing the text child of each element. For example, my tests are failing with messages like:

object at index 0 failed to match:
   expected #<Nokogiri::XML::Element:0x3fe7b9f4a3b8 name="string" children=[#<Nokogiri::XML::Text:0x3fe7b9f46b00 "Apples">]> to be a kind of String

Any idea where I'm going wrong here? I imagine it's probably just a problem on my side.

@apotonick
Copy link
Member

Weird, can you find a test case in Representable that proofs this actually does work?

@mhuggins
Copy link
Author

Sorry, I missed your response from before! I'll take a look and follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants