File Coverage

lib/WebService/VaultPress/Partner.pm
Criterion Covered Total %
statement 30 66 45.4
branch 0 28 0.0
condition n/a
subroutine 10 20 50.0
pod 4 8 50.0
total 44 122 36.0


line stmt bran cond sub pod time code
1             package WebService::VaultPress::Partner;
2 1     1   583668 use WebService::VaultPress::Partner::Response;
  1         4  
  1         77  
3 1     1   666 use WebService::VaultPress::Partner::Request::GoldenTicket;
  1         4  
  1         47  
4 1     1   686 use WebService::VaultPress::Partner::Request::History;
  1         5  
  1         46  
5 1     1   684 use WebService::VaultPress::Partner::Request::Usage;
  1         403  
  1         36  
6 1     1   9 use Moose;
  1         2  
  1         7  
7 1     1   6330 use Carp;
  1         4  
  1         106  
8 1     1   8 use JSON;
  1         2  
  1         10  
9 1     1   167 use LWP;
  1         3  
  1         32  
10 1     1   7 use Moose::Util::TypeConstraints;
  1         10  
  1         11  
11              
12             my $abs_int = subtype as 'Int', where { $_ >= 0 };
13              
14             our $VERSION = '0.05';
15             $VERSION = eval $VERSION;
16              
17             my %cache;
18              
19             has 'key' => (
20             is => 'ro',
21             isa => 'Str',
22             required => 1,
23             );
24              
25             has 'timeout' => (
26             is => 'ro',
27             isa => $abs_int,
28             default => 30,
29             );
30              
31             has 'user_agent' => (
32             is => 'ro',
33             isa => 'Str',
34             default => 'WebService::VaultPress::Partner/' . $VERSION,
35             );
36              
37             has _ua => (
38             is => 'ro',
39             init_arg => undef,
40             builder => '_build_ua',
41             );
42              
43 1     1   2195 no Moose;
  1         2  
  1         5  
