Codeguard/Partner.pm

filter: Statementstmt 47.6% 30 / 63 Branchbran 50.0% 2 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 9 / 18 Podpod 85.7% 6 / 7 totaltotal 46.5% 47 / 101 CC 6 SCAR 15.9
CC
6
Cov
44%
_set_uri
20.0
_initialize
3.1
create_user
2.0
CRAP 4.9
« Codeguard/User.pm ↑ Summary
1 package WWW::Codeguard::Partner;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings FATAL => 'all', NONFATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use parent qw(WWW::Codeguard);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 WWW::Codeguard::Partner - Perl interface to interact with the Codeguard API as a 'partner'.
15  
16 =cut
17  
18 =head1 SYNOPSIS
19  
20 This module provides you with an perl interface to interact with the Codeguard API and perform the 'partner' level calls.
21  
22 use WWW::Codeguard::Partner;
23  
24 my $api = WWW::Codeguard::Partner->new(
25 $api_url,
26 {
27 partner_key => $partner_key,
28 verify_hostname => 1,
29 }
30 );
31  
32 $api->create_user($params_for_create_user);
33 $api->list_user($params_for_list_user);
34 $api->delete_user($params_for_delete_user);
35  
36 =cut
37  
38 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
39  
40 2 my $class = shift;
41 2 my $self = {};
42 2 bless $self, $class;
43 2 $self->_initialize(@_);
44 2 return $self;
45 }
46  
47 2 ▶sub _initialize {
Subroutine _initialize: called CC=3 SCAR=11.3
48  
49 2 my ($self, $api_url, $opts) = @_;
50  
51 2 $self->{api_url} = $api_url;
52 2 ▶ $self->{partner_key} = delete $opts->{partner_key} or $self->_error('partner_key is a required parameter', 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"partner_key"} = delete $opts->{"partner_key"} or $self->_error("partner_key is a required parameter", 1) 2 0 2
53  
54 # initialize the UA
55 $self->{_ua} = LWP::UserAgent->new(
56 agent => 'WWW-Codeguard-Partner '.$self->VERSION,
57 ssl_opts => {
58 2 ▶ verify_hostname => (exists $opts->{verify_hostname}? $opts->{verify_hostname} : 1),
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'verify_hostname'} ? : 0 2 2
59 },
60 );
61  
62 2 return $self;
63 }
64  
65 =head1 METHODS
66  
67 Each of these map to a call on Codeguard's Partner API.
68
69 =cut
70  
71 =head2 create_user
72  
73 This allows you to create an account, on to which you can add website/database resources. Params should be a hashref that contains the following attributes:
74  
75 Required: The request will not succeed without these attributes.
76  
77 name
78 email
79  
80 Optional Attributes:
81  
82 password
83 time_zone
84 partner_data - This is a string that can be used to store user-specific information, that the partner may use to identify the user (such as package id, etc)
85 plan_id
86  
87 =cut
88  
89 0 ▶sub create_user {
Subroutine create_user: not called CC=1 SCAR=6.9
Pod documented
90  
91 0 my ($self, $params) = @_;
92 0 return $self->_do_method('create_user', $params);
93 }
94  
95 =head2 list_user
96  
97 This allows you to fetch information for a user. Params should be a hashref that contains the following attributes:
98  
99 Required:
100  
101 user_id
102  
103 Optional:
104  
105 None
106  
107 =cut
108  
109 0 ▶sub list_user {
Subroutine list_user: not called CC=1 SCAR=6.9
Pod documented
110  
111 0 my ($self, $params) = @_;
112 0 return $self->_do_method('list_user', $params);
113 }
114  
115 =head2 delete_user
116  
117 This method is used to delete existing users that were created using the specified partner_key. Parters can not delete users created by other partners.
118  
119 B<Note:> Deleting a user resource will also delete all associated Website and Database records.
120  
121 Params should be a hashref that contains the following attributes:
122  
123 Required:
124  
125 user_id
126  
127 Optional Attributes:
128  
129 None
130  
131 =cut
132  
133 0 ▶sub delete_user {
Subroutine delete_user: not called CC=1 SCAR=6.9
Pod documented
134  
135 0 my ($self, $params) = @_;
136 0 return $self->_do_method('delete_user', $params);
137 }
138  
139 =head2 change_user_plan
140  
141 This method is used to change an existing user's plan. Params should be a hashref that contains the following attributes:
142  
143 Required:
144  
145 user_id
146 plan_id
147  
148 Optional Attributes
149  
150 None
151  
152 =cut
153  
154 0 ▶sub change_user_plan {
Subroutine change_user_plan: not called CC=1 SCAR=6.9
Pod documented
155  
156 0 my ($self, $params) = @_;
157 0 return $self->_do_method('change_user_plan', $params);
158 }
159  
160 =head2 user_quota_report
161  
162 Required:
163  
164 None
165  
166 Optional:
167  
168 None
169  
170 Returns an array of Users who are using more than their allowed disk quota.
171  
172 B<Note>: Full User models are not included. Only the Attributes listed below are returned:
173  
174 =over 4
175  
176 =item * email - User email address.
177  
178 =item * package - CodeGuard plan ID associated with this user.
179  
180 =item * quota - Allowed quota in bytes.
181  
182 =item * usage - Disk usage in bytes.
183  
184 =item * percent_usage - Percent of quota used. This can be > 100%.
185  
186 =item * signup_date - Date of User account creation.
187  
188 =item * overage_date - Date the user was notified or 'Not yet notified' if a notification has not yet occurred.
189  
190 =back
191  
192 =cut
193  
194 0 ▶sub user_quota_report {
Subroutine user_quota_report: not called CC=1 SCAR=6.9
Pod documented
195  
196 0 my ($self, $params) = @_;
197 0 return $self->_do_method('user_quota_report', $params);
198 }
199  
200 =head1 Accessors
201  
202 Basic accessor methods to retrieve the current settings
203  
204 =cut
205  
206 =head2 get_partner_key
207  
208 Returns the partner_key of the object instance.
209  
210 =cut
211  
212 1 ▶sub get_partner_key { shift->{partner_key}; }
Subroutine get_partner_key: called CC=1 SCAR=0.0
Pod documented
213  
214 # Internal Methods
215  
216 0 ▶sub _create_request {
Subroutine _create_request: not called CC=1 SCAR=6.9
217  
218 0 my ($self, $action, $params) = @_;
219 0 my $action_map = {
220 'change_user_plan' => 'POST',
221 'create_user' => 'POST',
222 'delete_user' => 'DELETE',
223 'list_user' => 'GET',
224 'user_quota_report' => 'GET',
225 };
226 0 my $request = HTTP::Request->new( $action_map->{$action} );
227 0 $request->header('Content-Type' => 'application/json' );
228 0 $self->_set_content($request, $params);
229 0 $self->_set_uri($action, $request, $params);
230 0 return $request;
231 }
232  
233 0 ▶sub _set_uri {
Subroutine _set_uri: not called CC=4 SCAR=30.0
234  
235 0 my ($self, $action, $request, $params) = @_;
236 0 my $base_url = $self->get_api_url();
237 my $uri_map = {
238 'change_user_plan' => '/users/'.($params->{user_id} || '').'/plan',
239 'create_user' => '/users',
240 'delete_user' => '/users/'.($params->{user_id} || ''),
241 0 ▶ 'list_user' => '/users/'.($params->{user_id} || ''),
Condition0%0/6
$params->{'user_id'} || ''
l!l
00
$params->{'user_id'} || ''
l!l
00
$params->{'user_id'} || ''
l!l
00
MC/DC0%0/1
$params->{'user_id'} || ''
$params->{'user_id'}
MC/DC0%0/1
$params->{'user_id'} || ''
$params->{'user_id'}
MC/DC0%0/1
$params->{'user_id'} || ''
$params->{'user_id'}
Truth table0%0/2
A
A= $params->{'user_id'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'user_id'}
Aresult
11
00
Truth table0%0/2
A
A= $params->{'user_id'}
Aresult
11
00
242 'user_quota_report' => '/partners/user_quota_report',
243 };
244 0 $request->uri($base_url.$uri_map->{$action}.'?api_key='.$self->get_partner_key);
245 0 return;
246 }
247  
248 0 ▶sub _fetch_required_params {
Subroutine _fetch_required_params: not called CC=1 SCAR=6.9
249  
250 0 my ($self, $action, $params) = @_;
251 my $required_keys_map = {
252 0 'create_user' => { map { ($_ => 1) } qw(name email) },
253 0 'list_user' => { map { ($_ => 1) } qw(user_id) },
254 0 'change_user_plan' => { map { ($_ => 1) } qw(user_id plan_id) },
255 'user_quota_report' => { },
256 };
257 0 $required_keys_map->{delete_user} = $required_keys_map->{list_user};
258 0 return $required_keys_map->{$action};
259 }
260  
261 0 ▶sub _fetch_optional_params {
Subroutine _fetch_optional_params: not called CC=1 SCAR=6.9
262  
263 0 my ($self, $action) = @_;
264 my $optional_keys_map = {
265 0 'create_user' => { map { ($_ => 1) } qw(password time_zone partner_data plan_id) },
266 };
267 0 return $optional_keys_map->{$action};
268 }
269  
270 =head1 AUTHOR
271  
272 Rishwanth Yeddula, C<< <ryeddula at cpan.org> >>
273  
274 =head1 BUGS
275  
276 Please report any bugs or feature requests to C<bug-www-codeguard at rt.cpan.org>, or through
277 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Codeguard>. I will be notified, and then you'll
278 automatically be notified of progress on your bug as I make changes.
279  
280 =head1 SUPPORT
281  
282 You can find documentation for this module with the following perldoc commands.
283  
284 perldoc WWW::Codeguard
285 perldoc WWW::Codeguard::Partner
286 perldoc WWW::Codeguard::User
287  
288  
289 You can also look for information at:
290  
291 =over 4
292  
293 =item * RT: CPAN's request tracker (report bugs here)
294  
295 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Codeguard>
296  
297 =item * AnnoCPAN: Annotated CPAN documentation
298  
299 L<http://annocpan.org/dist/WWW-Codeguard>
300  
301 =item * CPAN Ratings
302  
303 L<http://cpanratings.perl.org/d/WWW-Codeguard>
304  
305 =item * Search CPAN
306  
307 L<http://search.cpan.org/dist/WWW-Codeguard/>
308  
309 =back
310  
311 =head1 ACKNOWLEDGMENTS
312  
313 Thanks to L<Hostgator.com|http://hostgator.com/> for funding the development of this module and providing test resources.
314  
315 =head1 LICENSE AND COPYRIGHT
316  
317 Copyright 2014 Rishwanth Yeddula.
318  
319 This program is free software; you can redistribute it and/or modify it
320 under the terms of the the Artistic License (2.0). You may obtain a
321 copy of the full license at:
322  
323 L<http://www.perlfoundation.org/artistic_license_2_0>
324  
325 Any use, modification, and distribution of the Standard or Modified
326 Versions is governed by this Artistic License. By using, modifying or
327 distributing the Package, you accept this license. Do not use, modify,
328 or distribute the Package, if you do not accept this license.
329  
330 If your Modified Version has been derived from a Modified Version made
331 by someone other than you, you are nevertheless required to ensure that
332 your Modified Version complies with the requirements of this license.
333  
334 This license does not grant you the right to use any trademark, service
335 mark, tradename, or logo of the Copyright Holder.
336  
337 This license includes the non-exclusive, worldwide, free-of-charge
338 patent license to make, have made, use, offer to sell, sell, import and
339 otherwise transfer the Package with respect to any patent claims
340 licensable by the Copyright Holder that are necessarily infringed by the
341 Package. If you institute patent litigation (including a cross-claim or
342 counterclaim) against any party alleging that the Package constitutes
343 direct or contributory patent infringement, then this Artistic License
344 to you shall terminate on the date that such litigation is filed.
345  
346 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
347 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
348 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
349 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
350 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
351 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
352 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
353 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
354  
355 =cut
356  
357 1 1;
« Codeguard/User.pm ↑ Summary