From fc647d5d5521650a6c1d75ad37facdca12b851cb Mon Sep 17 00:00:00 2001 From: Ken-ichi Mito Date: Mon, 19 Aug 2013 08:34:13 +0900 Subject: [PATCH] utils::get gives hashref tree from API --- Dev/github/GitHubBackup.pm | 46 ++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/Dev/github/GitHubBackup.pm b/Dev/github/GitHubBackup.pm index 18aabc7..3d57a0f 100644 --- a/Dev/github/GitHubBackup.pm +++ b/Dev/github/GitHubBackup.pm @@ -21,6 +21,24 @@ sub json_api { return $json->decode($res->content); } +sub get { + my $url = shift; + + my $page = 1; + my $data; + while(1) { + my $result = json_api("$url?per_page=100&page=$page"); + if (ref($result) eq 'ARRAY' && scalar @$result > 0) { + push @$data, @$result; + $page++; + + next; + } + last; + } + + return $data; +} package GitHubBackup; @@ -90,20 +108,8 @@ sub repos { return $self->{repos}; } - my $page = 1; - my @repos; - while (1) { - my $result = utils::json_api("/users/$account/repos?per_page=100&page=$page"); - if (ref($result) eq 'ARRAY' && scalar @$result > 0) { - push @repos, @$result; - $page++; - - next; - } - last; - } - - foreach my $repos (@repos) { + my $result = utils::get("/users/$account/repos"); + foreach my $repos (@$result) { push @{$self->{repos}}, GitHubBackup::Repository->new({ directory => sub {$self->directory}, @@ -195,17 +201,7 @@ sub get_forks { my $self = shift; return $self->{forks} if ($self->{forks}); - my $page = 1; - while (1) { - my $result = utils::json_api("/repos/" . $self->{full_name} . "/forks?per_page=100&page=$page"); - if (ref($result) eq 'ARRAY' && scalar @$result > 0) { - push @{$self->{forks}}, @$result; - $page++; - - next; - } - last; - } + $self->{forks} = utils::get("/repos/" . $self->{full_name} . "/forks"); return $self; } -- 1.7.9.5