Snapple works by using the Perl package LWP::UserAgent
to send GET (and sometimes POST) requests to a Snap2 instance on one of our servers. The default is https://newdev.shodor.org/snap2, but you can change this. There is a PHP controller in Snap2 to which all these requests are sent and this script executes them, produces some sort of result data and sends that back using JSON, which snapple interprets and uses to report information back to the user. Because of this interfaces, you can use snapple anywhere you have access to Snap2 via a web browser. You will need to have certain Perl packages installed, though (as well as Perl itself, of course), so you cannot simply take the snapple script and the Snap2 directory and run snapple on any machine. Right now, it is only guaranteed to run on login.shodor.org, where all the packages are installed.
There is also a Perl API for Snap2, which snapple uses to implement most of its base functionality. This API can be used independently of the command line interface. You need only use the Snap2API
module and then call snapAPIInit()
. The API is object-oriented, but that API is built on top of a procedural API, which supports all of the functionality currently supported by snapple in any form. As such, you may occasionally have to use the procedural API (or fix the OO API to support the missing functionality :).
Snap2File
: base class for directories and resources (=SnapFile)Snap2Directory
: a Snap2 directory (= SnapDirectory)Snap2Resource
: a Snap2 resource (= SnapResource)Snap2Version
: a Snap2 version (= SnapVersion)submit()
, which is equivalent to SnapVersion::submit()
. Note that all method names in Perl are lowercase with underscores separating words. This is only true for methods. Functions that do not belong to objects are camelCased
and are almost always prefixed with "snap", e.g., snapInit()
.
To use the API, you must use the Snap2API
module in your Perl script and the Snap2/ directory from common/snap2/perl must be in the same directory as (or in the library path of) your script. You must then call snapAPIInit()
. And that's all that is required for set up! The following example illustrates that and also shows some usage of the API, which be explained below:
#! /usr/bin/perl use Snap2::API; snapAPIInit(); my $v = new Snap2::Version '//test/mediatest@1'; print "Ordinal: $v->ordinal\n";
This code creates a new version object based on the Snap2 path given and then displays the ordinal of that version, which will be '1'.