root/WWW-OpenSearch/branches/WWW-OpenSearch-rewrite/lib/WWW/OpenSearch/Url.pm

Revision 1802 (checked in by bricas, 15 years ago)

Initial Import of rewrite branch

Line 
1 package WWW::OpenSearch::Url;
2
3 use base qw( Class::Accessor::Fast );
4
5 use URI;
6 use URI::Escape;
7
8 __PACKAGE__->mk_accessors( qw( type template method macros ) );
9
10 sub new {
11     my( $class, %options ) = @_;
12    
13     $options{ method } ||= 'GET';
14     $options{ template } = URI->new( $options{ template } );
15    
16     my $self = $class->SUPER::new( \%options );
17     $self->parse_macros;
18     return $self;
19 }
20
21 sub parse_macros {
22     my $self = shift;
23    
24     my %query = $self->template->query_form;
25    
26     my %macros;
27     for( keys %query ) {
28         if( $query{ $_ } =~ /^{(.+)}$/ ) {
29             $macros{ $1 } = $_;
30         }
31     }
32    
33     $self->macros( \%macros );
34 }
35
36 sub prepare_query {
37     my( $self, $params ) = @_;
38     my $url   = $self->template->clone;
39     my %query = $url->query_form;
40    
41     $params->{ startIndex     } ||= 1;
42     $params->{ startPage      } ||= 1;
43     $params->{ language       } ||= '*';
44     $params->{ outputEncoding } ||= 'UTF-8';
45     $params->{ inputEncoding  } ||= 'UTF-8';
46    
47     my $macros = $self->macros;
48     for( keys %$macros ) {
49         $query{ $macros->{ $_ } } = $params->{ $_ };
50     }
51    
52     $url->query_form( \%query );
53     return $url;
54 }
55
56 1;
Note: See TracBrowser for help on using the browser.