Last change
on this file since 6a2e5f1 was
6a2e5f1,
checked in by Ken-ichi Mito <mitty@…>, 11 years ago
|
GitHubBackup module for backup github project
- work in progress
- GitHubBackup->new->repos returns repository list
|
-
Property mode set to
100644
|
File size:
1.8 KB
|
Rev | Line | |
---|
[6a2e5f1] | 1 | package utils; |
---|
| 2 | use strict; |
---|
| 3 | use warnings; |
---|
| 4 | use utf8; |
---|
| 5 | use Carp qw(croak); |
---|
| 6 | |
---|
| 7 | use LWP::UserAgent; |
---|
| 8 | use JSON; |
---|
| 9 | my $ua = LWP::UserAgent->new; |
---|
| 10 | my $json = JSON->new->utf8->indent; |
---|
| 11 | |
---|
| 12 | sub json_api { |
---|
| 13 | my $url = shift; |
---|
| 14 | |
---|
| 15 | my $res = $ua->get( |
---|
| 16 | "https://api.github.com$url" |
---|
| 17 | ); |
---|
| 18 | |
---|
| 19 | $res->is_success or croak $res->status_line; |
---|
| 20 | |
---|
| 21 | return $json->decode($res->content); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | package GitHubBackup; |
---|
| 25 | use base qw(Class::Accessor::Fast); |
---|
| 26 | |
---|
| 27 | use strict; |
---|
| 28 | use warnings; |
---|
| 29 | use utf8; |
---|
| 30 | use Carp qw(croak); |
---|
| 31 | |
---|
| 32 | __PACKAGE__->mk_accessors( qw( |
---|
| 33 | directory |
---|
| 34 | account |
---|
| 35 | repository |
---|
| 36 | )); |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | # both hash and hashref are acceptable |
---|
| 40 | sub new { |
---|
| 41 | my $class = shift; |
---|
| 42 | |
---|
| 43 | my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_}; |
---|
| 44 | return $class->SUPER::new($args); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | sub repos { |
---|
| 48 | my $self = shift; |
---|
| 49 | |
---|
| 50 | my $account = $self->account or croak "account is not set"; |
---|
| 51 | if (my $repository = $self->repository) { |
---|
| 52 | $self->{repos} = [ |
---|
| 53 | GitHubBackup::Repository->new( |
---|
| 54 | {full_name => "$account/$repository"} |
---|
| 55 | ) |
---|
| 56 | ]; |
---|
| 57 | |
---|
| 58 | return $self->{repos}; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | my $page = 1; |
---|
| 62 | my @repos; |
---|
| 63 | while (1) { |
---|
| 64 | my $result = utils::json_api("/users/$account/repos?per_page=100&page=$page"); |
---|
| 65 | if (ref($result) eq 'ARRAY' && scalar @$result > 0) { |
---|
| 66 | push @repos, @$result; |
---|
| 67 | $page++; |
---|
| 68 | |
---|
| 69 | next; |
---|
| 70 | } |
---|
| 71 | last; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | foreach my $repos (@repos) { |
---|
| 75 | push @{$self->{repos}}, |
---|
| 76 | GitHubBackup::Repository->new({ |
---|
| 77 | full_name => $repos->{full_name}, |
---|
| 78 | clone_url => $repos->{clone_url}, |
---|
| 79 | }) |
---|
| 80 | ; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | return $self->{repos}; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | package GitHubBackup::Repository; |
---|
| 88 | use base qw(Class::Accessor::Fast); |
---|
| 89 | |
---|
| 90 | use strict; |
---|
| 91 | use warnings; |
---|
| 92 | use utf8; |
---|
| 93 | use Carp qw(croak); |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | 1; |
---|
| 98 | __END__ |
---|
Note: See
TracBrowser
for help on using the repository browser.