Jifty

 view release on metacpan or  search on metacpan

lib/Jifty/Test/WWW/Mechanize.pm  view on Meta::CPAN

    for my $form ($self->forms) {
        no warnings 'uninitialized';

        $i++;
        next unless first {   $_->name =~ /J:A-(?:\d+-)?$moniker/
                           && $_->type eq "submit" }
                        $form->inputs;
        next if grep {not $form->find_input("J:A:F-$_-$moniker")} @fields;

        $self->form_number($i); #select it, for $mech->submit etc
        return $form;
    } 
    return;
} 

=head2 action_field_input MONIKER, FIELD

Finds the field on the current page with the names FIELD in the
action MONIKER, and returns its L<HTML::Form::Input>, or undef if it can't be
found.

=cut

sub action_field_input {
    my $self = shift;
    my $moniker = shift;
    my $field = shift;

    my $action_form = $self->action_form($moniker, $field);
    return unless $action_form;

    my $input = $action_form->find_input("J:A:F-$field-$moniker");
    return $input;
}

=head2 action_field_value MONIKER, FIELD

Finds the field on the current page with the names FIELD in the
action MONIKER, and returns its value, or undef if it can't be found.

=cut

sub action_field_value {
    my $self = shift;
    my $input = $self->action_field_input(@_);
    return $input ? $input->value : undef;
}

=head2 send_action CLASS ARGUMENT => VALUE, [ ... ]

Sends a request to the server via the webservices API, and returns the
L<Jifty::Result> of the action.  C<CLASS> specifies the class of the
action, and all parameters thereafter supply argument keys and values.

The URI of the page is unchanged after this; this is accomplished by
using the "back button" after making the webservice request.

=cut

sub _build_webservices_request {
    my ($self, $endpoint, $data) = @_;

    my $uri = $self->uri->clone;
    $uri->path($endpoint);
    $uri->query('');

    my $body = Jifty::YAML::Dump({ path => $endpoint, %$data});

    HTTP::Request->new(
        POST => $uri,
        [ 'Content-Type' => 'text/x-yaml',
          'Content-Length' => length($body) ],
        $body
    );
}

sub send_action {
    my $self = shift;
    my $class = shift;
    my %args = @_;

    my $request = $self->_build_webservices_request
        ( "__jifty/webservices/yaml",
          { actions => {
                action => {
                    moniker => 'action',
                    class   => $class,
                    fields  => \%args
                }
            }
        });

    my $result = $self->request( $request );
    my $content = eval { Jifty::YAML::Load($result->content)->{action} } || undef;
    $self->back;
    return $content;
}

=head2 fragment_request PATH ARGUMENT => VALUE, [ ... ]

Makes a request for the fragment at PATH, using the webservices API,
and returns the string of the result.

=cut

sub fragment_request {
    my $self = shift;
    my $path = shift;
    my %args = @_;

    my $request = $self->_build_webservices_request
        ( "__jifty/webservices/xml",
          { fragments => {
                fragment => {
                    name  => 'fragment',
                    path  => $path,
                    args  => \%args
                }
            }
        });

    my $result = $self->request( $request );

    use XML::Simple;
    my $content = eval { XML::Simple::XMLin($result->content, SuppressEmpty => '')->{fragment}{content} } || '';
    $self->back;
    return $content;



( run in 0.708 second using v1.01-cache-2.11-cpan-524268b4103 )