Google/Discovery.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 151 CC 5 SCAR 14.1
CC
5
Cov
0%
metaForAPI
12.0
latestStableVersion
6.0
getMethodMeta
6.0
CRAP 4.1
1 package Moo::Google::Discovery;
2 $Moo::Google::Discovery::VERSION = '0.03';
3  
4 # ABSTRACT: Methods for working with Google API discovery service
5  
6  
7 use Moo;
8 use Mojo::UserAgent;
9 use List::Util qw(uniq);
10 use Hash::Slice qw/slice/;
11  
12 use Data::Dumper;
13  
14 has 'ua' => ( is => 'ro', default => sub { Mojo::UserAgent->new }, lazy => 1 );
15 has 'discovery_full' => ( is => 'ro', default => \&discover_all, lazy => 1 );
16 has 'debug' => ( is => 'rw', default => 0, lazy => 1 );
17  
18  
19 sub getRest {
20 my ( $self, $params ) = @_;
21 return $self->ua->get( 'https://www.googleapis.com/discovery/v1/apis/'
22 . $params->{api} . '/'
23 . $params->{version}
24 . '/rest' )->result->json;
25 }
26  
27  
28 sub discover_all {
29 shift->ua->get('https://www.googleapis.com/discovery/v1/apis')
30 ->result->json;
31 }
32  
33  
34 sub availableAPIs {
35 my $self = shift;
36 my $all = $self->discover_all()->{items};
37 for my $i (@$all) {
38 $i = {
39 map { $_ => $i->{$_} }
40 grep { exists $i->{$_} } qw/name version documentationLink/
41 };
42 }
43 my @subset = uniq map { $_->{name} } @$all; ## unique names
44 # warn scalar @$all;
45 # warn scalar @subset;
46 # warn Dumper \@subset;
47 # my @a = map { $_->{name} } @$all;
48  
49 my @arr;
50 for my $s (@subset) {
51 my @v = map { $_->{version} } grep { $_->{name} eq $s } @$all;
52 my @doclinks =
53 uniq map { $_->{documentationLink} } grep { $_->{name} eq $s } @$all;
54  
55 # warn "Match! :".Dumper \@v;
56 # my $versions = grep
57 push @arr, { name => $s, versions => \@v, doclinks => \@doclinks };
58 }
59  
60 return \@arr;
61  
62 # warn Dumper \@arr;
63  
64 # return \@a;
65 }
66  
67  
68 sub exists {
69 my ( $self, $api ) = @_;
70 my $apis_all = $self->availableAPIs();
71 my $res = grep { $_->{name} eq $api } @$apis_all;
72 }
73  
74  
75 sub printSupported {
76 my $self = shift;
77 my $apis_all = $self->availableAPIs();
78 for my $api (@$apis_all) {
79 print $api->{name} . ' : '
80 . join( ',', @{ $api->{versions} } ) . ' : '
81 . join( ',', @{ $api->{doclinks} } ) . "\n";
82 }
83 }
84  
85  
86 sub availableVersions {
87 my ( $self, $api ) = @_;
88 my $apis_all = $self->availableAPIs();
89 my @api_target = grep { $_->{name} eq $api } @$apis_all;
90 return $api_target[0]->{versions};
91 }
92  
93  
94 sub latestStableVersion {
95 my ( $self, $api ) = @_;
96 my $versions = $self->availableVersions($api); # arrayref
97 if ( $versions->[-1] =~ /beta/ ) {
98 return $versions->[0];
99 }
100 else {
101 return $versions->[-1];
102 }
103 }
104  
105  
106 sub findAPIsWithDiffVers {
107 my $self = shift;
108 my $all = $self->availableAPIs();
109 grep { scalar @{ $_->{versions} } > 1 } @$all;
110 }
111  
112  
113 sub searchInServices {
114 my ( $self, $string ) = @_;
115  
116 # warn Dumper $self->availableAPIs();
117 my @res = grep { $_->{name} eq lc $string } @{ $self->availableAPIs };
118  
119 # warn "Result: ".Dumper \@res;
120 return $res[0];
121 }
122  
123  
124 sub getMethodMeta {
125 my ( $self, $caller ) = @_;
126  
127 # $caller = 'Moo::Google::Calendar::CalendarList::delete';
128 my @a = split( /::/, $caller );
129  
130 # warn Dumper \@a;
131 my $method = pop @a; # delete
132 my $resource = lcfirst pop @a; # CalendarList
133 my $service = lc pop @a; # Calendar
134 my $service_data =
135 $self->searchInServices($service); # was string, become hash
136 warn "getResourcesMeta:service_data : " . Dumper $service_data
137 if ( $self->debug );
138  
139 my $all = $self->getRest(
140 {
141 api => $service_data->{name},
142 version => $service_data->{versions}[0]
143 }
144 );
145 my $baseUrl = $all->{baseUrl};
146 my $resource_data =
147 $all->{resources}{$resource}; # return just a list of all methods
148 my $method_data = $resource_data->{methods}{$method}; # need httpMethod
149 $method_data->{path} = $baseUrl . $method_data->{path};
150 my $res = slice $method_data, qw/httpMethod path id/;
151 }
152  
153  
154 sub getResourceMeta {
155 my ( $self, $package ) = @_;
156  
157 # $package = 'Moo::Google::Calendar::Events';
158 my @a = split( /::/, $package );
159 my $resource = lcfirst pop @a; # CalendarList
160 my $service = lc pop @a; # Calendar
161 my $service_data =
162 $self->searchInServices($service); # was string, become hash
163 my $all = $self->getRest(
164 {
165 api => $service_data->{name},
166 version => $service_data->{versions}[0]
167 }
168 );
169 return $all->{resources}{$resource}; # return just a list of all methods
170 }
171  
172  
173 sub listOfMethods {
174 my ( $self, $package ) = @_;
175 my $r = $self->getResourceMeta($package);
176 my @a = keys %{ $r->{methods} };
177 return \@a;
178 }
179  
180  
181 sub metaForAPI {
182 my ( $self, $params ) = @_;
183 my $full = $self->discovery_full;
184 my @a;
185  
186 if ( defined $params->{api} ) {
187 @a = grep { $_->{name} eq $params->{api} } @{ $full->{items} };
188 }
189 else {
190 die "metaForAPI() : No api specified!";
191 }
192  
193 if ( defined $params->{version} ) {
194 @a = grep { $_->{version} eq $params->{version} } @a;
195 }
196  
197 return $a[0];
198 }
199  
200 1;
201  
202 __END__