File Coverage

blib/lib/Mojo/Transaction/HTTP/Role/Mechanize.pm
Criterion Covered Total %
statement 22 22 100.0
branch 13 14 92.8
condition 7 7 100.0
subroutine 4 4 100.0
pod 1 1 100.0
total 47 48 97.9


line stmt bran cond sub pod time code
1             package Mojo::Transaction::HTTP::Role::Mechanize;
2              
3 1     1   31284 use Mojo::Base -role;
  1         2  
  1         7  
4 1     1   464 use Mojo::UserAgent::Transactor;
  1         4  
  1         9  
5              
6             our $VERSION = '0.04';
7              
8             requires qw{error};
9              
10             sub submit {
11 14     14 1 121831 my ($self, $selector, $overlay) = (shift);
12 14 100 100     99 $overlay = pop if @_ && ref($_[-1]) eq 'HASH';
13 14 100       57 $selector = shift if @_ % 2;
14 14   100     86 $overlay ||= { @_ };
15             # cannot continue from error state
16 14 100       62 return if $self->error;
17             # form from selector
18             return unless defined(my $form = $self->res->dom->find('form')
19 13 100 100 13   334 ->grep(sub { $_->at($selector || '') })->first);
  13         22538  
20             # compose ...
21 11 100       9275 $form->with_roles('Mojo::DOM::Role::Form')
22             unless Role::Tiny::does_role($form, 'Mojo::DOM::Role::Form');
23             # # now there is a form, rely on _form_default_submit() if we relied on $any b4.
24             # $selector = undef if $any eq $selector;
25 11 100       340 return unless (my ($method, $target, $type) =
26             $form->target($selector));
27 10         56 $target = $self->req->url->new($target);
28 10 50       903 $target = $target->to_abs($self->req->url) unless $target->is_abs;
29             # values from form
30 10         2384 my $state = $form->val($selector);
31             # merge in new values of form elements
32 10         76 my @keys = grep { exists $overlay->{$_} } keys %$state;
  135         310  
33 10         49 @$state{@keys} = @$overlay{@keys};
34              
35             # build a new transaction ...
36 10         75 return Mojo::UserAgent::Transactor->new->tx(
37             $method => $target, {}, form => $state
38             );
39             }
40              
41              
42             1;
43              
44             =encoding utf8
45              
46             =begin html
47              
48            
49             Travis Build Status
50             src="https://travis-ci.com/kiwiroy/mojo-transaction-http-role-mechanize.svg?branch=master" />
51            
52            
53             Kritika Analysis Status
54             src="https://kritika.io/users/kiwiroy/repos/7509235145731088/heads/master/status.svg?type=score%2Bcoverage%2Bdeps" />
55            
56            
57             Coverage Status
58             src="https://coveralls.io/repos/github/kiwiroy/mojo-transaction-http-role-mechanize/badge.svg?branch=master" />
59            
60              
61             =end html
62              
63             =head1 NAME
64              
65             Mojo::Transaction::HTTP::Role::Mechanize - Mechanize Mojo a little
66              
67             =head1 SYNOPSIS
68              
69             # description
70             my $tx = $ua->get('/')->with_roles('+Mechanize');
71             my $submit_tx = $tx->submit('#submit-id', username => 'fry');
72             $ua->start($submit_tx);
73              
74             =head1 DESCRIPTION
75              
76             L based role to compose a form submission I<"trait"> into
77             L.
78              
79             =head1 METHODS
80              
81             L implements the following method.
82              
83             =head2 submit
84              
85             # result using selector
86             $submit_tx = $tx->submit('#id', username => 'fry');
87             # result without selector using default submission
88             $submit_tx = $tx->submit(username => 'fry');
89             # passing hash, rather than list, of values
90             $submit_tx = $tx->submit({username => 'fry'});
91             # passing hash, rather than list, of values and a selector
92             $submit_tx = $tx->submit('#id', {username => 'fry'});
93              
94             Build a new L object with
95             L and the contents of the C
with the
96             C<$id> and merged values. If no selector is given, the first non-disabled
97             button or appropriate input element (of type button, submit, or image)
98             will be used for the submission.
99              
100             =head1 AUTHOR
101              
102             kiwiroy - Roy Storey C
103              
104             =head1 CONTRIBUTORS
105              
106             tekki - Rolf Stöckli C
107              
108             lindleyw - William Lindley C
109              
110             =head1 LICENSE
111              
112             This library is free software and may be distributed under the same terms as
113             perl itself.
114              
115             =cut