44              
45             # CamelCase linking to Perly methods.
46 0     0 1   sub CreateGoldenTicket { shift->create_golden_ticket(@_) }
47 0     0 1   sub GetUsage { shift->get_usage(@_) }
48 0     0 1   sub GetHistory { shift->get_history(@_) }
49 0     0 1   sub GetRedeemedHistory { shift->get_redeemed_history(@_) }
50              
51             sub create_golden_ticket {
52 0     0 0   my ( $self, %request ) = @_;
53              
54 0           my $req = WebService::VaultPress::Partner::Request::GoldenTicket->new( %request );
55              
56 0           my $res = $self->_ua->post(
57             $req->api,
58             {
59             key => $self->key,
60             email => $req->email,
61             fname => $req->fname,
62             lname => $req->lname,
63             },
64             );
65              
66             # Check for HTTP transaction error (timeouts, etc)
67 0           $self->_croak_on_http_error( $res );
68              
69 0           my $json = decode_json( $res->content );
70            
71             # The API tells us if the call failed.
72 0 0         die $json->{reason} unless $json->{status};
73              
74 0 0         return WebService::VaultPress::Partner::Response->new(
75             api_call => 'CreateGoldenTicket',
76             ticket => exists $json->{url} ? $json->{url} : "",
77             );
78             }
79              
80             sub get_usage {
81 0     0 0   my ( $self, %request ) = @_;
82            
83 0           my $req = WebService::VaultPress::Partner::Request::Usage->new( %request );
84            
85 0           my $res = $self->_ua->post( $req->api, { key => $self->key } );
86            
87             # Check for HTTP transaction error (timeouts, etc)
88 0           $self->_croak_on_http_error( $res );
89              
90 0           my $json = decode_json( $res->content );
91              
92             # If GetUsage has a status, the call failed.
93 0 0         die $json->{reason} if exists $json->{status};
94              
95 0 0         return WebService::VaultPress::Partner::Response->new(
    0          
    0          
96             api_call => 'GetUsage',
97             unused => exists $json->{unused} ? $json->{unused} : 0,
98             basic => exists $json->{basic} ? $json->{basic} : 0,
99             premium => exists $json->{premium} ? $json->{premium} : 0,
100              
101             );
102             }
103              
104             sub get_history {
105 0     0 0   my ( $self, %request ) = @_;
106            
107 0           my $req = WebService::VaultPress::Partner::Request::History->new( %request );
108            
109 0           my $res = $self->_ua->post(
110             $req->api,
111             {
112             key => $self->key,
113             offset => $req->offset,
114             limit => $req->limit,
115             },
116             );
117            
118             # Check for HTTP transaction error (timeouts, etc)
119 0           $self->_croak_on_http_error( $res );
120              
121 0           my $json = decode_json( $res->content );
122              
123             # If the call was successful, we should have
124             # an array ref.
125 0 0         die $json->{reason} unless ( ref $json eq 'ARRAY' );
126              
127 0           my @responses;
128 0           for my $elem ( @{$json} ) {
  0            
129 0 0         push @responses, WebService::VaultPress::Partner::Response->new(
    0          
    0          
    0          
    0          
    0          
130             api_call => 'GetHistory',
131             email => $elem->{email} ? $elem->{email} : "",
132             lname => $elem->{lname} ? $elem->{lname} : "",
133             fname => $elem->{fname} ? $elem->{fname} : "",
134             created => $elem->{created_on} ? $elem->{created_on} : "",
135             redeemed => $elem->{redeemed_on} ? $elem->{redeemed_on} : "",
136             type => $elem->{type} ? $elem->{type} : "",
137             );
138             }
139 0           return @responses;
140             }
141              
142             # This isn't in the spec, but it will be very useful in some reports,
143             # and it's on line of code.
144             sub get_redeemed_history {
145 0     0 0   return grep { $_->redeemed ne '0000-00-00 00:00:00' } shift->GetHistory(@_);
  0            
146             }
147              
148             sub _build_ua {
149 0     0     my ( $self ) = @_;
150 0           return LWP::UserAgent->new(
151             agent => $self->user_agent,
152             timeout => $self->timeout,
153             );
154             }
155              
156             sub _croak_on_http_error {
157 0     0     my ( $self, $res ) = @_;
158              
159 0 0         croak $res->status_line unless $res->is_success;
160 0           return 0;
161             }
162              
163             __PACKAGE__->meta->make_immutable;
164              
165             1;
166              
167              
168             __END__
169              
170             =head1 NAME
171              
172             WebService::VaultPress::Partner - The VaultPress Partner API Client
173              
174             =head1 VERSION
175              
176             version 0.05
177              
178             =head1 SYNOPSIS
179              
180             #!/usr/bin/perl
181             use warnings;
182             use strict;
183             use lib 'lib';
184             use WebService::VaultPress::Partner;
185            
186            
187             my $vp = WebService::VaultPress::Partner->new(
188             key => 'Your API Key Goes Here',
189             );
190            
191             my $result = eval { $vp->GetUsage };
192            
193             if ( $@ ) {
194             print "->GetUsage had an error: $@";
195             } else {
196             printf( "%7s => %5d\n", $_, $result->$_ ) for qw/ unused basic premium /;
197             }
198            
199             my @results = $vp->GetHistory;
200            
201             if ( $@ ) {
202             print "->GetHistory had an error: $@";
203             } else {
204             for my $res ( @results ) {
205             printf( "| %-20s | %-20s | %-30s | %-19s | %-19s | %-7s |\n", $res->fname,
206             $res->lname, $res->email, $res->created, $res->redeemed, $res->type );
207             }
208             }
209            
210             # Give Alan Shore a 'Golden Ticket' to VaultPress
211            
212             my $ticket = eval { $vp->CreateGoldenTicket(
213             fname => 'Alan',
214             lname => 'Shore',
215             email => 'alan.shore@gmail.com',
216             ); };
217            
218             if ( $@ ) {
219             print "->CreateGoldenTicket had an error: $@";
220             } else {
221             print "You can sign up for your VaultPress account <a href=\""
222             . $ticket->ticket ."\">Here!</a>\n";
223             }
224              
225             =head1 DESCRIPTION
226              
227             WebService::VaultPress::Partner is a set of Perl modules that provide a simple and
228             consistent Client API to the VaultPress Partner API. The main focus of
229             the library is to provide classes and functions that allow you to quickly
230             access VaultPress from Perl applications.
231              
232             The modules consist of the WebService::VaultPress::Partner module itself as well as a
233             handful of WebService::VaultPress::Partner::Request modules as well as a response object,
234             WebService::VaultPress::Partner::Response, that provides consistent error and success
235             methods.
236              
237             Error handling is done by die'ing when we run into problems. Use your favorite
238             exception handling style to grab errors.
239              
240             =head1 METHODS
241              
242             =head2 Constructor
243              
244             WebService::VaultPress::Partner->new(
245             timeout => 30,
246             user_agent => "CoolClient/1.0",
247             key => "i have a vaultpress key",
248             );
249              
250             The constructor takes the following input:
251              
252             =over 4
253              
254             =item key
255              
256             Your API key provided by VaultPress. Required.
257              
258             =item timeout
259              
260             The HTTP Timeout for all API requests in seconds. Default: 30
261              
262             =item user_agent
263              
264             The HTTP user-agent for the API to use. Default: "WebService::VaultPress::Partner/<Version>"
265              
266             =back
267              
268             The constructor returns a WebService::VaultPress::Partner object.
269              
270             =head2 CreateGoldenTicket
271              
272             The CreateGoldenTicket method provides an interface for creating signup
273             URLs for VaultPress.
274              
275             $ticket = eval { $vp->CreateGoldenTicket(
276             api => "https://partner-api.vaultpress.com/gtm/1.0/",
277             email => "alan.shore@gmail.com",
278             fname => "Alan",
279             lname => "Shore",
280             ); };
281              
282             =over 4
283              
284             =item INPUT
285              
286             =over 4
287              
288             =item api
289              
290             The URL to send the request to. Default: https://partner-api.vaultpress.com/gtm/1.0/
291              
292             =item email
293              
294             The email address of the user you are creating the golden ticket for.
295              
296             =item fname
297              
298             The first name of the user you are creating the golden ticket for.
299              
300             =item lname
301              
302             The lastname of the user you are creating the golden ticket for.
303              
304             =back
305              
306             =item OUTPUT
307              
308             The CreateGoldenTicket method returns a WebService::VaultPress::Partner::Response
309             object with the following methods:
310              
311             =over 4
312              
313             =item api_call
314              
315             The method called to generate the response. In this case 'CreateGoldenTicket'.
316              
317             =item ticket
318              
319             The URL for the user to redeem their golden ticket is set here.
320              
321             =back
322              
323             =back
324              
325             =head2 GetHistory
326              
327             The GetHistory method provides a detailed list of Golden Tickets that
328             have been given out, while letting you know if they have been redeemed
329             and what kind of a plan the user signed up for as well as other related
330             information.
331              
332             =over 4
333              
334             =item INPUT
335              
336             =over 4
337              
338             =item api
339              
340             The URL to send the request to. Default: https://partner-api.vaultpress.com/gtm/1.0/usage
341              
342             =item limit
343              
344             The number of results to return, between 1 and 500 inclusive. Default: 100
345              
346             =item offset
347              
348             The number of results to offset by. Default: 0
349              
350             An offset of 100 with a limit of 100 will return the 101th to 200th result.
351              
352             =back
353              
354              
355             =item OUTPUT
356              
357             This method returns an array of WebService::VaultPress::Partner::Response objects.
358              
359             The following will be set:
360              
361             =over 4
362              
363             =item api_call
364              
365             This will be set to 'GetHistory'
366              
367             =item email
368              
369             The email address of the user in this history item.
370              
371             =item lname
372              
373             The last name of the user in this history item.
374              
375             =item fname
376              
377             The first name of the user in this history item.
378              
379             =item created
380              
381             The time and date that a Golden Ticket was created for this history
382             item reported in the form of 'YYYY-MM-DD HH-MM-SS'.
383              
384             =item redeemed
385              
386             The time and date that a Golden Ticket was redeemed for this history
387             item, reported in the form of 'YYYY-MM-DD HH:MM:SS'.
388              
389             When a history item reflects that this Golden Ticket has not been redeemed
390             this will be set to '0000-00-00 00:00:00'
391              
392             =item type
393              
394             The type of account that the user signed up for. One of the following:
395             basic, premium.
396              
397             When a history item reflects that this Golden Ticket has not been redeemed
398             this will be set to "".
399              
400             =back
401              
402             =back
403              
404             =head2 GetRedeemedHistory
405              
406             This method operates exactly as GetHistory, except the returned
407             history items are guaranteed to have been redeemed. See GetHistory
408             for documentation on using this method.
409              
410             =head2 GetUsage
411              
412             This method provides a general overview of issued and redeemed Golden
413             Tickets by giving you the amounts issues, redeemed and the types of redeemd
414             tickets.
415              
416             =over 4
417              
418             =item INPUT
419              
420             =over 4
421              
422             =item api
423              
424             The URL to send the request to. Default: https://partner-api.vaultpress.com/gtm/1.0/summary
425              
426             =back
427              
428              
429             =item OUTPUT
430              
431             =over 4
432              
433             =item api_call
434              
435             This will be set to 'GetUsage'.
436              
437             =item unused
438              
439             The number of GoldenTickets issued which have not been redeemed. If no tickets
440             have been issues or all tickets issues have been redeemed this will be 0.
441              
442             =item basic
443              
444             The number of GoldenTickets issued which have been redeemed with the user signing
445             up for 'basic' type service. If no tickets have met this condition the value will
446             be 0.
447              
448             =item premium
449              
450             The number of GoldenTickets issued which have been redeemed with the user signing
451             up for 'premium' type service. If no tickets have met this condition the value will
452             be 0.
453              
454             =back
455              
456             =back
457              
458             =head1 AUTHOR
459              
460             SymKat I<E<lt>symkat@symkat.comE<gt>>
461              
462             =head1 COPYRIGHT AND LICENSE
463              
464             This is free software licensed under a I<BSD-Style> License. Please see the
465             LICENSE file included in this package for more detailed information.
466              
467             =head1 AVAILABILITY
468              
469             The latest version of this software is available through GitHub at
470             https://github.com/mediatemple/webservice-vaultpress-partner/
471              
472             =cut