File Coverage

blib/lib/Net/CronIO.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1 1     1   27543 use strict;
  1         2  
  1         36  
2 1     1   4 use warnings;
  1         2  
  1         50  
3             package Net::CronIO;
4             {
5             $Net::CronIO::VERSION = '0.01';
6             }
7              
8             # ABSTRACT: Perl binding for cron.io
9              
10 1     1   431 use Net::HTTP::API;
  0            
  0            
11             use Carp qw(croak);
12              
13              
14             net_api_declare cronio => (
15             api_base_url => 'http://api.cron.io/v1',
16             api_format => 'json',
17             api_format_mode => 'content-type',
18             authentication => 1,
19             );
20              
21              
22             net_api_method create_user => (
23             description => "Sign up for service and get a username/password",
24             method => "POST",
25             path => '/users',
26             params => [qw(email username password)],
27             required => [qw(email username password)],
28             expected => [qw(201)],
29             );
30              
31              
32             net_api_method get_all_crons => (
33             description => "List all the crons you have. Requires authentication.",
34             method => "GET",
35             path => '/crons',
36             authentication => 1,
37             );
38              
39              
40             net_api_method create_cron => (
41             description => "Create a new cron entry for your account. Requires authentication.",
42             method => "POST",
43             path => '/crons',
44             params => [qw(name url schedule)],
45             required => [qw(name url schedule)],
46             authentication => 1,
47             );
48              
49              
50             net_api_method get_cron => (
51             description => "List a specific cron entry. Requires a cron id and authentication.",
52             method => "GET",
53             path => '/crons/:id',
54             params => [qw(id)],
55             required => [qw(id)],
56             authentication => 1,
57             );
58              
59              
60             net_api_method update_cron => (
61             description => "Update a cron entry. Requires cron id and authentication.",
62             method => "PUT",
63             path => '/crons/:id',
64             params => [qw(id name url schedule)],
65             required => [qw(id)],
66             authentication => 1,
67             );
68              
69              
70             net_api_method delete_cron => (
71             descripton => "Delete a cron entry. Requires cron id and authentication.",
72             method => "DELETE",
73             path => '/crons/:id',
74             params => [qw(id)],
75             required => [qw(id)],
76             expected => [qw(204)],
77             authentication => 1,
78             );
79              
80             around 'delete_cron' => sub {
81             my $orig = shift;
82             my $self = shift;
83              
84             my ($content, $response) = $self->$orig(@_);
85              
86             if ( $response->is_success ) {
87             return 1;
88             }
89             else {
90             croak $response->status_line;
91             }
92             };
93              
94             around 'create_user' => sub {
95             my $orig = shift;
96             my $self = shift;
97              
98             my ($content, $response) = $self->$orig(@_);
99              
100             if ( $response->is_success ) {
101             return $content->{'message'};
102             }
103             else {
104             croak $response->status_line . ": " . $response->content;
105             }
106             };
107              
108             1;
109              
110              
111             __END__
112             =pod
113              
114             =head1 NAME
115              
116             Net::CronIO - Perl binding for cron.io
117              
118             =head1 VERSION
119              
120             version 0.01
121              
122             =head1 SYNOPSIS
123              
124             use 5.014;
125             use Net::CronIO;
126              
127             my $cron = Net::CronIO->new(
128             api_username => 'example',
129             api_password => 'sekrit',
130             );
131              
132             my $newjob = $cron->create_cron(
133             name => 'Daily clean up job',
134             url => 'http://example.com/blahblah/?cleanup=1',
135             schedule => '46 0 * * *',
136             );
137              
138             my $jobs = $cron->get_all_crons();
139              
140             foreach my $job ( $jobs ) {
141             if ( $job->{'url'} =~ /deadhost.com/ ) {
142             $cron->update_cron(
143             id => $job->{'id'},
144             url => $job->{'url'} =~ s/deadhost\.com/example\.com/r,
145             );
146             }
147             }
148            
149             say "deleted" if ( $cron->delete_cron( id => $jobs->[0]->{'id'} ) );
150              
151             This is a Perl binding for the L<cron.io|http://cron.io> service. Cron is a Unix service which
152             executes jobs on a periodic basis. The cron.io service contacts URLs using the same
153             time period specification.
154              
155             At the moment, the only way to generate a username and password for the service is by making
156             a call on the C<create_user()> method. Email verification is required before the
157             credentials are valid.
158              
159             =head1 ATTRIBUTES
160              
161             =head2 api_username
162              
163             You must supply an C<api_username> for every method except C<create_user()>. This can be done at
164             object construction time, or later by calling the C<api_username()> method.
165              
166             =head2 api_password
167              
168             You must supply an C<api_password> for every method except C<create_user()>. This can be done at
169             object construction time, or later by calling the C<api_username()> method.
170              
171             =head1 METHODS
172              
173             =head2 create_user()
174              
175             This method requires the following parameters: C<email>, C<username>, C<password>. This call will register
176             a username/password with the service. Human intervention (in the form of an email verification) is required
177             before your username/password are activated.
178              
179             Once these credentials are active, you must provide them to this binding to execute other methods.
180              
181             The return value is a string message provided by the API service (which evaluates to
182             a true value in Perl.) This method dies on errors.
183              
184             =head2 get_all_crons()
185              
186             This method returns an arrayref containing hashes of all cron jobs for your username.
187              
188             Hashes will contain the following keys:
189              
190             =over
191              
192             =item * C<id>
193              
194             This is an internal ID used by the service to identify a specific cron job. It is a required
195             parameter for most of the other methods.
196              
197             =item * C<name>
198              
199             This is the name you assigned to a specific job.
200              
201             =item * C<url>
202              
203             The URL to contact at the given schedule specification.
204              
205             =item * C<schedule>
206              
207             This is a standard Unix cron style specification. See L<cron|http://en.wikipedia.com/wiki/Cron> on Wikipedia
208             for a verbose description of this format.
209              
210             =back
211              
212             It is possible this call will return a reference to an empty list.
213              
214             =head2 create_cron()
215              
216             This method creates a new job. Required parameters are C<name>, C<url>, C<schedule>.
217              
218             The return value is a hash of C<id>, plus the three params you provided.
219              
220             =head2 get_cron()
221              
222             This method retrieves a specific job by its C<id>. C<id> is a required parameter for this
223             method. The return value is a hash as described above.
224              
225             =head2 update_cron()
226              
227             This method changes values with jobs which already have ids. C<id> is a required parameter.
228             Optional parameters are any or all of C<name>, C<url>, and/or C<schedule>.
229              
230             =head2 delete_cron()
231              
232             This method removes a job from the service. C<id> is a required parameter. On success, this
233             method returns a true value. It dies on errors.
234              
235             =head1 TESTING NOTE
236              
237             To execute the full test suite, you must set CRONIO_API_USERNAME and CRONIO_API_PASSWORD environment
238             variables with valid credentials.
239              
240             =head1 AUTHOR
241              
242             Mark Allen <mrallen1@yahoo.com>
243              
244             =head1 COPYRIGHT AND LICENSE
245              
246             This software is copyright (c) 2012 by Mark Allen.
247              
248             This is free software; you can redistribute it and/or modify it under
249             the same terms as the Perl 5 programming language system itself.
250              
251             =cut
252