[Bps-public-commit] net-lighthouse branch, master, updated. 6d14548b217ce3dee4751cd06d9fe0fe66a40699

sunnavy at bestpractical.com sunnavy at bestpractical.com
Fri Sep 11 11:15:40 EDT 2009


The branch, master has been updated
       via  6d14548b217ce3dee4751cd06d9fe0fe66a40699 (commit)
      from  b1f2edf797b962eb70f4e111ee093c9a7c7c4457 (commit)

Summary of changes:
 lib/Net/Lighthouse/Project.pm           |   40 ++++++++---------------------
 lib/Net/Lighthouse/Project/Changeset.pm |   10 +++----
 lib/Net/Lighthouse/Project/Message.pm   |   26 ++++++-------------
 lib/Net/Lighthouse/Project/Milestone.pm |   19 ++++---------
 lib/Net/Lighthouse/Project/Ticket.pm    |   16 +++--------
 lib/Net/Lighthouse/Project/TicketBin.pm |   42 +++++++++----------------------
 lib/Net/Lighthouse/User.pm              |    9 ++----
 lib/Net/Lighthouse/Util.pm              |   37 ++++++++++++++++++++++++++-
 8 files changed, 85 insertions(+), 114 deletions(-)

- Log -----------------------------------------------------------------
commit 6d14548b217ce3dee4751cd06d9fe0fe66a40699
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Sep 11 23:15:29 2009 +0800

    go on gather xml part: add translate_to_xml in Util.pm

diff --git a/lib/Net/Lighthouse/Project.pm b/lib/Net/Lighthouse/Project.pm
index a78c984..f60b15f 100644
--- a/lib/Net/Lighthouse/Project.pm
+++ b/lib/Net/Lighthouse/Project.pm
@@ -107,21 +107,12 @@ sub create {
     );
     my %args = @_;
 
-    if ( defined $args{name} ) {
-        $args{name} = { content => $args{name} };
-    }
-
-    for my $bool (qw/archived public/) {
-        next unless exists $args{$bool};
-        if ( $args{$bool} ) {
-            $args{$bool} = { content => 'true', type => 'boolean' };
-        }
-        else {
-            $args{$bool} = { content => 'false', type => 'boolean' };
-        }
-    }
+    my $xml = Net::Lighthouse::Util->translate_to_xml(
+        \%args,
+        root    => 'project',
+        boolean => [qw/archived public/],
+    );
 
-    my $xml = Net::Lighthouse->write_xml( { project => \%args });
     my $ua = $self->ua;
 
     my $url = $self->base_url . '/projects.xml';
@@ -151,21 +142,12 @@ sub update {
     );
     my %args = ( ( map { $_ => $self->$_ } qw/archived name public/ ), @_ );
 
-    if ( defined $args{name} ) {
-        $args{name} = { content => $args{name} };
-    }
-
-    for my $bool (qw/archived public/) {
-        next unless exists $args{$bool};
-        if ( $args{$bool} ) {
-            $args{$bool} = { content => 'true', type => 'boolean' };
-        }
-        else {
-            $args{$bool} = { content => 'false', type => 'boolean' };
-        }
-    }
+    my $xml = Net::Lighthouse::Util->translate_to_xml(
+        \%args,
+        root    => 'project',
+        boolean => [qw/archived public/],
+    );
 
-    my $xml = Net::Lighthouse->write_xml( { project => \%args });
     my $ua = $self->ua;
     my $url = $self->base_url . '/projects/' . $self->id . '.xml';
 
