Changeset 13e2400 in lab.git for Dev/github


Ignore:
Timestamp:
Aug 21, 2013 4:44:27 PM (11 years ago)
Author:
Ken-ichi Mito <mitty@…>
Branches:
master
Children:
4351b0c
Parents:
f7f894d
git-author:
Ken-ichi Mito <mitty@…> (08/21/13 16:38:15)
git-committer:
Ken-ichi Mito <mitty@…> (08/21/13 16:44:27)
Message:

change package utils to GitHubBackup::API

  • add GitHubBackup::API->access_token to use OAuth with json api
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/github/GitHubBackup.pm

    rf7f894d r13e2400  
    1 package utils; 
     1package GitHubBackup::API; 
    22use strict; 
    33use warnings; 
     
    88use JSON; 
    99 
     10sub new { 
     11    my $class = shift; 
     12    my $args  = shift; 
     13     
     14    return bless $args, $class; 
     15} 
     16 
    1017sub json_api { 
    11     my $url = shift; 
     18    my $self = shift; 
     19    my $url  = shift; 
    1220     
    1321    my $ua = LWP::UserAgent->new; 
     
    2432 
    2533sub get { 
    26     my $url = shift; 
     34    my $self = shift; 
     35    my $url  = shift; 
    2736    my %parameters = @_; 
    2837     
     
    3544    my $data = []; 
    3645    while(1) { 
    37         my $result = json_api("$url?per_page=100&page=$page$parameters"); 
     46        my $result = $self->json_api("$url?per_page=100&page=$page$parameters"); 
    3847        if (ref($result) eq 'ARRAY' && scalar @$result > 0) { 
    3948            push @$data, @$result; 
     
    4857} 
    4958 
     59sub access_token { 
     60    my $self = shift; 
     61     
     62    return $self->{access_token}->(); 
     63} 
     64 
     65 
    5066package GitHubBackup; 
    5167 
     
    100116} 
    101117 
     118sub access_token { 
     119    my $self = shift; 
     120    my $args = shift; 
     121     
     122    if (defined $args) { 
     123        $self->{access_token} = $args; 
     124    } 
     125     
     126    return $self->{access_token}; 
     127} 
     128 
     129sub api { 
     130    my $self = shift; 
     131     
     132    unless ($self->{api}) { 
     133        $self->{api} = GitHubBackup::API->new({ 
     134            access_token => sub {$self->access_token}, 
     135        }); 
     136    } 
     137     
     138    return $self->{api}; 
     139} 
     140 
    102141sub repos { 
    103142    my $self = shift; 
     
    109148    my $result; 
    110149    if (my $repository = $self->repository) { 
    111         $result = [ utils::json_api("/repos/$account/$repository") ]; 
     150        $result = [ $self->api->json_api("/repos/$account/$repository") ]; 
    112151    } 
    113152    else { 
    114         $result = utils::get("/users/$account/repos"); 
     153        $result = $self->api->get("/users/$account/repos"); 
    115154    } 
    116155     
     
    119158            GitHubBackup::Repository->new({ 
    120159                directory => sub {$self->directory}, 
     160                api       => sub {$self->api}, 
    121161                repos     => $repos, 
    122162            }) 
     
    194234} 
    195235 
     236sub api { 
     237    my $self = shift; 
     238     
     239    return $self->{api}->(); 
     240} 
     241 
    196242sub message { 
    197243    my $self = shift; 
     
    237283    return $self->{forks} if ($self->{forks}); 
    238284     
    239     $self->{forks} = utils::get("/repos/" . $self->full_name . "/forks"); 
     285    $self->{forks} = $self->api->get("/repos/" . $self->full_name . "/forks"); 
    240286     
    241287    return $self->{forks}; 
     
    292338    return $self->{issues} if ($self->{issues}); 
    293339     
    294     my $open   = utils::get("/repos/" . $self->full_name . "/issues"); 
    295     my $closed = utils::get("/repos/" . $self->full_name . "/issues", state => 'closed'); 
     340    my $open   = $self->api->get("/repos/" . $self->full_name . "/issues"); 
     341    my $closed = $self->api->get("/repos/" . $self->full_name . "/issues", state => 'closed'); 
    296342     
    297343    if ($open)   { push @{$self->{issues}}, @$open } 
Note: See TracChangeset for help on using the changeset viewer.