Google-Plus
view release on metacpan or search on metacpan
lib/Google/Plus.pm view on Meta::CPAN
package Google::Plus;
{
$Google::Plus::VERSION = '0.004';
}
use Mojo::Base -base;
use v5.10.1;
use Mojo::URL;
use Mojo::UserAgent;
use IO::Socket::SSL 1.37;
use Carp;
has [qw/key ua/];
our $service = 'https://www.googleapis.com/plus/v1';
our %API = (
person => '/people/ID',
activities => '/people/ID/activities/COLLECTION',
activity => '/activities/ID'
);
# transform our api dispatch above into an HTTPS request
# returns JSON decoded result or throws exception otherwise
sub _request {
my ($self, $api, $id, $args) = @_;
my $key = $self->key;
my $ua = $self->ua;
$api =~ s/ID/$id/;
my $url = Mojo::URL->new(join '', $service => $api);
# $args is a hashref corresponding to optional query parameters (such
# as nextPageToken)
if (ref $args eq 'HASH') {
PARAM: while (my ($k, $v) = each %$args) {
$k eq 'collection' and do {
my $p = $url->path->to_string;
$p =~ s/COLLECTION/$v/;
$url = $url->path($p);
next PARAM;
};
$url = $url->query({$k => $v});
}
}
$url = $url->query({key => $key});
my $tx = $ua->get($url);
$tx->success and return $tx->res->json;
# we never get here, unless something went wrong
my $message = $tx->error;
$tx->res->json and do {
my $json_err = $tx->res->json->{error}->{message};
$message = join ' ', $message => $json_err;
};
die "Error: $message";
}
sub new {
my $self = bless {}, shift;
croak "API key required" unless $_[0] and $_[0] eq 'key';
$self->key($_[1]);
$self->ua(Mojo::UserAgent->new->detect_proxy);
$self;
( run in 1.850 second using v1.01-cache-2.11-cpan-39bf76dae61 )