/usr/share/perl5/Jifty/Client.pm is in libjifty-perl 1.10518+dfsg-1ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | use strict;
use warnings;
package Jifty::Client;
use base qw/WWW::Mechanize/;
delete $ENV{'http_proxy'}; # Otherwise WWW::Mechanize tries to go through your HTTP proxy
use Jifty::YAML;
use HTTP::Cookies;
use HTML::TreeBuilder::XPath;
use List::Util qw(first);
use Carp;
=head1 NAME
Jifty::Client - Subclass of L<WWW::Mechanize> with extra Jifty features
=head1 DESCRIPTION
This module is a base for building robots to interact with Jifty applications.
It currently contains much overlapping code with C<Jifty::Test::WWW::Mechanize>,
except that it does not inherit from C<Test::WWW::Mechanize>.
Expect this code to be refactored in the near future.
=head1 METHODS
=head2 new
Overrides L<WWW::Mechanize>'s C<new> to automatically give the
bot a cookie jar.
=cut
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->cookie_jar(HTTP::Cookies->new);
return $self;
}
=head2 moniker_for ACTION, FIELD1 => VALUE1, FIELD2 => VALUE2
Finds the moniker of the first action of type I<ACTION> whose
"constructor" field I<FIELD1> is I<VALUE1>, and so on.
=cut
sub moniker_for {
my $self = shift;
my $action = Jifty->api->qualify(shift);
my %args = @_;
# Search through all the inputs of all the forms
for my $f ($self->forms) {
INPUT:
for my $input ($f->inputs) {
# Look for the matching action
if ($input->type eq "hidden" and $input->name =~ /^J:A-(?:\d+-)?(.*)/ and $input->value eq $action) {
# We have a potential moniker
my $moniker = $1;
# Make sure that this action actually has the field values we're
# looking for, if not keep looking
for my $id (keys %args) {
my $idfield = $f->find_input("J:A:F:F-$id-$moniker");
next INPUT unless $idfield and $idfield->value eq $args{$id};
}
# It does! Return it...
return $1;
}
}
}
return undef;
}
=head2 fill_in_action MONIKER, FIELD1 => VALUE1, FIELD2 => VALUE2, ...
Finds the fields on the current page with the names FIELD1, FIELD2,
etc in the MONIKER action, and fills them in. Returns the
L<HTML::Form> object of the form that the action is in, or undef if it
can't find all the fields.
=cut
sub fill_in_action {
my $self = shift;
my $moniker = shift;
my %args = @_;
# Load the form object containing the given moniker or quit
my $action_form = $self->action_form($moniker, keys %args);
return unless $action_form;
# For each field name given, set the field's value
for my $arg (keys %args) {
my $input = $action_form->find_input("J:A:F-$arg-$moniker");
return unless $input;
$input->value($args{$arg});
}
# Return the form in case they want to do something with it
return $action_form;
}
=head2 action_form MONIKER [ARGUMENTNAMES]
Returns the form (as an L<HTML::Form> object) corresponding to the
given moniker (which also contains inputs for the given
argumentnames), and also selects it as the current form. Returns
undef if it can't be found.
=cut
sub action_form {
my $self = shift;
my $moniker = shift;
my @fields = @_;
Carp::confess("No moniker") unless $moniker;
# Go through all the forms looking for the moniker
my $i;
for my $form ($self->forms) {
no warnings 'uninitialized';
# Keep looking unless the right kind of input is found
$i++;
next unless first { $_->name =~ /J:A-(?:\d+-)?$moniker/
&& $_->type eq "hidden" }
$form->inputs;
# Keep looking if the suggested field's don't match up
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_value MONIKER, FIELD
Finds the fields 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 $moniker = shift;
my $field = shift;
# Find the form containing the moniker requested
my $action_form = $self->action_form($moniker, $field);
return unless $action_form;
# Find the input containing the field requested and fetch the value
my $input = $action_form->find_input("J:A:F-$field-$moniker");
return unless $input;
return $input->value;
}
=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 send_action {
my $self = shift;
my $class = shift;
my %args = @_;
# Setup the URL of the request we're about to make
my $uri = $self->uri->clone;
$uri->path("__jifty/webservices/yaml");
# Setup the action request we're going to send
my $request = HTTP::Request->new(
POST => $uri,
[ 'Content-Type' => 'text/x-yaml' ],
Jifty::YAML::Dump(
{ path => $uri->path,
actions => {
action => {
moniker => 'action',
class => $class,
fields => \%args
}
}
}
)
);
# Fire off the request, evaluate the result, and return it
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 = @_;
# Setup the URL we're going to use
my $uri = $self->uri->clone;
$uri->path("__jifty/webservices/xml");
# Setup the request we're going to use
my $request = HTTP::Request->new(
POST => $uri,
[ 'Content-Type' => 'text/x-yaml' ],
Jifty::YAML::Dump(
{ path => $uri->path,
fragments => {
fragment => {
name => 'fragment',
path => $path,
args => \%args
}
}
}
)
);
# Fire the request, evaluate the result, and return it
my $result = $self->request( $request );
use XML::Simple;
my $content = eval { XML::Simple::XMLin($result->content, SuppressEmpty => '')->{fragment}{content} } || '';
$self->back;
return $content;
}
=head2 field_error_text MONIKER, FIELD
Finds the error span on the current page for the name FIELD in the
action MONIKER, and returns the text (tags stripped) from it. (If the
field can't be found, return undef).
=cut
sub field_error_text {
my $self = shift;
my $moniker = shift;
my $field = shift;
# Setup the XPath processor and the ID we're looking for
my $tree = HTML::TreeBuilder::XPath->new;
$tree->parse($self->content);
$tree->eof;
my $id = "errors-J:A:F-$field-$moniker";
# Search for the span containing that error
return $tree->findvalue(qq{//span[\@id = "$id"]});
}
=head2 uri
L<WWW::Mechanize> has a bug where it returns the wrong value for
C<uri> after redirect. This fixes that. See
http://rt.cpan.org/NoAuth/Bug.html?id=9059
=cut
sub uri { shift->response->request->uri }
=head2 session
Returns the server-side L<Jifty::Web::Session> object associated with
this Mechanize object.
=cut
sub session {
my $self = shift;
# We don't have a session!
return undef unless $self->cookie_jar->as_string =~ /JIFTY_SID_\d+=([^;]+)/;
# Load the data stored in the session cookie
my $session = Jifty::Web::Session->new;
$session->load($1);
return $session;
}
=head2 continuation [ID]
Returns the current continuation of the Mechanize object, if any. Or,
given an ID, returns the continuation with that ID.
=cut
sub continuation {
my $self = shift;
# If we don't have a session, we don't have a continuation
my $session = $self->session;
return undef unless $session;
# Look for the continuation info in the URL
my $id = shift;
($id) = $self->uri =~ /J:(?:C|CALL|RETURN)=([^&;]+)/ unless $id;
# Return information about the continuation
return $session->get_continuation($id);
}
=head2 current_user
Returns the L<Jifty::CurrentUser> object or descendant, if any.
=cut
sub current_user {
my $self = shift;
# We don't have a current user if we don't have a session
my $session = $self->session;
return undef unless $session;
# Fetch information about user from the session
my $id = $session->get('user_id');
my $object = Jifty->app_class("CurrentUser")->new();
my $user = $session->get('user_ref')->new( current_user => $object );
$user->load_by_cols( id => $id );
$object->user_object($user);
return $object;
}
=head1 SEE ALSO
L<Jifty::Test::WWW::Mechanize>
=head1 LICENSE
Jifty is Copyright 2005-2010 Best Practical Solutions, LLC.
Jifty is distributed under the same terms as Perl itself.
=cut
1;
|