Gnip.pm

filter: Statementstmt 18.7% 29 / 155 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 29.0% 9 / 31 Podpod 100% 19 / 19 totaltotal 22.0% 57 / 259 CC 24 SCAR 31.6
CC
24
Cov
14%
fetch
90.0
_filter_method
30.0
_do_request
30.0
CRAP 23.6
↑ Summary Gnip/Filter.pm »
1 package Net::Gnip;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Net::Gnip::ActivityStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Net::Gnip::FilterStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Net::Gnip::PublisherStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = "0.3";
14  
15 4 ▶use constant GNIP_BASE_ADDRESS => 'prod.gnipcentral.com';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use constant GNIP_BASE_URL => 'https://'.GNIP_BASE_ADDRESS;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 Net::Gnip - interact with Gnip
21  
22 =head1 SYNOPSIS
23  
24 use Net::Gnip;
25 my $gnip = Net::Gnip->new($user, $pass, $publisher_name);
26  
27 # Get a list of publisher
28 my @publisher = $gnip->publishers();
29 $gnip->create_publisher($name);
30  
31  
32 # Create an activity and publish it
33 my $activity = Net::Gnip::Activity->new('added_friend', 'me');
34 $gnip->publish($publisher_name, $activity);
35 # or
36 $gnip->publish($publisher_name, @activities);
37 # or
38 $gnip->publish($activity_stream);
39  
40 # Retrieve activities for a given publisher
41 my $stream = $gnip->fetch(notification => $publisher_name);
42 foreach my $activity ($stream->activities) {
43 print "Type=".$activity->type."\n";
44 }
45  
46 # Retrieve activities for a given publisher 10 minutes ago
47 my $stream = $gnip->fetch(notification => $publisher_name, time => 10*60);
48  
49 # Retrieve activities for a given publisher from a specific time
50 my $stream = $gnip->fetch(notification => $publisher_name, time => $dt);
51  
52 # The same but get the full date
53 my $stream = $gnip->fetch(activity => $publisher_name, time => $dt);
54  
55  
56 # Create a filter and
57 my @rules = ( { type => 'actor', value => 'joe' } );
58 my $filter = Net::Gnip::Filter->new('my_filter', 'true', [@rules]);
59 my $result = $gnip->create_filter($publisher_name, $filter);
60  
61 # Get a list of filters
62 foreach my $filters ($gnip->filters($publisher_name)) {
63 print $filter->name."\n";
64 }
65  
66
67 # Get the activities from it
68 my $stream = $gnip->fetch(activity => $publisher_name, filter => $filter_name);
69 foreach my $activity ($stream->activities) {
70 print "Type=".$activity->type."\n";
71 }
72
73 # Update it
74 $filter->full('false');
75 $gnip->update_filter($publisher_name, $filter);
76  
77 $gnip->add_filter_rule($filter, 'actor', 'simon');
78 $gnip->delete_filter_rule($filter, 'actor', 'simon')
79  
80
81 # Delete it
82 $gnip->delete_filter($publisher_name, $filter);
83  
84  
85  
86 =head2 METHODS
87  
88 =cut
89  
90 =head2 new <username> <password>
91  
92 Create a new Gnip object
93  
94 =cut
95  
96 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
97 0 my ($class, $username, $password, %self) = @_;
98 0 $self{'_username'} = $username;
99 0 $self{'_password'} = $password;
100 0 return bless {%self}, $class;
101 }
102  
103  
104  
105 =head2 fetch <publisher> [datetime])
106  
107 Gets all of the data for a specific publisher, based on the
108 datetime parameter.
109  
110 If datetime is not passed in, the current time will be used.
111  
112 Note that all times need to be as UTC DateTime objects.
113  
114 Returns a C<Net::Gnip::ActivityStream> object.
115  
116 =cut
117  
118 0 ▶sub fetch {
Subroutine fetch: not called CC=9 SCAR=45.0
Pod documented
119 0 my $self = shift;
120 0 my %opts = @_;
121  
122 0 my $what;
123 my $name;
124  
125 0 for my $key (qw(notification activity)) {
126 0 ▶ next unless defined $opts{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{$key} 0 0 0
127 0 $what = $key;
128 0 $name = $opts{$key};
129 }
130 0 ▶ die "You must must pass in either a 'notification' or 'activity' key with a publisher name"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $what 0 0 0
131 unless defined $what;
132  
133
134 0 my $path = "publishers/${name}";
135 0 ▶ if (defined $opts{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"filter"}) 0 0 0
136 0 $path .= "/filters/".$opts{filter};
137 }
138 0 $path .= "/${what}";
139  
140 0 my $datetime = $opts{time};
141 0 my $file = "current.xml";
142  
143 0 ▶ if (defined $datetime) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $datetime) 0 0 0
144 0 ▶ $datetime = DateTime->now->subtract( seconds => $datetime) unless ref($datetime);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $datetime 0 0 0
145 0 ▶ unless ($self->{no_time_sync}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"no_time_sync"}) 0 0 0
146 0 my $gnip_time = $self->sync_with_gnip_clock;
147 0 my $local_time = DateTime->now;
148 0 $datetime = $datetime + ($gnip_time - $local_time);
149 }
150 0 my $rounded = $self->round_time_to_nearest_bucket($datetime);
151 0 my $time_string = $self->time_to_string($rounded);
152 0 $file = "${time_string}.xml";
153 }
154 0 my $url = $self->GNIP_BASE_URL."/".$path."/".$file;
155 0 ▶ my $xml = $self->get($url) || return;
Condition0%0/2
$self->get($url) || (return)
l!l
00
MC/DC0%0/1
$self->get($url) || (return)
$self->get($url)
Truth table0%0/2
A
A= $self->get($url)
Aresult
11
00
156 0 return Net::Gnip::ActivityStream->parse($xml, _no_dt => $self->{_no_dt});
157 }
158  
159 =head2 publish <activity stream>
160  
161 Publish an activity stream.
162  
163 =cut
164  
165 =head2 publish <publisher name> <activity[s]>
166  
167 =cut
168 0 ▶sub publish {
Subroutine publish: not called CC=4 SCAR=30.0
Pod documented
169 0 my $self = shift;
170 0 my $publisher = shift;
171 0 my $stream;
172 0 ▶ if (ref $publisher) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $publisher) 0 0 0
173 0 $stream = $publisher;
174 0 $publisher = $stream->publisher;
175 0 ▶ die "An activity stream must have a publisher set if it's the only argument to publish()"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $publisher 0 0 0
176 unless defined $publisher;
177 }
178 0 ▶ die "You need to pass in some activities" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
179 0 $stream = Net::Gnip::ActivityStream->new( publisher => $publisher);
180 0 $stream->activities(@_);
181 0 my $url = $self->GNIP_BASE_URL."/publishers/" . $publisher . "/activity";
182 0 $self->post($url, $stream);
183 }
184  
185  
186  
187 =head2 filters <publisher name>
188  
189 Get a list of filters for this publisher.
190  
191 Returns a list of C<Net::Gnip::Filter> objects.
192  
193 =cut
194 0 ▶sub filters {
Subroutine filters: not called CC=2 SCAR=17.9
Pod documented
195 0 my $self = shift;
196 0 my $publisher = shift;
197 0 my $url = $self->GNIP_BASE_URL."/publishers/$publisher/filters.xml";
198 0 ▶ my $xml = $self->get($url) || return;
Condition0%0/2
$self->get($url) || (return)
l!l
00
MC/DC0%0/1
$self->get($url) || (return)
$self->get($url)
Truth table0%0/2
A
A= $self->get($url)
Aresult
11
00
199 0 return Net::Gnip::FilterStream->parse($xml)->filters;
200 }
201  
202  
203 =head2 create_filter <publisher name> <filter>
204  
205 Creates a new filter.
206  
207 =cut
208 0 ▶sub create_filter {
Subroutine create_filter: not called CC=1 SCAR=6.9
Pod documented
209 0 my ($self, $publisher, $filter) = @_;
210 0 my $url = $self->GNIP_BASE_URL."/publishers/$publisher/filters.xml";
211 0 return $self->post($url, $filter);
212 }
213  
214 =head2 get_filter <publisher name> <filter>
215  
216 Fetches an existing filter.
217  
218 C<filter> can either be a filter object or a name.
219  
220 Returns a C<Net::Gnip::Filter> object.
221  
222 =cut
223 0 ▶sub get_filter {
Subroutine get_filter: not called CC=2 SCAR=17.9
Pod documented
224 0 my $self = shift;
225 0 ▶ my $xml = $self->_filter_method('get', @_) || return;
Condition0%0/2
$self->_filter_method('get', @_) || (return)
l!l
00
MC/DC0%0/1
$self->_filter_method('get', @_) || (return)
$self->_filter_method('get', @_)
Truth table0%0/2
A
A= $self->_filter_method('get', @_)
Aresult
11
00
226 0 return Net::Gnip::Filter->parse($xml);
227 }
228  
229 =head2 update_filter <publisher name> <filter>
230  
231 Updates an existing filter.
232  
233 C<filter> can either be a filter object or a name.
234  
235 Returns 1 on success and 0 on failure.
236  
237 =cut
238 0 ▶sub update_filter {
Subroutine update_filter: not called CC=1 SCAR=6.9
Pod documented
239 0 my $self = shift;
240 0 return $self->_filter_method('put', @_);
241 }
242  
243 =head2 delete_filter <publisher name> <filter>
244  
245 Deletes an existing filter.
246  
247 C<filter> can either be a filter object or a name.
248  
249 =cut
250 0 ▶sub delete_filter {
Subroutine delete_filter: not called CC=1 SCAR=6.9
Pod documented
251 0 my $self = shift;
252 0 return $self->_filter_method('delete', @_);
253 }
254  
255 =head2 add_filter_rule <publisher> <filter> <type> <value>
256  
257 Incrementally add a filter rule.
258  
259 =cut
260 0 ▶sub add_filter_rule {
Subroutine add_filter_rule: not called CC=1 SCAR=6.9
Pod documented
261 0 my $self = shift;
262 0 $self->_filter_method('post', @_);
263 }
264  
265 =head2 delete_filter_rule <publisher> <filter> <type> <value>
266  
267 Incrementally delete a filter rule.
268  
269 =cut
270 0 ▶sub delete_filter_rule {
Subroutine delete_filter_rule: not called CC=1 SCAR=6.9
Pod documented
271 0 my $self = shift;
272 0 $self->_filter_method('delete', @_);
273 }
274  
275 0 ▶sub _filter_method {
Subroutine _filter_method: not called CC=5 SCAR=34.0
276 0 my $self = shift;
277 0 my $method = shift;
278 0 my $publisher = shift;
279 0 my $filter = shift;
280 0 my $type = shift;
281 0 my $value = shift;
282 0 ▶ my $name = (ref($filter))? $filter->name : $filter;
Branch: 1 branch
BranchTrueFalseTotal
ref $filter ? : 0 0 0
283 0 my $url = $self->GNIP_BASE_URL."/publishers/$publisher/filters/$name";
284 0 ▶ if (defined $type && defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and defined $value) { } 0 0 0
Condition0%0/3
defined $type and defined $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and defined $value
defined $type defined $value
Truth table0%0/3
A and B
A= defined $typeB= defined $value
ABresult
0X0
100
111
285 0 $url .= "/rules.xml?type=${type}&value=${value}";
286 0 ▶ if ($method eq 'post') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "post") 0 0 0
287 0 $filter = Net::Gnip::Filter->_create_rule({ type => $type, value => $value })->toString(1);
288 }
289 } else {
290 0 $url .= ".xml";
291 }
292 0 return $self->$method($url, $filter);
293 }
294  
295  
296 =head2 publishers
297  
298 Gets a list of publishers on the system
299  
300 =cut
301 0 ▶sub publishers {
Subroutine publishers: not called CC=2 SCAR=17.9
Pod documented
302 0 my ($self) = @_;
303  
304 0 my $url = $self->GNIP_BASE_URL."/publishers.xml";
305  
306 0 ▶ my $xml = $self->get($url) || return;
Condition0%0/2
$self->get($url) || (return)
l!l
00
MC/DC0%0/1
$self->get($url) || (return)
$self->get($url)
Truth table0%0/2
A
A= $self->get($url)
Aresult
11
00
307 0 return map { $_->name } Net::Gnip::PublisherStream->parse($xml)->publishers;
308 }
309  
310  
311  
312 =head2 create_publisher <publisher[s]>
313  
314 Takes one or more C<Net::Gnip::Publisher> objects and creates them.
315  
316 Returns 1 on success and undef on failure (and sets C<$@>)
317  
318 =cut
319 0 ▶sub create_publisher {
Subroutine create_publisher: not called CC=1 SCAR=6.9
Pod documented
320 0 my $self = shift;
321 0 my $name = shift;
322 0 my $stream = Net::Gnip::PublisherStream->new(children => [ @_ ] );
323 0 my $url = $self->GNIP_BASE_URL."/publishers.xml";
324 0 return $self->post($url, $stream);
325  
326 }
327  
328 =head2 get <url>
329  
330 Does an HTTP GET request of the passed in C<url>, and returns
331 the result from the server.
332  
333 Returns undef on failure (and sets C<$@>)
334  
335 =cut
336  
337 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
338 0 my $self = shift;
339 0 return $self->_do_http('GET', @_);
340 }
341  
342 =head2 post <url> <data object>
343  
344 Does a HTTP POST request of the passed in url and data object, and returns
345 the result from the server.
346  
347 Returns undef on failure (and sets C<$@>)
348  
349 =cut
350  
351 0 ▶sub post {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
352 0 my $self = shift;
353 0 return $self->_do_http('POST', @_);
354 }
355  
356 0 ▶sub _do_http {
Subroutine _do_http: not called CC=2 SCAR=17.9
357 0 my $self = shift;
358 0 my $type = shift;
359 0 my $url = shift;
360 0 my $response = $self->_do_request(1, $type, $url, @_);
361 # Check the outcome of the response
362 0 ▶ if ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
363 0 return $response->content;
364 } else {
365 0 $@ = "Failed to $type $url ".$response->status_line."\n\n".$response->as_string;
366 0 return;
367 }
368 }
369  
370 0 ▶sub _do_request {
Subroutine _do_request: not called CC=5 SCAR=34.0
371 0 my $self = shift;
372 0 my ($auth, $type, $url, $data) = @_;
373 0 ▶ my $agent = $self->{_agent} ||= LWP::UserAgent->new;
Condition0%0/3
$self->{"_agent"} ||= "LWP::UserAgent"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_agent"} ||= "LWP::UserAgent"->new
$self->{"_agent"} "LWP::UserAgent"->new
Truth table0%0/3
A ||= B
A= $self->{"_agent"}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
374 # Load proxy settings from *_proxy environment variables
375 0 $agent->env_proxy;
376 0 $agent->agent(__PACKAGE__."-".$VERSION);
377 0 my $request = HTTP::Request->new($type => $url);
378 0 ▶ $request->authorization_basic($self->{_username}, $self->{_password}) if $auth;
Branch: 1 branch
BranchTrueFalseTotal
if $auth 0 0 0
379 0 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 0 0 0
380 0 $request->content_type('application/xml');
381 0 ▶ $request->content((ref $data)?$data->as_xml:$data);
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 0 0 0
382 }
383 0 return $agent->request($request);
384 }
385  
386  
387 =head2 put <url> <data>
388  
389 Does an HTTP PUT request of the passed in url and data object, and returns
390 the result from the server.
391  
392 Returns undef on failure (and sets C<$@>)
393  
394 =cut
395  
396 0 ▶sub put {
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
397 0 my $self = shift;
398 0 return $self->_do_http('PUT', @_)
399 }
400  
401 =head2 delete <url>
402  
403 Does a HTTP Delete request of the passed in url and returns
404 the result from the server.
405  
406 Returns undef on failure (and sets C<$@>)
407  
408 =cut
409  
410 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
411 0 my $self = shift;
412 0 return $self->_do_http('DELETE', @_)
413 }
414  
415 =head2 round_time_to_nearest_bucket <datetime>
416  
417 Rounds the time passed in down to the previous 1 minute mark.
418  
419 Returns a new C<DateTime> object.
420  
421 =cut
422  
423 0 ▶sub round_time_to_nearest_bucket {
Subroutine round_time_to_nearest_bucket: not called CC=1 SCAR=6.9
Pod documented
424 0 my $self = shift;
425 0 my $datetime = shift->clone;
426  
427 0 my $min = $datetime->minute();
428 0 my $new = $min - ($min % 1);
429  
430 0 $datetime->set(minute => $new);
431 0 $datetime->set(second => 0);
432  
433 0 return $datetime;
434 }
435  
436 =head2 sync_with_gnip_clock <datetime>
437  
438 This method gets the current time from the Gnip server.
439  
440 Returns a new C<DateTime> object.
441  
442 =cut
443  
444 0 ▶sub sync_with_gnip_clock {
Subroutine sync_with_gnip_clock: not called CC=1 SCAR=6.9
Pod documented
445 0 my $self = shift;
446  
447 0 my $response = $self->_do_request(0, 'HEAD', GNIP_BASE_URL);
448 0 my $formatter = DateTime::Format::Strptime->new( pattern => '%a, %d %b %Y %H:%M:%S %Z' );
449 0 my $gnip_time = $formatter->parse_datetime($response->header('Date'));
450 0 return $gnip_time;
451 }
452  
453 =head2 time_to_string <datetime>
454  
455 Converts the time passed in to a string of the form YYYYMMDDHHMM.
456  
457 =cut
458  
459 0 ▶sub time_to_string {
Subroutine time_to_string: not called CC=1 SCAR=6.9
Pod documented
460 0 my ($self, $datetime) = @_;
461 0 return $datetime->strftime("%Y%m%d%H%M");
462 }
463  
464 =head1 AUTHOR
465  
466 Simon Wistow <simon@thegestalt.org>
467  
468 Based on code by ajackson from http://github.com/gnip/gnip-perl/tree/master
469  
470 =head1 COPYRIGHT
471  
472 Copyright 2008, Simon Wistow
473  
474 Release under the same terms as Perl itself.
475  
476 =cut
477 4 1;
478  
↑ Summary Gnip/Filter.pm »