| 1 | package Google::Plus; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 1 | $Google::Plus::VERSION = '0.004'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use v5.10.1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Mojo::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use IO::Socket::SSL 1.37; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | has [qw/key ua/]; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our $service = 'https://www.googleapis.com/plus/v1'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | our %API = ( | |||||||||||||||||||||||||||
| 18 | person => '/people/ID', | ||||||||||||||||||||||||||||
| 19 | activities => '/people/ID/activities/COLLECTION', | ||||||||||||||||||||||||||||
| 20 | activity => '/activities/ID' | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # transform our api dispatch above into an HTTPS request | ||||||||||||||||||||||||||||
| 24 | # returns JSON decoded result or throws exception otherwise | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub _request { | ||||||||||||||||||||||||||
|
Subroutine
_request: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 26 | 0 | my ($self, $api, $id, $args) = @_; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | my $key = $self->key; | |||||||||||||||||||||||||||
| 29 | 0 | my $ua = $self->ua; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | $api =~ s/ID/$id/; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my $url = Mojo::URL->new(join '', $service => $api); | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # $args is a hashref corresponding to optional query parameters (such | ||||||||||||||||||||||||||||
| 36 | # as nextPageToken) | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if (ref $args eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | PARAM: while (my ($k, $v) = each %$args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $k eq 'collection' and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | my $p = $url->path->to_string; | |||||||||||||||||||||||||||
| 41 | 0 | $p =~ s/COLLECTION/$v/; | |||||||||||||||||||||||||||
| 42 | 0 | $url = $url->path($p); | |||||||||||||||||||||||||||
| 43 | 0 | next PARAM; | |||||||||||||||||||||||||||
| 44 | }; | ||||||||||||||||||||||||||||
| 45 | 0 | $url = $url->query({$k => $v}); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 0 | $url = $url->query({key => $key}); | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my $tx = $ua->get($url); | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | $tx->success and return $tx->res->json; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # we never get here, unless something went wrong | ||||||||||||||||||||||||||||
| 54 | 0 | my $message = $tx->error; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | $tx->res->json and do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | my $json_err = $tx->res->json->{error}->{message}; | |||||||||||||||||||||||||||
| 57 | 0 | $message = join ' ', $message => $json_err; | |||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||
| 59 | 0 | die "Error: $message"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 0 | my $self = bless {}, shift; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | croak "API key required" unless $_[0] and $_[0] eq 'key'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_[0] and $_[0] eq "key"
MC/DC0%0/2
$_[0] and $_[0] eq "key"
$_[0]
$_[0] eq "key"
Truth table0%0/3
A and B
A= $_[0]B= $_[0] eq "key"
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | $self->key($_[1]); | |||||||||||||||||||||||||||
| 68 | 0 | $self->ua(Mojo::UserAgent->new->detect_proxy); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | $self; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub person { | ||||||||||||||||||||||||||
|
Subroutine
person: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | my ($self, $user_id, $fields) = @_; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | croak 'user ID required' unless $user_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | croak 'Invalid user ID' unless $user_id =~ /[0-9]+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | $fields | ||||||||||||||||||||||||||||
| 80 | ? $self->_request($API{person} => $user_id, {fields => $fields}) | ||||||||||||||||||||||||||||
| 81 | 0 | ▶ | : $self->_request($API{person} => $user_id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | sub activities { | ||||||||||||||||||||||||||
|
Subroutine
activities: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | my ($self, $user_id, $collection, $next, $fields) = @_; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | croak 'user ID required' unless $user_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | croak 'Invalid user ID' unless $user_id =~ /[0-9]+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $collection //= 'public'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$collection //= "public"
MC/DC0%0/1
$collection //= "public"
$collection
Truth table0%0/2
A
A= $collection
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my %args = (collection => $collection); | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | $args{pageToken} = $next if $next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $args{fields} = $fields if $fields; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | $self->_request($API{activities} => $user_id, \%args); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub activity { | ||||||||||||||||||||||||||
|
Subroutine
activity: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($self, $activity_id, $fields) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | croak 'activity ID required' unless $activity_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | croak 'Invalid activity ID' unless $activity_id =~ /\w+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | $fields | ||||||||||||||||||||||||||||
| 106 | ? $self->_request($API{activity} => $activity_id, {fields => $fields}) | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | : $self->_request($API{activity} => $activity_id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | "Inspired by tempire's Google::Voice :3"; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | __END__ | ||||||||||||||||||||||||||||