use base qw( HTTP::Response Class::Accessor::Fast );
use XML::Feed;
-use URI;
use Data::Page;
+use WWW::OpenSearch::Agent;
+use WWW::OpenSearch::Request;
-__PACKAGE__->mk_accessors( qw( feed pager parent ) );
+__PACKAGE__->mk_accessors( qw( feed pager ) );
=head1 NAME
=head1 CONSTRUCTOR
-=head2 new( $parent, $response )
+=head2 new( $response )
-Constructs a new instance of WWW::OpenSearch::Response. Arguments
-include the WWW::OpenSearch object which initiated the search (parent)
-and the HTTP::Response returned by the search request.
+Constructs a new instance of WWW::OpenSearch::Response from the
+WWWW::OpenSearch:Response returned by the search request.
=head1 METHODS
=head2 pager( )
-=head2 parent( )
-
=head1 AUTHOR
=over 4
=head1 COPYRIGHT AND LICENSE
-Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy
+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.
sub new {
my $class = shift;
- my $parent = shift;
my $response = shift;
my $self = bless $response, $class;
- $self->parent( $parent );
return $self unless $self->is_success;
$self->parse_response;
my $feed = $self->feed;
my $format = $feed->format;
- my $ns = $self->parent->description->ns;
+ my $ns = $self->request->opensearch_url->ns;
# TODO
# adapt these for any number of opensearch elements in
my $pagermethod = "${direction}_page";
my $page = $pager->$pagermethod;
return unless $page;
-
- my $request = $self->request;
- my $method = lc $request->method;
- if( $method ne 'post' ) { # force query build on POST
- my $link = $self->_get_link( $direction );
- return $self->parent->do_search( $link, $method ) if $link;
- }
-
- my $template = $self->parent->description->get_best_url;
- my( $param, $query );
- if( $method eq 'post' ) {
- my $uri = URI->new( 'http://foo.com/?' . $request->content );
- $query = { $uri->query_form };
- }
- else {
- $query = { $self->request->uri->query_form };
- }
-
- if( $param = $template->macros->{ startPage } ) {
- $query->{ $param } = $pager->$pagermethod
- }
- elsif( $param = $template->macros->{ startIndex } ) {
- if( $query->{ $param } ) {
- $query->{ $param } = $direction eq 'previous'
- ? $query->{ $param } -= $pager->entries_per_page
- : $query->{ $param } += $pager->entries_per_page;
- }
- else {
- $query->{ $param } = $direction eq 'previous'
- ? 1
- : $pager->entries_per_page + 1;
+ my $params;
+ my $osu = $self->request->opensearch_url;
+
+# this code is too fragile -- deparse depends on the order of query
+# params and the like. best just to use the last query params and
+# do the paging from there.
+#
+# if( lc $osu->method ne 'post' ) { # force query build on POST
+# my $link = $self->_get_link( $direction );
+# if( $link ) {
+# $params = $osu->deparse( $link );
+# }
+# }
+
+ # rebuild the query
+ if( !$params ) {
+ $params = $self->request->opensearch_params;
+
+ # handle paging via a page #
+ $params->{ startPage } = $page;
+
+ # handle paging via an index
+ if( exists $params->{ startIndex } ) {
+ # start index is pre-existing
+ if( $params->{ startIndex } ) {
+ if( $direction eq 'previous' ) {
+ $params->{ startIndex } -= $pager->entries_per_page
+ }
+ else {
+ $params->{ startIndex } += $pager->entries_per_page;
+ }
+ }
+ # start index did not exist previously
+ else {
+ if( $direction eq 'previous' ) {
+ $params->{ startIndex } = 1
+ }
+ else {
+ $params->{ startIndex } = $pager->entries_per_page + 1;
+ }
+
+ }
}
}
- return $self->parent->do_search( $template->prepare_query( $query ), $method );
+ my $agent = WWW::OpenSearch::Agent->new;
+ return $agent->search( WWW::OpenSearch::Request->new(
+ $osu, $params
+ ) );
}
sub _get_link {
return unless $feed;
for( $feed->link ) {
- return $_->get( 'href' ) if $_->get( 'rel' ) eq $type;
+ return $_->href if $_->rel eq $type;
}
return;