The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

E2::Node - A module for loading nodes from everything2.com based on title or node_id

SYNOPSIS

        use E2::Node;

        my $node = new E2::Node;
        $node->login( "username", "password" ); # See E2::Interface

        if( $node->load( "Butterfinger McFlurry" ) ) {
                print "Title: " . $node->title;
                print "\nAuthor: " . $node->author;
                print "\nCreatetime: " . $node->createtime;
        }

        # List softlinks
        
        print "\nSoftlinks:\n";
        foreach( $node->list_softlinks ) {
                print $_->{title} . "\n";
        }

DESCRIPTION

This module is the base class for all e2interface modules that load data from everything2.com based on title or node_id. It allows access to the data in those nodes, and its subclasses provide data exclusive to their particular node types.

This module provides generic methods to load and parse nodes, and is also capable of detecting the type of node passed to it and re-blessing itself into the proper subclass (see the documentation for load and autodetect for more information on this).

This module inherits E2::Interface.

CONSTRUCTOR

new

new creates a new E2::Node object.

METHODS

$node->title
$node->node_id
$node->author
$node->author_id
$node->createtime
$node->type

These methods return, respectively, the title of the node, the node_id, the author, the user_id of the author, the createtime (in the format "YYYY-MM-DD HH:MM:SS"), or the type, of the current node. They return undef if there is no node currently loaded.

$node->exists

Boolean: "Does this node exist?"

$node->load_from_xml XML_STRING

These methods load a node based upon, respectively, TITLE, NODE_ID, or XML_STRING. They populate a number of internal variables, which are accessable through the access methods listed above.

load_from_xml expects to be passed an XML string of the type generated by a query to everything2.com with 'displaytype=xmltrue' set.

load and load_by_id fetch the appropriate page from everything2.com. For load, if TYPE is specified, it fetches the node of that type. If no appropriate node of that type exists, they return undef. Otherwise, they return true.

If SOFTLINK is specified (as a node_id), $node attempts to create a softlink between SOFTLINK and the node it is loading. If SOFTLINK is -1, $node attempts to create a softlink between the previously-loaded node and the node it is loading.

No softlinks will be created unless SOFTLINK is specified.

If the object that's doing the loading is of this class, rather than one of its descendants, the load methods will attempt to determine from the XML the type of node they were passed, and then re-bless the current object into that class. These are the classes an E2::Node object will be re-blessed into based on node type:

        e2node          => E2::E2Node
        writeup         => E2::E2Node   # NOTE: Not E2::Writeup
        user            => E2::User
        usergroup       => E2::Usergroup
        room            => E2::Room
        superdoc        => E2::Superdoc
        superdocnolinks => E2::Superdoc

And here's an example:

        my $node = new E2::Node;

        # At ths point, $node is of class 'E2::Node'

        $node->load( "Brian Eno", "e2node" );

        # Now $node is of class 'E2::E2Node' and has access to
        # all the methods of that class.

Note: once an object has been re-blessed, it is a member of the new class, and therefore will generate an exception if it calls one of the load methods on a different type of node.

        # (continued from above)

        $node->load( "nate", "user" ); # throws 'Wrong node type:'

Once the object has been re-blessed, if we wish to autodetect node type again, we must call $node->autodetect.

Exceptions: 'Unable to process request', 'Wrong node type:', 'Parse error:', 'Invalid node type:'

$node->autodetect

This method is used enable nodetype autodetection on an object that would normally not allow it.

Objects of class E2::Node autodetect automatically. Derived objects, however, throw an exception whenever they load a node of an incompatible type. In most cases, we don't want, say, an E2::User to just become an E2::Superdoc. autodetect is for times that we do.

When would this be useful? Well, mostly, this is important if we want to use an object for autodetection more than once. Once an E2::Node has autodetected, it becomes the detected nodetype (it is re-blessed) and therefore loses the ability to autodetect.

And that's where autodetect comes in. It blesses the object back into the E2::Node class. As a side-effect, most of its methods become unavailable.

Example use:

        my $node = new E2::Node;

        for(;;) {
                print "Input node title (or RETURN to end):";
                my $title = <STDIN>;
                chomp $title;

                if( ! $title ) { last }

                print "Input node type (or RETURN for none):";
                my $type = <STDIN>;
                chomp $type;

                $node->autodetect;
                $node->load( $title, $type )
                        or die "Unable to load node";

                &display_node( $node );
        }

        sub display_node {
                my $node = shift;

                # Print node info shared by all types of nodes

                print   "Node title: " . $node->title;
                print "\nNode type:  " . $node->type . "\n\n";

                # Print node info specific to each type

                if( $node->type eq 'e2node' ) {
                        # ...
                } elsif( $node->type eq 'user' ) {
                        # ...
                } 

                # There should be a bunch of elsif ... statements
                # here, each displaying a specific type of node
                # ...
        }

SEE ALSO

E2::Interface, E2::Node, E2::E2Node, E2::Writeup, E2::Superdoc, E2::Room, E2::Usergroup, http://everything2.com, http://everything2.com/?node=clientdev

AUTHOR

Jose M. Weeks <jose@joseweeks.com> (Simpleton on E2)

COPYRIGHT

This software is public domain.