| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::PerlWatcher::Watcher::HTTP; | 
| 2 |  |  |  |  |  |  | { | 
| 3 |  |  |  |  |  |  | $App::PerlWatcher::Watcher::HTTP::VERSION = '0.20'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  | # ABSTRACT: The base role for watching external events via HTTP | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 2 |  |  | 2 |  | 1553 | use 5.12.0; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 85 |  | 
| 8 | 2 |  |  | 2 |  | 11 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 73 |  | 
| 9 | 2 |  |  | 2 |  | 11 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 52 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 2 |  |  | 2 |  | 11 | use App::PerlWatcher::EventItem; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 42 |  | 
| 12 | 2 |  |  | 2 |  | 1914 | use AnyEvent::HTTP; | 
|  | 2 |  |  |  |  | 38502 |  | 
|  | 2 |  |  |  |  | 201 |  | 
| 13 | 2 |  |  | 2 |  | 24 | use Carp; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 113 |  | 
| 14 | 2 |  |  | 2 |  | 11 | use Smart::Comments -ENV; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 26 |  | 
| 15 | 2 |  |  | 2 |  | 3276 | use List::MoreUtils qw/any/; | 
|  | 2 |  |  |  |  | 5873 |  | 
|  | 2 |  |  |  |  | 210 |  | 
| 16 | 2 |  |  | 2 |  | 21 | use Moo::Role; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 28 |  | 
| 17 | 2 |  |  | 2 |  | 1071 | use URI; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 2294 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | requires 'url'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | requires 'process_http_response'; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | with qw/App::PerlWatcher::Watcher/; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | has 'frequency'         => ( is => 'ro', default => sub { 60; } ); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # for internal use only. No docs. | 
| 30 |  |  |  |  |  |  | has 'uri'               => ( is => 'lazy'); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | has 'timeout'           => ( is => 'lazy'); | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | has 'title'             => ( is => 'lazy'); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | has 'watcher_callback'  => ( is => 'lazy'); | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub _build_uri { | 
| 42 | 1 |  |  | 1 |  | 380 | return URI->new($_[0]->url); | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub _build_timeout { | 
| 46 | 0 |  | 0 | 0 |  | 0 | $_[0]->config->{timeout} // $_[0]->engine_config->{defaults}->{timeout} // 5; | 
|  |  |  | 0 |  |  |  |  | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub _build_title { | 
| 50 | 0 |  |  | 0 |  | 0 | $_[0]->uri->host; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub _build_watcher_callback { | 
| 54 | 1 |  |  | 1 |  | 691 | my $self = shift; | 
| 55 | 1 |  |  |  |  | 5 | my $uri = $self->uri; | 
| 56 |  |  |  |  |  |  | my $watcher = sub { | 
| 57 | 4 |  |  | 4 |  | 167 | $self->poll_callback->($self); | 
| 58 |  |  |  |  |  |  | $self -> {_guard} = http_get (scalar $uri, | 
| 59 |  |  |  |  |  |  | timeout => $self->timeout, | 
| 60 |  |  |  |  |  |  | sub { | 
| 61 | 4 |  |  |  |  | 1009844 | my ($body, $headers) = @_; | 
| 62 | 4 | 100 |  |  |  | 42 | if ($headers -> {Status} =~ /^2/) { | 
| 63 |  |  |  |  |  |  | # $body | 
| 64 | 2 |  |  |  |  | 15 | $self->process_http_response($body, $headers); | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  | else{ | 
| 67 | 2 |  |  |  |  | 10 | my $reason = $headers -> {Status}; | 
| 68 |  |  |  |  |  |  | # bad thing has happend | 
| 69 |  |  |  |  |  |  | # $reason | 
| 70 |  |  |  |  |  |  | # $self | 
| 71 |  |  |  |  |  |  | $self->interpret_result( | 
| 72 |  |  |  |  |  |  | 0, | 
| 73 |  |  |  |  |  |  | sub { | 
| 74 | 2 |  |  |  |  | 4 | my $status = shift; | 
| 75 | 2 |  |  |  |  | 18 | $self->_invoke_callback( | 
| 76 |  |  |  |  |  |  | $self->callback, | 
| 77 |  |  |  |  |  |  | $status | 
| 78 |  |  |  |  |  |  | ); | 
| 79 |  |  |  |  |  |  | } | 
| 80 | 2 |  |  |  |  | 34 | ); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  | } | 
| 83 | 4 |  |  |  |  | 3109 | ); | 
| 84 | 1 |  |  |  |  | 12749 | }; | 
| 85 | 1 |  |  |  |  | 10 | return $watcher; | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | sub build_watcher_guard { | 
| 89 | 4 |  |  | 4 | 0 | 8 | my $self = shift; | 
| 90 |  |  |  |  |  |  | return AnyEvent->timer( | 
| 91 |  |  |  |  |  |  | after    => 0, | 
| 92 |  |  |  |  |  |  | interval => $self->frequency, | 
| 93 |  |  |  |  |  |  | cb       => sub { | 
| 94 | 4 | 50 |  | 4 |  | 5754 | $self->watcher_callback->() | 
| 95 |  |  |  |  |  |  | if $self->active; | 
| 96 |  |  |  |  |  |  | } | 
| 97 | 4 |  |  |  |  | 60 | ); | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub description { | 
| 101 | 0 |  |  | 0 | 0 | 0 | my $self = shift; | 
| 102 | 0 |  |  |  |  | 0 | return "HTTP [" . $self->title . "]"; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | # private API | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | # intendent to be overriden in descendants | 
| 108 |  |  |  |  |  |  | sub _invoke_callback { | 
| 109 | 2 |  |  | 2 |  | 7 | my ($self, $callback, $status) = @_; | 
| 110 | 2 |  |  |  |  | 10 | $callback->($status); | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | 1; | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | __END__ |