| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package AnyEvent::Digg::Stream; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 24517 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 1022 | use AnyEvent::HTTP; | 
|  | 1 |  |  |  |  | 62455 |  | 
|  | 1 |  |  |  |  | 117 |  | 
| 7 | 1 |  |  | 1 |  | 14 | use Carp qw(croak); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 50 |  | 
| 8 | 1 |  |  | 1 |  | 1428 | use JSON; | 
|  | 1 |  |  |  |  | 24721 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 9 | 1 |  |  | 1 |  | 1101 | use URI; | 
|  | 1 |  |  |  |  | 5157 |  | 
|  | 1 |  |  |  |  | 588 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 12 |  |  |  |  |  |  | $VERSION = eval $VERSION; | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub new { | 
| 15 | 0 |  |  | 0 | 1 |  | my ($class, %params) = @_; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 0 |  | 0 | 0 |  |  | my $on_error      = $params{on_error}      || sub { croak @_ }; | 
|  | 0 |  |  |  |  |  |  | 
| 18 | 0 |  | 0 | 0 |  |  | my $on_disconnect = $params{on_disconnect} || sub { croak 'Disconnected' }; | 
|  | 0 |  |  |  |  |  |  | 
| 19 | 0 |  | 0 | 0 |  |  | my $on_event      = $params{on_event}      || sub { }; | 
|  | 0 |  |  |  |  |  |  | 
| 20 | 0 |  | 0 | 0 |  |  | my $on_comment    = $params{on_comment}    || sub { }; | 
|  | 0 |  |  |  |  |  |  | 
| 21 | 0 |  | 0 | 0 |  |  | my $on_digg       = $params{on_digg}       || sub { }; | 
|  | 0 |  |  |  |  |  |  | 
| 22 | 0 |  | 0 | 0 |  |  | my $on_submission = $params{on_submission} || sub { }; | 
|  | 0 |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 0 |  |  |  |  |  | my %events = ( | 
| 25 |  |  |  |  |  |  | digg       => $on_digg, | 
| 26 |  |  |  |  |  |  | comment    => $on_comment, | 
| 27 |  |  |  |  |  |  | submission => $on_submission, | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 0 |  |  |  |  |  | my @events; | 
| 31 | 0 |  |  |  |  |  | for my $event (keys %{{map { $_=>1 } @{$params{events}} }}) { | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 32 | 0 | 0 |  |  |  |  | $on_error->("Unknown event type: $event\n") unless $events{$event}; | 
| 33 | 0 |  |  |  |  |  | push @events, $event; | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 0 |  |  |  |  |  | my $uri = URI->new('http://services.digg.com/2.0/stream'); | 
| 37 | 0 | 0 |  |  |  |  | $uri->query_form( | 
|  |  | 0 |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | format => 'json', | 
| 39 |  |  |  |  |  |  | 3 == @events ? () : @events ? (types => join ',', @events) : (), | 
| 40 |  |  |  |  |  |  | ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 |  |  |  |  |  | my $json = JSON->new->utf8; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | my $conn = http_request( | 
| 45 |  |  |  |  |  |  | GET     => $uri, | 
| 46 |  |  |  |  |  |  | headers => { | 
| 47 |  |  |  |  |  |  | 'User-Agent' => __PACKAGE__ . "/$VERSION", | 
| 48 |  |  |  |  |  |  | Referer      => undef, | 
| 49 |  |  |  |  |  |  | }, | 
| 50 |  |  |  |  |  |  | on_header => sub { | 
| 51 | 0 |  |  | 0 |  |  | my ($headers) = @_; | 
| 52 | 0 | 0 |  |  |  |  | if ('200' ne $headers->{Status}) { | 
| 53 | 0 |  |  |  |  |  | $on_error->("$headers->{Status}: $headers->{Reason}"); | 
| 54 | 0 |  |  |  |  |  | return; | 
| 55 |  |  |  |  |  |  | } | 
| 56 | 0 | 0 |  |  |  |  | if ('application/json' ne $headers->{'content-type'}) { | 
| 57 | 0 |  |  |  |  |  | $on_error->( | 
| 58 |  |  |  |  |  |  | "Unexpected Content-Type: $headers->{'content-type'}" | 
| 59 |  |  |  |  |  |  | ); | 
| 60 | 0 |  |  |  |  |  | return; | 
| 61 |  |  |  |  |  |  | } | 
| 62 | 0 |  |  |  |  |  | return 1; | 
| 63 |  |  |  |  |  |  | }, | 
| 64 |  |  |  |  |  |  | on_body => sub { | 
| 65 | 0 |  |  | 0 |  |  | my ($content) = @_; | 
| 66 | 0 | 0 |  |  |  |  | if (my $data = eval { $json->incr_parse($content) }) { | 
|  | 0 | 0 |  |  |  |  |  | 
| 67 | 0 |  |  |  |  |  | $on_event->($data); | 
| 68 | 0 |  | 0 |  |  |  | ($events{$data->{type}} || sub {})->($data); | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | elsif ($@) { | 
| 71 | 0 |  |  |  |  |  | $json->incr_skip; | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 0 |  |  |  |  |  | return 1; | 
| 74 |  |  |  |  |  |  | }, | 
| 75 | 0 |  |  | 0 |  |  | sub { $on_disconnect->() }, | 
| 76 | 0 |  |  |  |  |  | ); | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 0 |  |  |  |  |  | return $conn; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | 1; | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | __END__ |