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

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

handle POST properly... i think.

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 params 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
58     return $self;
59 }
60
61 sub parse_macros {
62     my $self = shift;
63    
64     my %query = $self->method eq 'post'
65         ? %{ $self->params }
66         : $self->template->query_form;
67    
68     my %macros;
69     for( keys %query ) {
70         if( $query{ $_ } =~ /^{(.+)}$/ ) {
71             $macros{ $1 } = $_;
72         }
73     }
74    
75     $self->macros( \%macros );
76 }
77
78 sub prepare_query {
79     my( $self, $params ) = @_;
80     my $url   = $self->template->clone;
81    
82     $params->{ startIndex     } ||= 1;
83     $params->{ startPage      } ||= 1;
84     $params->{ language       } ||= '*';
85     $params->{ outputEncoding } ||= 'UTF-8';
86     $params->{ inputEncoding  } ||= 'UTF-8';
87    
88     my $macros = $self->macros;
89
90     # attempt to handle POST
91     if( $self->method eq 'post' ) {
92         my $post = $self->params;
93         for( keys %macros ) {
94             $post->{ $macros->{ $_ } } = $params->{ $_ };
95         }
96         return [ $url, $post ];
97     }
98
99     my $query = { $url->query_form };
100     for( keys %$macros ) {
101         $query->{ $macros->{ $_ } } = $params->{ $_ };
102     }
103    
104     $url->query_form( $query );
105     return $url;
106 }
107
108 1;
Note: See TracBrowser for help on using the browser.