SYNOPSIS

    use Net::Jifty;
    my $j = Net::Jifty->new(
        site        => 'http://mushroom.mu/',
        cookie_name => 'MUSHROOM_KINGDOM_SID',
        email       => '[email protected]',
        password    => 'melange',
    );

    # the story begins
    $j->create(Hero => name => 'Mario', job => 'Plumber');

    # find the hero whose job is Plumber and change his name to Luigi
    # and color to green
    $j->update(Hero => job => 'Plumber',
        name  => 'Luigi',
        color => 'Green',
    );

    # win!
    $j->delete(Enemy => name => 'Bowser');

DESCRIPTION

Jifty is a full-stack web framework. It provides an optional \s-1REST\s0 interface for applications. Using this module, you can interact with that \s-1REST\s0 interface to write client-side utilities.

You can use this module directly, but you'll be better off subclassing it, such as what we've done for Net::Hiveminder.

This module also provides a number of convenient methods for writing short scripts. For example, passing \*(C`use_config => 1\*(C' to \*(C`new\*(C' will look at the config file for the username and password (or \s-1SID\s0) of the user. If neither is available, it will prompt the user for them.

METHODS

\s-1CRUD\s0 - create, read, update and delete.

create \s-1MODEL\s0, \s-1FIELDS\s0

Create a new object of type \*(C`MODEL\*(C' with the \*(C`FIELDS\*(C' set.

read \s-1MODEL\s0, \s-1KEY\s0 => \s-1VALUE\s0

Find some \*(C`MODEL\*(C' where \*(C`KEY\*(C' is \*(C`VALUE\*(C' and return it.

update \s-1MODEL\s0, \s-1KEY\s0 => \s-1VALUE\s0, \s-1FIELDS\s0

Find some \*(C`MODEL\*(C' where \*(C`KEY\*(C' is \*(C`VALUE\*(C' and set \*(C`FIELDS\*(C' on it.

delete \s-1MODEL\s0, \s-1KEY\s0 => \s-1VALUE\s0

Find some \*(C`MODEL\*(C' where \*(C`KEY\*(C' is \*(C`VALUE\*(C' and delete it.

Other actions

search \s-1MODEL\s0, FIELDS[, \s-1OUTCOLUMN\s0]

Searches for all objects of type \*(C`MODEL\*(C' that satisfy \*(C`FIELDS\*(C'. The optional \*(C`OUTCOLUMN\*(C' defines the output column, in case you don't want the entire records.

act \s-1ACTION\s0, \s-1ARGS\s0

Perform any \*(C`ACTION\*(C', using \*(C`ARGS\*(C'. This does use the \s-1REST\s0 interface.

Arguments of actions

Arguments are treated as arrays with (name, value) pairs so you can do the following:

$jifty->create('Model', x => 1, x => 2, x => 3 );

Some actions may require file uploads then you can use hash reference as value with content, filename and content_type fields. filename and content_type are optional. content_type by default is 'application/octeat-stream'.

validate_action_args action => args

Validates the given action, to check to make sure that all mandatory arguments are given and that no unknown arguments are given.

Arguments are checked \s-1CRUD\s0 and act methods if 'strict_arguments' is set to true.

You may give action as a string, which will be interpreted as the action name; or as an array reference for \s-1CRUD\s0 - the first element will be the action (create, update, or delete) and the second element will be the model name.

This will throw an error or if validation succeeds, will return 1.

Specifications of actions and models

get_action_spec \s-1NAME\s0

Returns the action spec (which arguments it takes, and metadata about them). The first request for a particular action will ask the server for the spec. Subsequent requests will return it from the cache.

get_model_spec \s-1NAME\s0

Returns the model spec (which columns it has). The first request for a particular model will ask the server for the spec. Subsequent requests will return it from the cache.

Subclassing

\s-1BUILD\s0

Each Net::Jifty object will do the following upon creation:

Read config

..but only if you \*(C`use_config\*(C' is set to true.

Log in

..unless a sid is available, in which case we're already logged in.

login

This method is called automatically when each Net::Jifty object is constructed (unless a session \s-1ID\s0 is passed in).

This assumes your site is using Jifty::Plugin::Authentication::Password. If that's not the case, override this in your subclass.

prompt_login_info

This will ask the user for her email and password. It may do so repeatedly until login is successful.

call \s-1ACTION\s0, \s-1ARGS\s0

This uses the Jifty \*(L"web services\*(R" \s-1API\s0 to perform \*(C`ACTION\*(C'. This is not the \s-1REST\s0 interface, though it resembles it to some degree.

This module currently only uses this to log in.

Requests helpers

post \s-1URL\s0, \s-1ARGS\s0

This will post \*(C`ARGS\*(C' to \*(C`URL\*(C'. See the documentation for \*(C`method\*(C' about the format of \*(C`URL\*(C'.

get \s-1URL\s0, \s-1ARGS\s0

This will get the specified \*(C`URL\*(C' with \*(C`ARGS\*(C' as query parameters. See the documentation for \*(C`method\*(C' about the format of \*(C`URL\*(C'.

method \s-1METHOD\s0, URL[, \s-1ARGS\s0]

This will perform a \*(C`METHOD\*(C' (\s-1GET\s0, \s-1POST\s0, \s-1PUT\s0, \s-1DELETE\s0, etc) using the internal LWP::UserAgent object.

\*(C`URL\*(C' may be a string or an array reference (which will have its parts properly escaped and joined with \*(C`/\*(C'). \*(C`URL\*(C' already has \*(C`http://your.site/=/\*(C' prepended to it, and \*(C`.yml\*(C' appended to it, so you only need to pass something like \*(C`model/YourApp.Model.Foo/name\*(C', or \*(C`[qw/model YourApp.Model.Foo name]\*(C'.

This will return the data structure returned by the Jifty application, or throw an error.

form_url_encoded_args \s-1ARGS\s0

This will take an array containing (name, value) argument pairs and convert those arguments into \s-1URL\s0 encoded form. I.e., (x => 1, y => 2, z => 3) becomes:

x=1&y=2&z=3

These are then ready to be appened to the \s-1URL\s0 on a \s-1GET\s0 or placed into the content of a \s-1PUT\s0. However this method can not handle file uploads as they must be sent using 'multipart/form-date'.

See also /\*(L"form_form_data_args \s-1ARGS\s0\*(R" and \*(L"Arguments of actions\*(R".

form_form_data_args \s-1ARGS\s0

This will take an array containing (name, value) argument pairs and convert those arguments into HTTP::Message objects ready for adding to a 'mulitpart/form-data' HTTP::Request as parts with something like:

my $req = HTTP::Request->new( POST => $uri ); $req->header('Content-type' => 'multipart/form-data'); $req->add_part( $_ ) foreach $self->form_form_data_args( @args );

This method can handle file uploads, read more in \*(L"Arguments of actions\*(R".

See also /\*(L"form_form_data_args \s-1ARGS\s0\*(R" and \*(L"Arguments of actions\*(R".

join_url \s-1FRAGMENTS\s0

Encodes \*(C`FRAGMENTS\*(C' and joins them with \*(C`/\*(C'.

escape \s-1STRINGS\s0

Returns \*(C`STRINGS\*(C', properly URI-escaped.

Various helpers

email_eq \s-1EMAIL\s0, \s-1EMAIL\s0

Compares the two email addresses. Returns true if they're equal, false if they're not.

is_me \s-1EMAIL\s0

Returns true if \*(C`EMAIL\*(C' looks like it is the same as the current user's.

email_of \s-1ID\s0

Retrieve user \*(C`ID\*(C''s email address.

load_date \s-1DATE\s0

Loads \*(C`DATE\*(C' (which must be of the form \*(C`YYYY-MM-DD\*(C') into a DateTime object.

get_sid

Retrieves the sid from the LWP::UserAgent object.

Working with config

load_config

This will return a hash reference of the user's preferences. Because this method is designed for use in small standalone scripts, it has a few peculiarities.

  • It will \*(C`warn\*(C' if the permissions are too liberal on the config file, and fix them.

  • It will prompt the user for an email and password if necessary. Given the email and password, it will attempt to log in using them. If that fails, then it will try again.

  • Upon successful login, it will write a new config consisting of the options already in the config plus session \s-1ID\s0, email, and password.

config_permissions

This will warn about (and fix) config files being readable by group or others.

read_config_file

This transforms the config file into a hashref. It also does any postprocessing needed, such as transforming localhost to 127.0.0.1 (due to an obscure bug, probably in HTTP::Cookies).

The config file is a \s-1YAML\s0 document that looks like:

--- email: [email protected] password: drowssap sid: 11111111111111111111111111111111

write_config_file

This will write the config to disk. This is usually only done when a sid is discovered, but may happen any time.

filter_config [\s-1DIRECTORY\s0] -> \s-1HASH\s0

Looks at the (given or) current directory, and all parent directories, for files named \*(C`$self->filter_file\*(C'. Each file is \s-1YAML\s0. The contents of the files will be merged (such that child settings override parent settings), and the merged hash will be returned.

What this is used for is up to the application or subclasses. Net::Jifty doesn't look at this at all, but it may in the future (such as for email and password).

RELATED TO Net::Jifty…

Jifty, Net::Hiveminder

AUTHORS

Shawn M Moore, \*(C`<sartak at bestpractical.com>\*(C'

Ruslan Zakirov, \*(C`<ruz at bestpractical.com>\*(C'

Jesse Vincent, \*(C`<jesse at bestpractical.com>\*(C'

CONTRIBUTORS

Andrew Sterling Hanenkamp, \*(C`<[email protected]>\*(C',

BUGS

Please report any bugs or feature requests to \*(C`bug-net-jifty at rt.cpan.org\*(C', or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Jifty <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Jifty>.

COPYRIGHT & LICENSE

Copyright 2007-2009 Best Practical Solutions.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.