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