X-Git-Url: http://erislabs.net/gitweb/?p=libwww-opensearch-perl.git;a=blobdiff_plain;f=lib%2FWWW%2FOpenSearch.pm;h=86ad45cc5be4d11e65f0ca65c0a24f82d4f8affc;hp=5985929798be9ffe00df43eb5ed44072bff22be9;hb=1fc70f0d6badbef9bb483990d761d1446d52cbb2;hpb=ec50391b25e981b1b9bae8e42e03c0fc9c218845 diff --git a/lib/WWW/OpenSearch.pm b/lib/WWW/OpenSearch.pm index 5985929..86ad45c 100644 --- a/lib/WWW/OpenSearch.pm +++ b/lib/WWW/OpenSearch.pm @@ -1,176 +1,156 @@ -package WWW::OpenSearch; - -use strict; -use warnings; - -use base qw( Class::Accessor::Fast ); - -use Carp; -use WWW::OpenSearch::Response; -use WWW::OpenSearch::Description; -use Encode qw( _utf8_off ); - -__PACKAGE__->mk_accessors( qw( description_url agent description ) ); - -our $VERSION = '0.06_02'; - -=head1 NAME - -WWW::OpenSearch - Search A9 OpenSearch compatible engines - -=head1 SYNOPSIS - - 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}; - } - -=head1 DESCRIPTION - -WWW::OpenSearch is a module to search A9's OpenSearch compatible search engines. See http://opensearch.a9.com/ for details. - -=head1 CONSTRUCTOR - -=head2 new( $url [, $useragent] ) - -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). Pass any -LWP::UserAgent compatible object if you wish to override the default -agent. - -=head1 METHODS - -=head2 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. - -=head2 search( $query [, \%params] ) - -Searches the engine for the given query using the given -search parameters. Valid search parameters include: - -=over 4 - -=item * startPage - -=item * totalResults - -=item * startIndex - -=item * itemsPerPage - -=back - -See http://opensearch.a9.com/spec/1.1/response/#elements for details. - -=head2 do_search( $url [, $method] ) - -Performs a request for the given URL and returns a -WWW::OpenSearch::Response object. Method defaults to 'GET'. - -=head1 ACCESSORS - -=head2 description_url( [$description_url] ) - -=head2 agent( [$agent] ) - -=head2 description( [$description] ) - -=head1 AUTHOR - -=over 4 - -=item * Tatsuhiko Miyagawa Emiyagawa@bulknews.netE - -=item * Brian Cassidy Ebricas@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy - -This library is free software; you can redistribute it and/or modify -it under the same terms as Perl itself. - -=cut - -sub new { - my( $class, $url, $agent ) = @_; - - croak( "No OpenSearch Description url provided" ) unless $url; - +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.11'; + +=head1 NAME + +WWW::OpenSearch - Search A9 OpenSearch compatible engines + +=head1 SYNOPSIS + + 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}; + } + +=head1 DESCRIPTION + +WWW::OpenSearch is a module to search A9's OpenSearch compatible search +engines. See http://opensearch.a9.com/ for details. + +=head1 CONSTRUCTOR + +=head2 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). + +=head1 METHODS + +=head2 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. + +=head2 search( $query [, \%params] ) + +Searches the engine for the given query using the given +search parameters. Valid search parameters include: + +=over 4 + +=item * startPage + +=item * totalResults + +=item * startIndex + +=item * itemsPerPage + +=back + +See http://opensearch.a9.com/spec/1.1/response/#elements for details. + +=head2 do_search( $url [, $method] ) + +Performs a request for the given URL and returns a +WWW::OpenSearch::Response object. Method defaults to 'GET'. + +=head1 ACCESSORS + +=head2 description_url( [$description_url] ) + +=head2 agent( [$agent] ) + +=head2 description( [$description] ) + +=head1 AUTHOR + +=over 4 + +=item * Tatsuhiko Miyagawa Emiyagawa@bulknews.netE + +=item * Brian Cassidy Ebricas@cpan.orgE + +=back + +=head1 COPYRIGHT AND LICENSE + +Copyright 2007 by Tatsuhiko Miyagawa and Brian Cassidy + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut + +sub new { + my( $class, $url ) = @_; + + croak( "No OpenSearch Description url provided" ) unless $url; + my $self = $class->SUPER::new; - unless( $agent ) { - require LWP::UserAgent; - $agent = LWP::UserAgent->new( agent => join( '/', ref $self, $VERSION ) ); + $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_url( $url ); - $self->agent( $agent ); - - $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 ) = @_; - - $params ||= { }; - $params->{ searchTerms } = $query; - _utf8_off( $params->{ searchTerms } ); - - my $url = $self->description->get_best_url; - return $self->do_search( $url->prepare_query( $params ), $url->method ); -} - -sub do_search { - my( $self, $url, $method ) = @_; - - $method = lc( $method ) || 'get'; - - my $response; - if( $method eq 'post' ) { - $response = $self->agent->post( @$url ); - } - else { - $response = $self->agent->$method( $url ); - } - - return WWW::OpenSearch::Response->new( $self, $response ); -} - -1; + + $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;