crack icon indicating copy to clipboard operation
crack copied to clipboard

Parsing elements AND attributes

Open ryanwood opened this issue 15 years ago • 3 comments

I've finally tracked down an issue I'm having to crack. (I bet that's been said before!) I'm using using hashrocket/mousetrap to connect to the CheddarGetter API. It depends on httparty which in turn depends on crack. Here's the issue. The API returns an XML string like so:

<error id="1234" code="404" auxCode="">Customer not found</error>

What I get back from httparty (via crack) is:

{ :error => "Customer not found" }

I basically lose all the meta data about the call. This is even more true for validation errors:

<error id="12345" code="412" auxCode="firstName:isEmpty">A value is required</error>

becomes

{ :error => " value is required" }

which is not really helpful.

I know crack is meant to simplify parsing, but it's dropping important data from a valid xml string. I saw John responded to a comment on the announcement post for crack:

"It works with attributes or elements, just not combinations."

Is there any plan to add that ability? It's pretty important. (I don't mean to sound ungrateful. Crack and httparty are great libs. Thanks much!)

ryanwood avatar Jun 27 '10 16:06 ryanwood

Has there been a fix or update to this? before i dive into the world of monkey patching this, i want to see if there has been any update.

blinder avatar Dec 20 '10 03:12 blinder

I also run into this when empty valid xml elements are returned from a library. The point is that I cannot know whether what I get in the hash is the value itself or the attributes, and is so relevant in my case.

I think that there should be a way to be able to switch from the current implementation to another one, where both elements and attributes are included, making use of a configurable prefix in the keys of the attributes.

This way backward compatibility is mantained and everybody can make use of the new functionallity adapting the prefix to fits their domain problem.

magec avatar Jan 11 '11 10:01 magec

I ran into this as well and started looking at the code/working on a fix. Your attributes ARE there, it's just poorly documented. The relevant code is here:

https://github.com/jnunemaker/crack/blob/master/lib/crack/xml.rb#L85-86

The REXMLUtilityNode has an @attributes accessor added to hold the attributes.

fermion avatar May 04 '11 19:05 fermion