WWW::OpenSearch - Search A9 OpenSearch compatible engines


WWW-OpenSearch documentation Contained in the WWW-OpenSearch distribution.

Index


Code Index:

NAME

Top

WWW::OpenSearch - Search A9 OpenSearch compatible engines

SYNOPSIS

Top

    use WWW::OpenSearch;

    my $url = "http://bulkfeeds.net/opensearch.xml";
    my $engine = WWW::OpenSearch->new($url);

    my $name = $engine->description->ShortName;
    my $tags = $engine->description->Tags;

    # Perform search for "iPod"
    my $response = $engine->search("iPod");
    for my $item (@{$response->feed->items}) {
        print $item->{description};
    }

    # Retrieve the next page of results
    my $next_page = $response->next_page;
    for my $item (@{$next_page->feed->items}) {
        print $item->{description};
    }

DESCRIPTION

Top

WWW::OpenSearch is a module to search A9's OpenSearch compatible search engines. See http://opensearch.a9.com/ for details.

CONSTRUCTOR

Top

new( $url )

Constructs a new instance of WWW::OpenSearch using the given URL as the location of the engine's OpenSearch Description document (retrievable via the description_url accessor).

METHODS

Top

fetch_description( [ $url ] )

Fetches the OpenSearch Descsription found either at the given URL or at the URL specified by the description_url accessor. Fetched description may be accessed via the description accessor.

search( $query [, \%params] )

Searches the engine for the given query using the given search parameters. Valid search parameters include:

* startPage
* totalResults
* startIndex
* itemsPerPage

See http://opensearch.a9.com/spec/1.1/response/#elements for details.

do_search( $url [, $method] )

Performs a request for the given URL and returns a WWW::OpenSearch::Response object. Method defaults to 'GET'.

ACCESSORS

Top

description_url( [$description_url] )

agent( [$agent] )

description( [$description] )

AUTHOR

Top

Brian Cassidy <bricas@cpan.org>

Tatsuhiko Miyagawa <miyagawa@bulknews.net>

COPYRIGHT AND LICENSE

Top


WWW-OpenSearch documentation Contained in the WWW-OpenSearch distribution.

package WWW::OpenSearch;

use strict;
use warnings;

use base qw( Class::Accessor::Fast );

use Carp;
use WWW::OpenSearch::Agent;
use WWW::OpenSearch::Request;
use WWW::OpenSearch::Description;

use Encode ();

__PACKAGE__->mk_accessors( qw( description_url agent description ) );

our $VERSION = '0.16';

sub new {
    my ( $class, $url ) = @_;

    croak( "No OpenSearch Description url provided" ) unless $url;

    my $self = $class->SUPER::new;

    $self->description_url( $url );
    $self->agent( WWW::OpenSearch::Agent->new() );

    $self->fetch_description;

    return $self;
}

sub fetch_description {
    my ( $self, $url ) = @_;
    $url ||= $self->description_url;
    $self->description_url( $url );
    my $response = $self->agent->get( $url );

    unless ( $response->is_success ) {
        croak "Error while fetching $url: " . $response->status_line;
    }

    $self->description(
        WWW::OpenSearch::Description->new( $response->content ) );
}

sub search {
    my ( $self, $query, $params, $url ) = @_;

    $params ||= {};
    $params->{ searchTerms } = $query;
    Encode::_utf8_off( $params->{ searchTerms } );

    $url ||= $self->description->get_best_url;
    return $self->agent->search(
        WWW::OpenSearch::Request->new( $url, $params ) );
}

1;