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

Revision 1825 (checked in by bricas, 14 years ago)

minor pod fix

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 =head1 NAME
11
12 WWW::OpenSearch::Url
13
14 =head1 SYNOPSIS
15
16 =head1 DESCRIPTION
17
18 =head1 CONSTRUCTOR
19
20 =head2 new( [%options] )
21
22 =head1 METHODS
23
24 =head2 parse_macros( )
25
26 =head2 prepare_query( [ \%params ] )
27
28 =head1 ACCESSORS
29
30 =head1 AUTHOR
31
32 =over 4
33
34 =item * Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
35
36 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
37
38 =back
39
40 =head1 COPYRIGHT AND LICENSE
41
42 Copyright 2006 by Tatsuhiko Miyagawa and Brian Cassidy
43
44 This library is free software; you can redistribute it and/or modify
45 it under the same terms as Perl itself.
46
47 =cut
48
49 sub new {
50     my( $class, %options ) = @_;
51    
52     $options{ method } ||= 'GET';
53     $options{ template } = URI->new( $options{ template } );
54    
55     my $self = $class->SUPER::new( \%options );
56     $self->parse_macros;
57     return $self;
58 }
59
60 sub parse_macros {
61     my $self = shift;
62    
63     my %query = $self->template->query_form;
64    
65     my %macros;
66     for( keys %query ) {
67         if( $query{ $_ } =~ /^{(.+)}$/ ) {
68             $macros{ $1 } = $_;
69         }
70     }
71    
72     $self->macros( \%macros );
73 }
74
75 sub prepare_query {
76     my( $self, $params ) = @_;
77     my $url   = $self->template->clone;
78     my %query = $url->query_form;
79    
80     $params->{ startIndex     } ||= 1;
81     $params->{ startPage      } ||= 1;
82     $params->{ language       } ||= '*';
83     $params->{ outputEncoding } ||= 'UTF-8';
84     $params->{ inputEncoding  } ||= 'UTF-8';
85    
86     my $macros = $self->macros;
87     for( keys %$macros ) {
88         $query{ $macros->{ $_ } } = $params->{ $_ };
89     }
90    
91     $url->query_form( \%query );
92     return $url;
93 }
94
95 1;
Note: See TracBrowser for help on using the browser.