improve de-nastification for lists
authorjeff <jeff>
Wed, 9 Dec 2009 02:05:55 +0000 (02:05 +0000)
committerjeff <jeff>
Wed, 9 Dec 2009 02:05:55 +0000 (02:05 +0000)
Changes
lib/Net/OpenSRS.pm

diff --git a/Changes b/Changes
index 6df9503..5c4d457 100644 (file)
--- a/Changes
+++ b/Changes
@@ -8,3 +8,6 @@
 0.03 Tue Sep 1 2009
     - Ugly fix for Expat parser error due to spaces in an element name
       in the OpenSRS renew domain response.
+
+0.04
+   -  Correct de-nastification to handle lists
index 47150d3..2b74009 100644 (file)
@@ -1166,7 +1166,13 @@ XML
         my $rslt = $res->content;
         # OpenSRS renew response triggers Expat parser error due to spaces in element name
         $rslt =~ s/registration expiration date/registration_expiration_date/g;
-        eval { $struct = XML::Simple::XMLin( $rslt ); };
+
+        eval { $struct = XML::Simple::XMLin(
+                 $rslt,
+                 'KeyAttr' => [ 'dt_assoc' ],
+                 'GroupTags' => { 'dt_assoc' => 'item',  'dt_array' => 'item' },
+               );
+        };
 
         if ($self->debug_level > 1) {
             $self->debug("\nOpenSRS Response XML:\n" . '-' x 30);
@@ -1176,10 +1182,7 @@ XML
 
         # get the struct looking just how we want it.
         # (de-nastify it.)
-        $xml = XML::Simple::XMLout( $struct->{body}->{data_block}->{dt_assoc}->{item} );
-        $struct = XML::Simple::XMLin( $xml );
-        $xml = XML::Simple::XMLout( $struct->{attributes}->{item} );
-        $struct->{attributes} = XML::Simple::XMLin( $xml );
+        (undef, $struct) = _denastify( $struct->{body}->{data_block} );
     }
     else {
         $self->debug("HTTP error: " . $res->status_line);
@@ -1229,6 +1232,26 @@ sub _format
     return $xml;
 }
 
+sub _denastify {
+  my ($arg) = ( shift );
+  if ( ref($arg) eq 'HASH' ) {
+    my $value;
+    if ( exists( $arg->{content} ) ) {
+      $value = $arg->{content};
+    } elsif ( exists( $arg->{dt_array} ) ) {
+      $value = [ map {
+                       { map { _denastify($_) } @{ $_->{dt_assoc} } }
+                     }
+                 @{ $arg->{dt_array} }
+               ];
+    } elsif ( exists( $arg->{dt_assoc} ) ) {
+      $value = { map { _denastify($_) } @{ $arg->{dt_assoc} } };
+    }
+    return ( $arg->{key} => $value );
+  }
+  ();
+}
+
 =back
 
 =head1 Author