From: Ken-ichi Mito Date: Sun, 18 Aug 2013 23:34:13 +0000 (+0900) Subject: utils::get gives hashref tree from API X-Git-Url: http://lab.mitty.jp/git/?a=commitdiff_plain;h=fc647d5d5521650a6c1d75ad37facdca12b851cb;p=lab.git utils::get gives hashref tree from API --- 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; }