File Coverage

blib/lib/WWW/Agent/Plugins/Focus.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package WWW::Agent::Plugins::Focus;
2              
3 1     1   7 use strict;
  1         2  
  1         41  
4 1     1   7 use Data::Dumper;
  1         2  
  1         64  
5 1     1   6 use POE;
  1         2  
  1         9  
6              
7             sub new {
8             my $class = shift;
9             my %options = @_;
10             my $self = bless { }, $class;
11              
12             $self->{hooks} = {
13             'init' => sub {
14             my ($kernel, $heap) = (shift, shift);
15             #warn "focus reset";
16             $heap->{focus} = undef;
17             return 1; # it worked
18             },
19             'cycle_pos_response' => sub {
20             my ($kernel, $heap) = (shift, shift);
21             #warn "positive response code";
22             my ($tab, $object) = (shift, shift);
23             $heap->{focus} = _refocus ($object->content);
24             ##warn "focus after response ".Dumper $heap->{focus};
25             return $object;
26             },
27             'cycle_neg_response' => sub {
28             my ($kernel, $heap) = (shift, shift);
29             my ($tab, $object) = (shift, shift);
30             #warn "negative response code";
31             $heap->{focus} = {};
32             return $object;
33             },
34             'focus_reset' => sub {
35             my ($kernel, $heap) = @_[KERNEL, HEAP];
36             #warn "focus reset";
37             $heap->{focus} = _refocus ($heap->{focus}->{content});
38             #warn "after reset ".$heap->{focus}->{focus};
39             },
40             'focus_set' => sub {
41             my ($kernel, $heap) = @_[KERNEL, HEAP];
42             #warn "focus set";
43             my ($tag, $pattern, $index, $baseurl) = @_[ARG0, ARG1, ARG2, ARG3];
44              
45             my $focus = $heap->{focus}->{focus};
46            
47             my @cands = $focus->look_down ( sub {
48             my $e = shift;
49             return $e->tag eq $tag;
50             } );
51             #warn "found cands". scalar @cands;
52             #warn "found cands". Dumper \@cands;
53              
54             if ($pattern) { # filter out those which do match
55             @cands = grep (_match ($_->as_HTML, $pattern), @cands);
56             }
57             #warn "2 found cands ". scalar @cands;
58             #warn "2 found cands". Dumper \@cands;
59             #warn "index ".$index;
60              
61             $heap->{focus}->{focus} = $cands[$index]; # just to indicate what we have found
62             if ($heap->{focus}->{focus} && $tag =~ /form/i) {
63 1     1   2720 use HTML::Form;
  0            
  0            
64             $heap->{focus}->{form} = HTML::Form->parse ($heap->{focus}->{focus}->as_HTML, $baseurl);
65             #warn "created form";
66             }
67             return $heap->{focus}->{focus};
68             },
69             'focus_get' => sub {
70             my ($kernel, $heap) = @_[KERNEL, HEAP];
71             #warn "focus get";
72             return $heap->{focus}->{form} || $heap->{focus}->{focus}; # when we have a FORM we prefer that
73             },
74             'focus_fill' => sub {
75             my ($kernel, $heap) = @_[KERNEL, HEAP];
76             #warn "focus fill";
77             my ($field, $value) = @_[ARG0, ARG1];
78              
79             return 0 unless $heap->{focus}->{form};
80             my $form = $heap->{focus}->{form};
81            
82             $form->value( $field, $value );
83             #warn $form->dump;
84             return 1;
85             },
86             };
87              
88             $self->{namespace} = 'focus';
89             return $self;
90             }
91              
92             sub _match {
93             my $s = shift;
94             my $p = shift;
95            
96             #warn "checking '$s' against pattern $p".Dumper $p;
97             return $s =~ $p;
98             }
99              
100             sub _refocus {
101             my $content = shift;
102              
103             use HTML::TreeBuilder;
104             return { focus => HTML::TreeBuilder->new_from_content ($content),
105             form => undef,
106             content => $content };
107             }
108              
109             1;
110              
111             __END__