Changeset 976

Show
Ignore:
Timestamp:
08/20/08 19:03:38 (3 months ago)
Author:
mpaschal
Message:

provide some of the perldoc promised in the recipe guide

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ActionStreams/plugins/ActionStreams/lib/ActionStreams/Event.pm

    r729 r976  
    3693691; 
    370370 
     371__END__ 
     372 
     373=head1 NAME 
     374 
     375ActionStreams::Event - an Action Streams stream definition 
     376 
     377=head1 SYNOPSIS 
     378 
     379    # in plugin's config.yaml 
     380     
     381    profile_services: 
     382        example: 
     383             
     384 
     385    # in plugin's lib/My/Stream.pm 
     386 
     387    package My::Stream; 
     388    use base qw( ActionStreams::Event ); 
     389     
     390    __PACKAGE__->install_properties({ 
     391        class_type => 'example_streamid', 
     392    }); 
     393     
     394    sub update_events { 
     395        my $class = shift; 
     396        my %profile = @_; 
     397         
     398        # trivial example: save a random number 
     399        my $die_roll = int rand 20; 
     400        my %item = ( 
     401            title      => $die_roll, 
     402            identifier => $die_roll, 
     403        ); 
     404         
     405        return $class->build_results( 
     406            author => $profile{author}, 
     407            items  => [ \%item ], 
     408        ); 
     409    } 
     410     
     411    1; 
     412 
     413=head1 DESCRIPTION 
     414 
     415I<ActionStreams::Event> provides the basic implementation of an action stream. 
     416Stream definitions are engines for turning web resources into I<actions> (also 
     417called I<events>). This base class produces actions based on generic stream 
     418recipes defined in the MT registry, as well as providing the internal machinery 
     419for you to implement your own streams in Perl code. 
     420 
     421=head1 METHODS TO IMPLEMENT 
     422 
     423These are the methods one commonly implements (overrides) when implementing a 
     424stream subclass. 
     425 
     426=head2 C<$class-E<GT>update_events(%profile)> 
     427 
     428Fetches the web resource specified by the profile parameters, collects data 
     429from it into actions, and saves the action records for use later. Required 
     430members of C<%profile> are: 
     431 
     432=over 4 
     433 
     434=item * C<author> 
     435 
     436The C<MT::Author> instance for whom events should be collected. 
     437 
     438=item * C<ident> 
     439 
     440The author's identifier on the given service. 
     441 
     442=back 
     443 
     444Other information about the stream, such as the URL pattern into which the 
     445C<ident> parameter can be replaced, is available through the 
     446C<$class-E<gt>registry_entry()> method. 
     447 
     448=head2 C<$self-E<GT>as_html()> 
     449 
     450Returns the HTML version of the action, suitable for display to readers. 
     451 
     452The default implementation uses the stream's registry definition to construct 
     453the action: the author's name and the action's values as named in `html_params` 
     454are replaced into the stream's `html_form` setting. You need override it only 
     455if you have more complex requirements. 
     456 
     457=head1 AVAILABLE METHODS 
     458 
     459These are the methods provided by I<ActionStreams::Event> to perform common 
     460tasks. Call them from your overridden methods. 
     461 
     462=head2 C<$self-E<GT>set_values(\%values)> 
     463 
     464Stores the data given in C<%values> as members of this event. 
     465 
     466=head2 C<$class-E<GT>fetch_xpath(%param)> 
     467 
     468Returns the items discovered by scanning a web resource by the given XPath 
     469recipe. Required members of C<%param> are: 
     470 
     471=over 4 
     472 
     473=item * C<url> 
     474 
     475The address of the web resource to scan for events. The resource should be a 
     476valid XML document. 
     477 
     478=item * C<foreach> 
     479 
     480The XPath selector with which to select the individual events from the 
     481resource. 
     482 
     483=item * C<get> 
     484 
     485A hashref containing the XPath selectors with which to collect individual data 
     486for each item, keyed on the names of the fields to contain the data. 
     487 
     488=back 
     489 
     490C<%param> may also contain additional arguments for the C<ua()> method. 
     491 
     492Returned items are hashrefs containing the discovered fields, suitable for 
     493turning into C<ActionStreams::Event> records with the C<build_results()> 
     494method. 
     495 
     496=head2 C<$class-E<GT>fetch_scraper(%param)> 
     497 
     498Returns the items discovered by scanning by the given recipe. Required members 
     499of C<%param> are: 
     500 
     501=over 4 
     502 
     503=item * C<url> 
     504 
     505The address of the web resource to scan for events. The resource should be an 
     506HTML or XML document suitable for analysis by the C<Web::Scraper> module. 
     507 
     508=item * C<scraper> 
     509 
     510The C<Web::Scraper> scraper with which to extract item data from the specified 
     511web resource. See L<Web::Scraper> for information on how to construct a 
     512scraper. 
     513 
     514=back 
     515 
     516Returned items are hashrefs containing the discovered fields, suitable for 
     517turning into C<ActionStreams::Event> records with the C<build_results()> 
     518method. 
     519 
     520See also the below I<NOTE ON WEB::SCRAPER>. 
     521 
     522=head2 C<$class-E<GT>build_results(%param)> 
     523 
     524Converts a set of collected items into saved action records of type C<$class>. 
     525The required members of C<%param> are: 
     526 
     527=over 4 
     528 
     529=item * C<author> 
     530 
     531The C<MT::Author> instance whose action the items represent. 
     532 
     533=item * C<items> 
     534 
     535An arrayref of items to save as actions. Each item is a hashref containing the 
     536action data, keyed on the names of the fields containing the data. 
     537 
     538=back 
     539 
     540Optional parameters are: 
     541 
     542=over 4 
     543 
     544=item * C<profile> 
     545 
     546An arrayref describing the data for the author's profile for the associated 
     547stream, such as is returned by the C<MT::Author::other_profile()> method 
     548supplied by the Action Streams plugin. 
     549 
     550The profile member is not used directly by C<build_results()>; they are only 
     551passed to callbacks. 
     552 
     553=item * C<stream> 
     554 
     555A hashref containing the settings from the registry about the stream, such as 
     556is returned from the C<registry_entry()> method. 
     557 
     558=back 
     559 
     560=head2 C<$class-E<GT>ua(%param)> 
     561 
     562Returns the common HTTP user-agent, an instance of C<LWP::UserAgent>, with 
     563which you can fetch web resources. No arguments are required; possible optional 
     564parameters are: 
     565 
     566=over 4 
     567 
     568=item * C<default_useragent> 
     569 
     570If set, the returned HTTP user-agent will use C<LWP::UserAgent>'s default 
     571identifier in the HTTP C<User-Agent> header. If omitted, the UA will use the 
     572Action Streams identifier of C<mt-actionstreams-lwp/I<version>>. 
     573 
     574=back 
     575 
     576=head2 C<$self-E<GT>author()> 
     577 
     578Returns the C<MT::Author> instance associated with this event, if its 
     579C<author_id> field has been set. 
     580 
     581=head2 C<$class-E<GT>install_properties(\%properties)> 
     582 
     583I<TODO> 
     584 
     585=head2 C<$class-E<GT>install_meta(\%properties)> 
     586 
     587I<TODO> 
     588 
     589=head2 C<$class-E<GT>registry_entry()> 
     590 
     591Returns the registry data for the stream represented by C<$class>. 
     592 
     593=head2 C<$class-E<GT>classes_for_type($service_id)> 
     594 
     595Given a profile service ID (that is, a key from the C<profile_services> section 
     596of the registry), returns a list of stream classes for scanning that service's 
     597streams. 
     598 
     599=head1 NOTE ON WEB::SCRAPER 
     600 
     601The C<Web::Scraper> module is powerful, but it has complex dependencies. While 
     602its pure Perl requirements are bundled with the Action Streams plugin, it also 
     603requires a compiled XML module. Also, because of how its syntax works, you must 
     604directly C<use> the module in your own code, contrary to the Movable Type idiom 
     605of using C<require> so that modules are loaded only when they are sure to be 
     606used. 
     607 
     608If you attempt load C<Web::Scraper> in the normal way, but C<Web::Scraper> is 
     609unable to load due to its missing requirement, whenever the plugin attempts to 
     610load your scraper, the entire plugin will fail to load. 
     611 
     612Therefore the C<ActionStreams::Scraper> wrapper module is provided for you. If 
     613you need to load C<Web::Scraper> so as to make a scraper to pass 
     614C<ActionStreams::Event::fetch_scraper()> method, instead write in your module: 
     615 
     616    use ActionStreams::Scraper; 
     617 
     618This module provides the C<Web::Scraper> interface, but if C<Web::Scraper> is 
     619unable to load, the error will be thrown when your module tries to I<use> it, 
     620rather than when you I<load> it. That is, if C<Web::Scraper> can't load, no 
     621errors will be thrown to end users until they try to use your stream. 
     622 
     623=head1 AUTHOR 
     624 
     625Mark Paschal E<lt>mark@sixapart.comE<gt> 
     626 
     627=cut 
     628