File Coverage

blib/lib/Apache/BalancerManager.pm
Criterion Covered Total %
statement 55 64 85.9
branch 1 2 50.0
condition n/a
subroutine 14 15 93.3
pod 3 3 100.0
total 73 84 86.9


line stmt bran cond sub pod time code
1             package Apache::BalancerManager;
2             $Apache::BalancerManager::VERSION = '0.002000';
3             # ABSTRACT: Interact with the Apache BalancerManager
4              
5 1     1   272814 use Moo;
  1         9409  
  1         9  
6 1     1   2083 use List::Util ();
  1         2  
  1         26  
7 1     1   663 use Web::Scraper;
  1         110071  
  1         9  
8              
9             has url => (
10             is => 'ro',
11             required => 1,
12             );
13              
14             has name => (
15             is => 'ro',
16             lazy => 1,
17             builder => '_build_name',
18             );
19              
20             sub _build_name {
21 1     1   278373 my $ret = $_[0]->_scraped_content->{name_stuff};
22              
23 1         76 $ret =~ s(^balancer://)();
24              
25 1         19 return $ret
26             }
27              
28             has nonce => (
29             is => 'ro',
30             init_arg => undef,
31             lazy => 1,
32             builder => '_build_nonce',
33             );
34              
35             sub _build_nonce {
36 1     1   14 my $self = shift;
37              
38 1         8 my $url = $self->url;
39 1         26 my $link = $self->_scraped_content->{data}[0]{link};
40              
41 1         40 require URI;
42 1         760 require URI::QueryParam;
43 1         180 my $uri = URI->new("$url$link");
44 1         194 scalar $uri->query_param('nonce')
45             }
46              
47             has _index_content => (
48             is => 'ro',
49             init_arg => undef,
50             lazy => 1,
51             builder => '_build_index_content',
52             );
53              
54             sub _build_index_content {
55 1     1   17 my $self = shift;
56              
57 1         33 my $response = $self->_get($self->url);
58 1 50       25999 if ($response->is_success) {
59 1         25 return $response->decoded_content;
60             }
61             else {
62 0         0 die $response->status_line;
63             }
64             }
65              
66             has _scraper => (
67             is => 'ro',
68             init_arg => undef,
69             lazy => 1,
70             builder => '_build_scraper',
71             handles => {
72             _scrape => 'scrape',
73             },
74             );
75              
76             sub _build_scraper {
77             return scraper {
78 1     1   49210 process 'h3 > a', name_stuff => 'TEXT';
79             process '//table[2]/tr' => 'data[]' => scraper {
80 3         45566 process '//td[1]/a/@href', 'link' => 'TEXT';
81 3         23935 process '//td[1]/a', 'location' => 'TEXT';
82 3         14782 process '//td[2]', 'route' => 'TEXT';
83 3         13975 process '//td[3]', 'route_redirect' => 'TEXT';
84 3         14140 process '//td[4]', 'load_factor' => 'TEXT';
85 3         14275 process '//td[5]', 'lb_set' => 'TEXT';
86 3         14393 process '//td[6]', status => 'TEXT';
87 3         14561 process '//td[7]', times_elected => 'TEXT';
88 3         14528 process '//td[8]', busy => 'TEXT';
89 3         14458 process '//td[9]', load => 'TEXT';
90 3         14635 process '//td[10]', to => 'TEXT';
91 3         14912 process '//td[11]', from => 'TEXT';
92 1         13776 };
93 1     1   8593 };
94             }
95              
96             has _scraped_content => (
97             is => 'ro',
98             init_arg => undef,
99             lazy => 1,
100             builder => '_build_scraped_content',
101             );
102              
103             sub _build_scraped_content {
104 1     1   68 my $s = $_[0]->_scrape( $_[0]->_index_content );
105 1         7096 $s->{data} = [grep %$_, @{$s->{data}}];
  1         8  
106 1         9 $s
107             }
108              
109             has _user_agent => (
110             is => 'ro',
111             lazy => 1,
112             init_arg => 'user_agent',
113             builder => '_build_user_agent',
114             handles => {
115             _get => 'get',
116             _post => 'post',
117             },
118             );
119              
120             sub _build_user_agent {
121 0     0   0 my $self = shift;
122 0         0 require LWP::UserAgent;
123 0         0 require URI;
124 0         0 my $uri = URI->new($self->url);
125 0         0 my $ua = LWP::UserAgent->new;
126             # since httpd 2.4.41 referer is required for XSS protection
127 0         0 $ua->default_header(Referer => $self->url);
128 0         0 $ua->default_header(Host => $uri->host);
129 0         0 $ua
130             }
131              
132             has _members => (
133             is => 'ro',
134             # TODO: support passing memebers as either objects or strings that coerce
135             init_arg => undef,
136             lazy => 1,
137             builder => '_build_members',
138             );
139              
140             sub _build_members {
141 1     1   776 require Apache::BalancerManager::Member;
142              
143             [
144             map {;
145 2         29 $_->{status} = $_->{status} =~ m/Ok/;
146 2         4 $_->{manager} = $_[0];
147 2         50 Apache::BalancerManager::Member->new($_),
148 1         5 } @{$_[0]->_scraped_content->{data}}
  1         36  
149             ]
150             }
151              
152 2     2 1 2006 sub get_member_by_index { $_[0]->_members->[$_[1]]; }
153              
154             sub get_member_by_location {
155 2     2 1 1008 my ($self, $location) = @_;
156              
157 2     3   10 List::Util::first { $_->location eq $location } @{$self->_members};
  3         33  
  2         63  
158             }
159              
160 1     1 1 1104 sub member_count { scalar @{$_[0]->_members} }
  1         36  
161              
162             1;
163              
164             __END__