File Coverage

blib/lib/WebService/Google/Sets.pm
Criterion Covered Total %
statement 15 37 40.5
branch 0 4 0.0
condition n/a
subroutine 5 8 62.5
pod 2 2 100.0
total 22 51 43.1


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             WebService::Google::Sets - Perl access to Google Labs Sets site
4              
5             =head1 SYNOPSIS
6              
7             use WebService::Google::Sets;
8              
9             my @os_list = qw(Linux Windows Solaris);
10             my $expanded_os_list = get_gset(@os_list);
11              
12             # check something came back
13             die "No results returned from server" unless $expanded_os_list;
14              
15             foreach my $element (@$expanded_os_list) {
16             print $element, "\n";
17             }
18              
19             =head1 DESCRIPTION
20              
21             WebService::Google::Sets provides function based access to the Sets
22             service hosted at Google Labs.
23              
24             The Sets service attempts to expand the values you provide. The example
25             provided in the SYNOPSIS would return an array that included "Windows
26             NT", "HPUX" and "AIX" as values in addition to those supplied.
27              
28             =cut
29              
30              
31             package WebService::Google::Sets;
32 1     1   35747 use strict;
  1         3  
  1         37  
33 1     1   7 use warnings;
  1         2  
  1         33  
34 1     1   2270 use CGI;
  1         27638  
  1         8  
35 1     1   1466 use LWP;
  1         140447  
  1         55  
36              
37             require Exporter;
38 1     1   13 use vars qw(@EXPORT @EXPORT_OK @ISA $VERSION);
  1         2  
  1         427  
39              
40             #--------------------------------#
41              
42             @ISA = qw(Exporter);
43             @EXPORT = qw(get_gset);
44              
45             # do this or EXPORT as there are only two functions?
46             @EXPORT_OK = qw( get_small_gset get_large_gset );
47              
48             $VERSION = '0.03';
49              
50             # remove once checked in to svn.
51             #%EXPORT_TAGS = (
52             # all => [ @EXPORT_OK ],
53             # small_set => [ 'get_gset' ],
54             # large_set => [ 'get_large_gset' ],
55             #);
56              
57             # alias get_gset to the more common case.
58             sub get_gset; # quiet warnings;
59             *get_gset = \&get_small_gset;
60              
61             =head1 FUNCTIONS
62              
63             By default this module exports get_gset.
64              
65             =over 4
66              
67             =item get_gset
68              
69             A utility alias for get_small_gset.
70              
71             Exported by default.
72              
73             =item get_small_gset
74              
75             This function takes an array of terms to expand and attempts to expand them
76             using the Google Sets website.
77              
78             It returns undef on failure to connect to the remote server and an array
79             reference pointing to the expanded list on success.
80              
81             =cut
82              
83             sub get_small_gset {
84 0     0 1   _get_google_set("small", @_);
85             }
86              
87             #--------------------------------#
88              
89             =item get_large_gset
90              
91             This function takes an array of terms to expand and attempts to expand them
92             using the Google Sets website. It returns a larger list than get_small_gset
93             or get_gset.
94              
95             This function must be explictly imported.
96              
97             use WebService::Google::Sets qw(get_large_gset);
98              
99             It returns undef on failure to connect to the remote server and an array
100             reference pointing to the expanded list on success.
101              
102             =back
103              
104             =cut
105              
106             sub get_large_gset {
107 0     0 1   _get_google_set("large", @_);
108             }
109              
110             #--------------------------------#
111              
112             sub _get_google_set {
113 0     0     my $set_size = shift;
114 0           my @words = @_;
115 0           my @expanded_set;
116              
117 0           my $base_url = 'http://labs.google.com/sets?hl=en&';
118 0           my $sets_url; # this gets built and then escaped
119              
120             my $offset;
121 0           for my $word (@words) {
122 0           $offset++;
123 0           $sets_url .= qq{q$offset=$word&};
124             }
125              
126             # get the set size
127 0 0         if ($set_size eq "large") {
128 0           $sets_url .= 'btn=Large Set';
129             } else {
130 0           $sets_url .= 'btn=Small+Set+items+or+fewer';
131             }
132              
133             # encode the query string
134 0           my $q = new CGI($sets_url);
135 0           my $escaped_url = $q->query_string;
136              
137 0           my $browser = LWP::UserAgent->new();
138 0           my $response = $browser->get("$base_url$escaped_url");
139              
140             # return nothing if the server's playing up.
141 0 0         return undef unless $response->is_success;
142              
143 0           my $content = $response->content;
144              
145 0           while($content =~ m!
(.*)
!g) {
146 0           push(@expanded_set, $1);
147             }
148              
149 0           return \@expanded_set;
150             }
151              
152             #--------------------------------#
153              
154             1;
155              
156             =head1 COMMAND LINE PROGRAM
157              
158             A very simple script called F is supplied in the
159             distribution's F folder. It accepts between one and five values and
160             then attempts to expand them.
161              
162             =head1 DEPENDENCIES
163              
164             WebService::Google::Sets requires the following modules:
165              
166             CGI
167              
168             LWP
169              
170             =head1 LICENCE AND COPYRIGHT
171              
172             Copyright (C) 2006 Dean Wilson. All Rights Reserved.
173              
174             This module is free software; you can redistribute it and/or modify it
175             under the same terms as Perl itself.
176              
177             =head1 AUTHOR
178              
179             Dean Wilson
180              
181             =cut