File Coverage

blib/lib/Mojo/Transaction/HTTP/Role/Mechanize.pm
Criterion Covered Total %
statement 25 25 100.0
branch 11 12 91.6
condition 7 7 100.0
subroutine 6 6 100.0
pod 2 2 100.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Mojo::Transaction::HTTP::Role::Mechanize;
2              
3 1     1   27859 use Mojo::Base -role;
  1         2  
  1         6  
4 1     1   435 use Mojo::UserAgent::Transactor;
  1         2  
  1         8  
5              
6             our $VERSION = '0.05';
7              
8             requires qw{error res};
9              
10             sub extract_forms {
11 14     14 1 1014 my $self = shift;
12              
13             my $forms
14 14     14   41 = $self->res->dom->find('form')->each(sub { $_->with_roles('+Form') });
  14         24384  
15              
16 14         3774 return $forms;
17             }
18              
19             sub submit {
20 14     14 1 98632 my ($self, $selector, $overlay) = (shift);
21 14 100 100     88 $overlay = pop if @_ && ref($_[-1]) eq 'HASH';
22 14 100       50 $selector = shift if @_ % 2;
23 14   100     65 $overlay ||= {@_};
24              
25             # cannot continue from error state
26 14 100       63 return if $self->error;
27              
28             # extract form
29 13 100 100 13   273 my $form = $self->extract_forms->grep(sub { $_->at($selector // '') })->first
  13         144  
30             or return;
31              
32 11 100       7261 return unless (my ($method, $target, $type) = $form->target($selector));
33 10         46 $target = $self->req->url->new($target);
34 10 50       722 $target = $target->to_abs($self->req->url) unless $target->is_abs;
35              
36             # values from form
37 10         1880 my $state = $form->val($selector);
38              
39             # merge in new values of form elements
40 10         58 my @keys = grep { exists $overlay->{$_} } keys %$state;
  135         219  
41 10         40 @$state{@keys} = @$overlay{@keys};
42              
43             # build a new transaction ...
44 10         63 return Mojo::UserAgent::Transactor->new->tx(
45             $method => $target,
46             {}, form => $state
47             );
48             }
49              
50             1;
51              
52             =encoding utf8
53              
54             =begin html
55              
56            
57             Travis Build Status
58             src="https://travis-ci.com/kiwiroy/mojo-transaction-http-role-mechanize.svg?branch=master" />
59            
60            
61             Kritika Analysis Status
62             src="https://kritika.io/users/kiwiroy/repos/7509235145731088/heads/master/status.svg?type=score%2Bcoverage%2Bdeps" />
63            
64            
65             Coverage Status
66             src="https://coveralls.io/repos/github/kiwiroy/mojo-transaction-http-role-mechanize/badge.svg?branch=master" />
67            
68            
69             CPAN version
70             src="https://badge.fury.io/pl/Mojo-Transaction-HTTP-Role-Mechanize.svg" />
71            
72              
73             =end html
74              
75             =head1 NAME
76              
77             Mojo::Transaction::HTTP::Role::Mechanize - Mechanize Mojo a little
78              
79             =head1 SYNOPSIS
80              
81             use Mojo::UserAgent;
82             use Mojo::Transaction::HTTP::Role::Mechanize;
83              
84             my $ua = Mojo::UserAgent->new;
85             my $tx = $ua->get('/')->with_roles('+Mechanize');
86              
87             # call submit immediately
88             my $submit_tx = $tx->submit('#submit-id', username => 'fry');
89             $ua->start($submit_tx);
90              
91             # first extract form values
92             my $values = $tx->extract_forms->first->val;
93             $submit_tx = $tx->submit('#submit-id', counter => $values->{counter} + 3);
94             $ua->start($submit_tx);
95              
96             =head1 DESCRIPTION
97              
98             L based role to compose a form submission I<"trait"> into
99             L.
100              
101             =head1 METHODS
102              
103             L implements the following method.
104              
105             =head2 extract_forms
106              
107             $collection = $tx->extract_forms;
108              
109             Returns a L of L elements with activated L
110             that contains all the forms of the page.
111              
112             =head2 submit
113              
114             # result using selector
115             $submit_tx = $tx->submit('#id', username => 'fry');
116              
117             # result without selector using default submission
118             $submit_tx = $tx->submit(username => 'fry');
119              
120             # passing hash, rather than list, of values
121             $submit_tx = $tx->submit({username => 'fry'});
122              
123             # passing hash, rather than list, of values and a selector
124             $submit_tx = $tx->submit('#id', {username => 'fry'});
125              
126             Build a new L object with
127             L and the contents of the C
with the
128             C<$id> and merged values. If no selector is given, the first non-disabled
129             button or appropriate input element (of type button, submit, or image)
130             will be used for the submission.
131              
132             =head1 AUTHOR
133              
134             kiwiroy - Roy Storey C
135              
136             =head1 CONTRIBUTORS
137              
138             tekki - Rolf Stöckli C
139              
140             lindleyw - William Lindley C
141              
142             =head1 LICENSE
143              
144             This library is free software and may be distributed under the same terms as
145             perl itself.
146              
147             =head1 SEE ALSO
148              
149             L, L.
150              
151             =cut