Mojo-Transaction-HTTP-Role-Mechanize

 view release on metacpan or  search on metacpan

lib/Mojo/Transaction/HTTP/Role/Mechanize.pm  view on Meta::CPAN

package Mojo::Transaction::HTTP::Role::Mechanize;

use Mojo::Base -role;
use Mojo::UserAgent::Transactor;

our $VERSION = '0.06';

requires qw{error res};

sub extract_forms {
  my $self = shift;

  my $forms
    = $self->res->dom->find('form')->each(sub { $_->with_roles('+Form') });

  return $forms;
}

sub submit {
  my ($self, $selector, $overlay) = (shift);
  $overlay  = pop   if @_ && ref($_[-1]) eq 'HASH';
  $selector = shift if @_ % 2;
  $overlay ||= {@_};

  # cannot continue from error state
  return if $self->error;

  # extract form
  my $form = $self->extract_forms->grep(sub { $_->at($selector // '') })->first
    or return;

  return unless (my ($method, $target, $type) = $form->target($selector));
  $target = $self->req->url->new($target);
  $target = $target->to_abs($self->req->url) unless $target->is_abs;

  # values from form
  my $state = $form->val($selector);

  # merge in new values of form elements
  my @keys = grep { exists $overlay->{$_} } keys %$state;
  @$state{@keys} = @$overlay{@keys};

  # build a new transaction ...
  return Mojo::UserAgent::Transactor->new->tx(
    $method => $target,
    {}, form => $state
  )->previous($self);
}

1;

=encoding utf8

=begin html

<img alt="Build Status - Linux"
     src="https://github.com/kiwiroy/mojo-transaction-http-role-mechanize/workflows/linux/badge.svg" />
<img alt="Build Status - Macos"
    src="https://github.com/kiwiroy/mojo-transaction-http-role-mechanize/workflows/macos/badge.svg" />
<img alt="Build Status - Windows"
     src="https://github.com/kiwiroy/mojo-transaction-http-role-mechanize/workflows/windows/badge.svg" />
<a href="https://kritika.io/users/kiwiroy/repos/7509235145731088/heads/master/">
  <img alt="Kritika Analysis Status"
       src="https://kritika.io/users/kiwiroy/repos/7509235145731088/heads/master/status.svg?type=score%2Bcoverage%2Bdeps" />
</a>
<a href="https://coveralls.io/github/kiwiroy/mojo-transaction-http-role-mechanize?branch=master">
  <img alt="Coverage Status"
       src="https://coveralls.io/repos/github/kiwiroy/mojo-transaction-http-role-mechanize/badge.svg?branch=master" />
</a>
<a href="https://badge.fury.io/pl/Mojo-Transaction-HTTP-Role-Mechanize">
  <img alt="CPAN version"
       src="https://badge.fury.io/pl/Mojo-Transaction-HTTP-Role-Mechanize.svg" />
</a>

=end html

=head1 NAME

Mojo::Transaction::HTTP::Role::Mechanize - Mechanize Mojo a little

=head1 SYNOPSIS

  use Mojo::UserAgent;
  use Mojo::Transaction::HTTP::Role::Mechanize;

  my $ua = Mojo::UserAgent->new;
  my $tx = $ua->get('/')->with_roles('+Mechanize');

  # call submit immediately
  my $submit_tx = $tx->submit('#submit-id', username => 'fry');
  $ua->start($submit_tx);

  # first extract form values
  my $values = $tx->extract_forms->first->val;
  $submit_tx = $tx->submit('#submit-id', counter => $values->{counter} + 3);
  $ua->start($submit_tx);

=head1 DESCRIPTION

L<Role::Tiny> based role to compose a form submission I<"trait"> into
L<Mojo::Transaction::HTTP>.



( run in 1.078 second using v1.01-cache-2.11-cpan-39bf76dae61 )