File Coverage

blib/lib/WWW/TarPipe.pm
Criterion Covered Total %
statement 39 40 97.5
branch 8 14 57.1
condition n/a
subroutine 11 11 100.0
pod 3 3 100.0
total 61 68 89.7


line stmt bran cond sub pod time code
1             package WWW::TarPipe;
2              
3 2     2   62035 use warnings;
  2         5  
  2         72  
4 2     2   13 use strict;
  2         5  
  2         71  
5 2     2   10 use Carp;
  2         8  
  2         203  
6 2     2   2888 use LWP::UserAgent;
  2         148087  
  2         176  
7              
8             =head1 NAME
9              
10             WWW::TarPipe - An interface to tarpipe.com's REST based web service.
11              
12             =head1 VERSION
13              
14             This document describes WWW::TarPipe version 0.01
15              
16             =cut
17              
18             our $VERSION = '0.01';
19              
20             my @ATTR;
21              
22             BEGIN {
23 2     2   9 @ATTR = qw(
24             base_uri body image key title
25             );
26              
27 2         4 for my $attr ( @ATTR ) {
28 2     2   26 no strict 'refs';
  2         4  
  2         187  
29             *$attr = sub {
30 5     5   7 my $self = shift;
31 5 50       15 croak "$attr may not be set" if @_;
32 5         26 return $self->{$attr};
33 10         1052 };
34             }
35             }
36              
37             =head1 SYNOPSIS
38              
39             use WWW::TarPipe;
40              
41             my $tp = WWW::TarPipe->new(
42             key => '77c38f56696963fa13f5b6cd97a2556c'
43             );
44              
45             $tp->upload(
46             title => 'The outside temperature is 27C'
47             );
48            
49             =head1 DESCRIPTION
50              
51             tarpipe.com is a publishing mediation and distribution platform that
52             simplifies regular upload activities:
53              
54             =over
55              
56             =item * Publishing content to multiple Web locations;
57              
58             =item * Combining different media into a single blog post or article;
59              
60             =item * Transforming documents on-the-fly;
61              
62             =item * Managing repeatable upload actions.
63              
64             =back
65              
66             You can learn more by visiting L.
67              
68             =head1 INTERFACE
69              
70             =head2 C<< new >>
71              
72             Create a new WWW::TarPipe. Accepts a number of key, value pairs. The
73             following arguments are recognised:
74              
75             =over
76              
77             =item C<< base_uri >>
78              
79             The base URI for the tarpit REST service. Defaults to
80              
81             http://rest.receptor.tarpipe.net:8000/
82              
83             =item C<< title >>
84              
85             The title of the data being posted.
86              
87             =item C<< body >>
88              
89             A larger chunk of text associated with this post such as a blog post.
90              
91             =item C<< image >>
92              
93             A chunk of binary data - perhaps an image - for this post.
94              
95             =item C<< key >>
96              
97             The token generated when you save a REST API workflow.
98              
99             =back
100              
101             Any options not passed to C<< new >> may be passed to a subsequent call
102             to C<< upload >>, for example:
103              
104             my $tp = WWW::TarPipe->new(
105             key => '77c38f56696963fa13f5b6cd97a2556c'
106             );
107              
108             $tp->upload(
109             title => 'The outside temperature is 27C'
110             );
111              
112             is equivalent to
113              
114             my $tp = WWW::TarPipe->new;
115              
116             $tp->upload(
117             key => '77c38f56696963fa13f5b6cd97a2556c',
118             title => 'The outside temperature is 27C'
119             );
120              
121             When making multiple posts to the same workflow it is convenient to
122             supply unchanging options as arguments to C<< new >> and pass those that
123             change to C<< upload >>.
124              
125             =cut
126              
127             sub new {
128 2     2 1 503 my $class = shift;
129 2         8 return bless {
130             base_uri => $class->default_base_uri,
131             $class->_check_args( @_ )
132             }, $class;
133             }
134              
135             =head2 C<< upload >>
136              
137             Send an upload request to the tarpit.com REST service. A number of key,
138             value argument pairs should be passed. See C<< new >> above for details
139             of the arguments that can be specified.
140              
141             $tp->upload(
142             key => '77c38f56696963fa13f5b6cd97a2556c',
143             title => 'Hello, World',
144             body => "First Post!\nYay me!\n"
145             );
146              
147             If the request fails an exception will be thrown.
148              
149             =cut
150              
151             sub upload {
152 1     1 1 3 my $self = shift;
153 1         6 my %args = ( %$self, $self->_check_args( @_ ) );
154 1         18 my $ua = LWP::UserAgent->new;
155              
156 1 50       3460 my $uri = delete $args{base_uri}
157             or croak "base_uri must be supplied";
158 1 50       6 my $key = delete $args{key}
159             or croak "key must be supplied";
160              
161 1         9 my $resp = $ua->post(
162             "$uri?key=$key",
163             Content_Type => 'form-data',
164             Content => \%args
165             );
166              
167 1 50       374615 croak $resp->status_line if $resp->is_error;
168 0         0 return $resp->content;
169              
170             }
171              
172             sub _check_args {
173 3     3   5 my $self = shift;
174 3 50       12 croak "Please supply a number of key, value pairs"
175             if @_ % 1;
176 3         13 my %args = @_;
177 3         4 my %got = ();
178              
179 3         8 for my $attr ( @ATTR ) {
180 15 100       8484 $got{$attr} = delete $args{$attr}
181             if exists $args{$attr};
182             }
183              
184 3 50       12 croak "Invalid options: ", join ', ', sort keys %args if keys %args;
185 3         36 return %got;
186             }
187              
188             =head2 Accessors
189              
190             Each of the options that may be supplied to C<< new >> and C<< upload >>
191             have a corresponding read only accessor.
192              
193             =head3 C<< base_uri >>
194              
195             The base URI for the tarput service.
196              
197             =head3 C<< title >>
198              
199             The title of the post.
200              
201             =head3 C<< body >>
202              
203             The body of the post.
204              
205             =head3 C<< image >>
206              
207             Arbitrary image data.
208              
209             =head3 C<< key >>
210              
211             The REST key for the workflow.
212              
213             =head3 C<< default_base_uri >>
214              
215             Returns the default URI for the tarpipe service. May be overridden in
216             subclasses or by supplying the C<< base_uri >> option to C<< new >> or
217             C<< upload >>.
218              
219             =cut
220              
221 3     3 1 17 sub default_base_uri { 'http://rest.receptor.tarpipe.net:8000/' }
222              
223             1;
224              
225             __END__