diff options
author | Fayland Lam <fayland@gmail.com> | 2011-09-25 15:33:02 +0800 |
---|---|---|
committer | Fayland Lam <fayland@gmail.com> | 2011-09-25 15:33:02 +0800 |
commit | afb604e470ce28d91c54cda0d784b8c5805c6005 (patch) | |
tree | 1c588fe2fba2d9c4a239f9c3fa2422d24a0d4550 /lib | |
parent | c2c061b9edc35f3c7dad69ccfaa901b9bd7d247a (diff) |
init orgs, build methods on fly
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Net/GitHub/V3.pm | 17 | ||||
-rw-r--r-- | lib/Net/GitHub/V3/Issues.pm | 11 | ||||
-rw-r--r-- | lib/Net/GitHub/V3/Orgs.pm | 81 | ||||
-rw-r--r-- | lib/Net/GitHub/V3/PullRequests.pm | 11 | ||||
-rw-r--r-- | lib/Net/GitHub/V3/Query.pm | 34 | ||||
-rw-r--r-- | lib/Net/GitHub/V3/Repos.pm | 11 | ||||
-rw-r--r-- | lib/Net/GitHub/V3/Users.pm | 11 |
7 files changed, 135 insertions, 41 deletions
diff --git a/lib/Net/GitHub/V3.pm b/lib/Net/GitHub/V3.pm index a098288..3b9a782 100644 --- a/lib/Net/GitHub/V3.pm +++ b/lib/Net/GitHub/V3.pm @@ -11,6 +11,7 @@ use Net::GitHub::V3::Users; use Net::GitHub::V3::Repos; use Net::GitHub::V3::Issues; use Net::GitHub::V3::PullRequests; +use Net::GitHub::V3::Orgs; has '+is_main_module' => (default => 1); @@ -24,6 +25,16 @@ has 'user' => ( }, ); +has 'org' => ( + is => 'rw', + isa => 'Net::GitHub::V3::Orgs', + lazy => 1, + default => sub { + my $self = shift; + return Net::GitHub::V3::Orgs->new( $self->args_to_pass ); + }, +); + has 'repos' => ( is => 'rw', isa => 'Net::GitHub::V3::Repos', @@ -203,6 +214,12 @@ L<Net::GitHub::V3::Issues> L<Net::GitHub::V3::PullRequests> +=head3 org + + my @orgs = $gh->org->orgs; + +L<Net::GitHub::V3::Orgs> + =head1 SEE ALSO L<Any::Moose>, L<Pithub> diff --git a/lib/Net/GitHub/V3/Issues.pm b/lib/Net/GitHub/V3/Issues.pm index ff6a9e6..d27f85a 100644 --- a/lib/Net/GitHub/V3/Issues.pm +++ b/lib/Net/GitHub/V3/Issues.pm @@ -611,13 +611,6 @@ L<http://developer.github.com/v3/issues/milestones/> =back -=head1 AUTHOR +=head1 AUTHOR & COPYRIGHT & LICENSE -Fayland Lam, C<< <fayland at gmail.com> >> - -=head1 COPYRIGHT & LICENSE - -Copyright 2009 Fayland Lam, all rights reserved. - -This program is free software; you can redistribute it and/or modify it -under the same terms as Perl itself. +Refer L<Net::GitHub>
\ No newline at end of file diff --git a/lib/Net/GitHub/V3/Orgs.pm b/lib/Net/GitHub/V3/Orgs.pm new file mode 100644 index 0000000..e7396f2 --- /dev/null +++ b/lib/Net/GitHub/V3/Orgs.pm @@ -0,0 +1,81 @@ +package Net::GitHub::V3::Orgs; + +use Any::Moose; + +our $VERSION = '0.40'; +our $AUTHORITY = 'cpan:FAYLAND'; + +use URI::Escape; + +with 'Net::GitHub::V3::Query'; + +sub orgs { + my ( $self, $user ) = @_; + + if ($user) { + return $self->query("/users/" . uri_escape($user) . '/orgs'); + } else { + return $self->query('/user/orgs'); + } +} + +## build methods on fly +my %__methods = ( + 'org' => { + url => "/orgs/%s", + }, + 'update_org' => { + url => "/orgs/%s", + method => 'PATCH', + args => 1 + }, + +); +__build_methods(__PACKAGE__, %__methods); + +no Any::Moose; +__PACKAGE__->meta->make_immutable; + +1; +__END__ + +=head1 NAME + +Net::GitHub::V3::Orgs - GitHub Orgs API + +=head1 SYNOPSIS + + use Net::GitHub::V3; + + my $gh = Net::GitHub::V3->new; # read L<Net::GitHub::V3> to set right authentication info + my $user = $gh->org; + +=head1 DESCRIPTION + +=head2 METHODS + +=head3 Orgs + +L<http://developer.github.com/v3/orgs/> + +=over 4 + +=item orgs + + my @orgs = $org->orgs(); # /user/org + my @orgs = $org->orgs( 'nothingmuch' ); # /users/:user/org + +=item org + + my $org = $org->org('perlchina'); + +=item update_org + + my $org = $org->update_org($org_name, { name => 'new org name' }); + +=back + + +=head1 AUTHOR & COPYRIGHT & LICENSE + +Refer L<Net::GitHub>
\ No newline at end of file diff --git a/lib/Net/GitHub/V3/PullRequests.pm b/lib/Net/GitHub/V3/PullRequests.pm index b2c548b..c6437e2 100644 --- a/lib/Net/GitHub/V3/PullRequests.pm +++ b/lib/Net/GitHub/V3/PullRequests.pm @@ -298,13 +298,6 @@ L<http://developer.github.com/v3/pulls/comments/> =back -=head1 AUTHOR +=head1 AUTHOR & COPYRIGHT & LICENSE -Fayland Lam, C<< <fayland at gmail.com> >> - -=head1 COPYRIGHT & LICENSE - -Copyright 2009 Fayland Lam, all rights reserved. - -This program is free software; you can redistribute it and/or modify it -under the same terms as Perl itself. +Refer L<Net::GitHub> diff --git a/lib/Net/GitHub/V3/Query.pm b/lib/Net/GitHub/V3/Query.pm index 118d5cb..1add103 100644 --- a/lib/Net/GitHub/V3/Query.pm +++ b/lib/Net/GitHub/V3/Query.pm @@ -7,7 +7,7 @@ our $AUTHORITY = 'cpan:FAYLAND'; use URI; use JSON::Any; -use WWW::Mechanize; +use WWW::Mechanize::GZip; use MIME::Base64; use HTTP::Request; use Carp qw/croak/; @@ -68,12 +68,12 @@ sub args_to_pass { } has 'ua' => ( - isa => 'WWW::Mechanize', + isa => 'WWW::Mechanize::GZip', is => 'ro', lazy => 1, default => sub { my $self = shift; - return WWW::Mechanize->new( + return WWW::Mechanize::GZip->new( agent => "perl-net-github $VERSION", cookie_jar => {}, stack_depth => 1, @@ -138,8 +138,7 @@ sub query { return $ua->res if $self->raw_response; return $ua->content if $self->raw_string; - - + if ($res->header('Content-Type') and $res->header('Content-Type') eq 'application/json') { my $json = $ua->content; $data = eval { $self->json->jsonToObj($json) }; @@ -166,6 +165,31 @@ sub query { return $data; } +## build methods on fly +sub __build_methods { + my $package = shift; + my %methods = @_; + + foreach my $m (keys %methods) { + my $v = $methods{$m}; + my $url = $v->{url}; + my $method = $v->{method} || 'GET'; + my $args = $v->{args}; # args for ->query + + $package->meta->add_method( $m => sub { + my $self = shift; + + # count how much %s inside u + my $n = ($url =~ /\%s/g); + my @uargs = splice(@_, 0, $n); + my $u = sprintf($url, @uargs); + + my @qargs = $args ? splice(@_, 0, $args) : (); + return $self->query($method, $u, @qargs); + } ); + } +} + no Any::Moose; 1; diff --git a/lib/Net/GitHub/V3/Repos.pm b/lib/Net/GitHub/V3/Repos.pm index f68dea5..be04638 100644 --- a/lib/Net/GitHub/V3/Repos.pm +++ b/lib/Net/GitHub/V3/Repos.pm @@ -885,13 +885,6 @@ L<http://developer.github.com/v3/repos/hooks/> =back -=head1 AUTHOR +=head1 AUTHOR & COPYRIGHT & LICENSE -Fayland Lam, C<< <fayland at gmail.com> >> - -=head1 COPYRIGHT & LICENSE - -Copyright 2009 Fayland Lam, all rights reserved. - -This program is free software; you can redistribute it and/or modify it -under the same terms as Perl itself. +Refer L<Net::GitHub>
\ No newline at end of file diff --git a/lib/Net/GitHub/V3/Users.pm b/lib/Net/GitHub/V3/Users.pm index 435c8d3..16060c8 100644 --- a/lib/Net/GitHub/V3/Users.pm +++ b/lib/Net/GitHub/V3/Users.pm @@ -226,13 +226,6 @@ L<http://developer.github.com/v3/users/keys/> =back -=head1 AUTHOR +=head1 AUTHOR & COPYRIGHT & LICENSE -Fayland Lam, C<< <fayland at gmail.com> >> - -=head1 COPYRIGHT & LICENSE - -Copyright 2009 Fayland Lam, all rights reserved. - -This program is free software; you can redistribute it and/or modify it -under the same terms as Perl itself. +Refer L<Net::GitHub>
\ No newline at end of file |