@@ -205,7 +187,7 @@ sub list {
     my $url = $self->base_url . '/projects.xml';
     my $res = $ua->get( $url );
     if ( $res->is_success ) {
-        my $ps = Net::Lighthouse::Util->read_xml( $res->content )->{project};
+        my $ps = Net::Lighthouse::Util->read_xml( $res->content )->{projects}{project};
         my @list = map {
             my $p = Net::Lighthouse::Project->new(
                 map { $_ => $self->$_ }
diff --git a/lib/Net/Lighthouse/Project/Changeset.pm b/lib/Net/Lighthouse/Project/Changeset.pm
index 0968ebd..8dda419 100644
--- a/lib/Net/Lighthouse/Project/Changeset.pm
+++ b/lib/Net/Lighthouse/Project/Changeset.pm
@@ -82,12 +82,9 @@ sub create {
     );
     my %args = @_;
 
-    for my $field (qw/revision body title changes changed_at/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'changeset', );
 
-    my $xml = Net::Lighthouse->write_xml( { changeset => \%args });
     my $ua = $self->ua;
 
     my $url = $self->base_url . '/projects/' . $self->project_id . '/changesets.xml';
@@ -133,7 +130,8 @@ sub list {
     my $ua  = $self->ua;
     my $res = $ua->get($url);
     if ( $res->is_success ) {
-        my $cs = Net::Lighthouse::Util->read_xml( $res->content )->{changeset};
+        my $cs =
+          Net::Lighthouse::Util->read_xml( $res->content )->{changesets}{changeset};
         my @list = map {
             my $t = Net::Lighthouse::Project::Changeset->new(
                 map { $_ => $self->$_ }
diff --git a/lib/Net/Lighthouse/Project/Message.pm b/lib/Net/Lighthouse/Project/Message.pm
index 27ef2a8..7b50b4f 100644
--- a/lib/Net/Lighthouse/Project/Message.pm
+++ b/lib/Net/Lighthouse/Project/Message.pm
@@ -109,12 +109,8 @@ sub create {
     );
     my %args = @_;
 
-    for my $field (qw/title body/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
-
-    my $xml = Net::Lighthouse->write_xml( { message => \%args });
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'message', );
     my $ua = $self->ua;
 
     my $url = $self->base_url . '/projects/' . $self->project_id . '/messages.xml';
@@ -142,14 +138,11 @@ sub create_comment {
     );
     my %args = @_;
 
-    for my $field (qw/body/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
-
     # TODO doc says <message>, but it doesn't work actually.
     # comment can work, though still with a problem
-    my $xml = Net::Lighthouse->write_xml( { comment => \%args });
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'comment', );
+
     my $ua = $self->ua;
 
     my $url =
@@ -185,12 +178,9 @@ sub update {
     );
     my %args = ( ( map { $_ => $self->$_ } qw/title body/ ), @_ );
 
-    for my $field (qw/title body/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'message', );
 
-    my $xml = Net::Lighthouse->write_xml( { message => \%args });
     my $ua = $self->ua;
     my $url =
         $self->base_url
@@ -240,7 +230,7 @@ sub list {
     my $ua  = $self->ua;
     my $res = $ua->get($url);
     if ( $res->is_success ) {
-        my $ms = Net::Lighthouse::Util->read_xml( $res->content )->{message};
+        my $ms = Net::Lighthouse::Util->read_xml( $res->content )->{messages}{message};
         my @list = map {
             my $t = Net::Lighthouse::Project::Message->new(
                 map { $_ => $self->$_ }
diff --git a/lib/Net/Lighthouse/Project/Milestone.pm b/lib/Net/Lighthouse/Project/Milestone.pm
index baa15ae..0b282d7 100644
--- a/lib/Net/Lighthouse/Project/Milestone.pm
+++ b/lib/Net/Lighthouse/Project/Milestone.pm
@@ -89,12 +89,8 @@ sub create {
     );
     my %args = @_;
 
-    for my $field (qw/goals title due_on/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
-
-    my $xml = Net::Lighthouse->write_xml( { milestone => \%args });
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'milestone', );
     my $ua = $self->ua;
 
     my $url = $self->base_url . '/projects/' . $self->project_id . '/milestones.xml';
@@ -124,12 +120,8 @@ sub update {
     );
     my %args = ( ( map { $_ => $self->$_ } qw/title goals due_on/ ), @_ );
 
-    for my $field (qw/goals title due_on/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
-
-    my $xml = Net::Lighthouse->write_xml( { milestone => \%args });
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'milestone', );
     my $ua = $self->ua;
     my $url =
         $self->base_url
@@ -178,7 +170,8 @@ sub list {
     my $ua  = $self->ua;
     my $res = $ua->get($url);
     if ( $res->is_success ) {
-        my $ms = Net::Lighthouse::Util->read_xml( $res->content )->{milestone};
+        my $ms =
+          Net::Lighthouse::Util->read_xml( $res->content )->{milestones}{milestone};
         my @list = map {
             my $t = Net::Lighthouse::Project::Milestone->new(
                 map { $_ => $self->$_ }
diff --git a/lib/Net/Lighthouse/Project/Ticket.pm b/lib/Net/Lighthouse/Project/Ticket.pm
index 44d138c..bde2b48 100644
--- a/lib/Net/Lighthouse/Project/Ticket.pm
+++ b/lib/Net/Lighthouse/Project/Ticket.pm
@@ -154,12 +154,9 @@ sub create {
     );
     my %args = @_;
 
-    for my $field (qw/title body state assigned_user_id milestone_id tag/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'ticket', );
 
-    my $xml = Net::Lighthouse->write_xml( { ticket => \%args });
     my $ua = $self->ua;
 
     my $url = $self->base_url . '/projects/' . $self->project_id . '/tickets.xml';
@@ -206,12 +203,9 @@ sub update {
         @_
     );
 
-    for my $field (qw/title body state assigned_user_id milestone_id tag/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'ticket', );
 
-    my $xml = Net::Lighthouse->write_xml( { ticket => \%args });
     my $ua = $self->ua;
     my $url =
         $self->base_url
@@ -277,7 +271,7 @@ sub list {
     my $ua  = $self->ua;
     my $res = $ua->get($url);
     if ( $res->is_success ) {
-        my $ts = Net::Lighthouse::Util->read_xml( $res->content )->{ticket};
+        my $ts = Net::Lighthouse::Util->read_xml( $res->content )->{tickets}{ticket};
         my @list = map {
             my $t = Net::Lighthouse::Project::Ticket->new(
                 map { $_ => $self->$_ }
diff --git a/lib/Net/Lighthouse/Project/TicketBin.pm b/lib/Net/Lighthouse/Project/TicketBin.pm
index ee95daf..60c49d9 100644
--- a/lib/Net/Lighthouse/Project/TicketBin.pm
+++ b/lib/Net/Lighthouse/Project/TicketBin.pm
@@ -96,22 +96,12 @@ sub create {
     );
     my %args = @_;
 
-    if ( exists $args{default} ) {
-        if ( $args{default} ) {
-            $args{default} = { content => 'true', type => 'boolean' };
-        }
-        else {
-            $args{default} = { content => 'false', type => 'boolean' };
-        }
-    }
-    
-
-    for my $field (qw/name query/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
+    my $xml = Net::Lighthouse::Util->translate_to_xml(
+        \%args,
+        root    => 'bin',
+        boolean => ['default'],
+    );
 
-    my $xml = Net::Lighthouse->write_xml( { bin => \%args } );
     my $ua = $self->ua;
 
     my $url = $self->base_url . '/projects/' . $self->project_id . '/bins.xml';
@@ -141,21 +131,12 @@ sub update {
     );
     my %args = ( ( map { $_ => $self->$_ } qw/name query default/ ), @_ );
 
-    if ( exists $args{default} ) {
-        if ( $args{default} ) {
-            $args{default} = { content => 'true', type => 'boolean' };
-        }
-        else {
-            $args{default} = { content => 'false', type => 'boolean' };
-        }
-    }
-
-    for my $field (qw/name query/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
+    my $xml = Net::Lighthouse::Util->translate_to_xml(
+        \%args,
+        root    => 'bin',
+        boolean => ['default'],
+    );
 
-    my $xml = Net::Lighthouse->write_xml( { bin => \%args } );
     my $ua = $self->ua;
     my $url =
         $self->base_url
@@ -205,7 +186,8 @@ sub list {
     my $ua   = $self->ua;
     my $res  = $ua->get($url);
     if ( $res->is_success ) {
-        my $bs = Net::Lighthouse::Util->read_xml( $res->content )->{'ticket-bin'};
+        my $bs =
+          Net::Lighthouse::Util->read_xml( $res->content )->{'ticket-bins'}{'ticket-bin'};
         my @list = map {
             my $t = Net::Lighthouse::Project::TicketBin->new(
                 map { $_ => $self->$_ }
diff --git a/lib/Net/Lighthouse/User.pm b/lib/Net/Lighthouse/User.pm
index a4b5b72..a440632 100644
--- a/lib/Net/Lighthouse/User.pm
+++ b/lib/Net/Lighthouse/User.pm
@@ -65,12 +65,9 @@ sub update {
     );
     my %args = ( ( map { $_ => $self->$_ } qw/name job website/ ), @_ );
 
-    for my $field (qw/name job website/) {
-        next unless exists $args{$field};
-        $args{$field} = { content => $args{$field} };
-    }
+    my $xml =
+      Net::Lighthouse::Util->translate_to_xml( \%args, root => 'user', );
 
-    my $xml = Net::Lighthouse->write_xml( { user => \%args } );
     my $ua  = $self->ua;
     my $url = $self->base_url . '/users/' . $self->id . '.xml';
 
@@ -97,7 +94,7 @@ sub memberships {
     my $res  = $ua->get($url);
     require Net::Lighthouse::User::Membership;
     if ( $res->is_success ) {
-        my $ms = Net::Lighthouse::Util->read_xml( $res->content )->{membership};
+        my $ms = Net::Lighthouse::Util->read_xml( $res->content )->{memberships}{membership};
         my @list = map {
             my $m = Net::Lighthouse::User::Membership->new;
             $m->load_from_xml($_);
diff --git a/lib/Net/Lighthouse/Util.pm b/lib/Net/Lighthouse/Util.pm
index 5095730..0c8b2b7 100644
--- a/lib/Net/Lighthouse/Util.pm
+++ b/lib/Net/Lighthouse/Util.pm
@@ -19,7 +19,7 @@ BEGIN {
 
 sub read_xml {
     my $self = shift;
-    return XMLin( @_, KeyAttr => [] );
+    return XMLin( @_, KeyAttr => [], KeepRoot => 1 );
 }
 
 sub write_xml {
@@ -33,6 +33,12 @@ sub translate_from_xml {
     my $ref = shift;
     return unless $ref;
     $ref = Net::Lighthouse::Util->read_xml( $ref ) unless ref $ref;
+
+    # remove root
+    if ( keys %$ref == 1 ) {
+        ($ref) = values %$ref;
+    }
+
     %$ref = map { my $new = $_; $new =~ s/-/_/g; $new => $ref->{$_} } keys %$ref;
     for my $k ( keys %$ref ) {
         if ( ref $ref->{$k} eq 'HASH' ) {
@@ -71,6 +77,35 @@ sub translate_from_xml {
     return $ref;
 }
 
+sub translate_to_xml {
+    my $self = shift;
+    my $ref  = shift;
+    my %args = @_;
+
+    my %normal = map { $_ => 1 } keys %$ref;
+
+    if ( $args{boolean} ) {
+        for my $boolean ( @{ $args{boolean} } ) {
+            delete $normal{$_};
+            next unless exists $ref->{$boolean};
+            if ( $ref->{$boolean} ) {
+                $ref->{$boolean} = { content => 'true', type => 'boolean' };
+            }
+            else {
+                $ref->{$boolean} = { content => 'false', type => 'boolean' };
+            }
+        }
+    }
+
+    for my $normal ( keys %normal ) {
+        next unless exists $ref->{$normal};
+        $ref->{$normal} = { content => $ref->{$normal} };
+    }
+
+    $ref = { $args{root} => $ref } if $args{root};
+    return Net::Lighthouse::Util->write_xml($ref);
+}
+
 sub datetime_from_string {
     my $class  = shift;
     my $string = shift;

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list