diff options
-rw-r--r-- | lib/WebService/Google/Suggest.pm | 19 | ||||
-rw-r--r-- | t/01_suggest.t | 3 |
2 files changed, 14 insertions, 8 deletions
diff --git a/lib/WebService/Google/Suggest.pm b/lib/WebService/Google/Suggest.pm index 41b5251..9b2edba 100644 --- a/lib/WebService/Google/Suggest.pm +++ b/lib/WebService/Google/Suggest.pm @@ -28,14 +28,19 @@ sub complete { $response->is_success or croak "Google doesn't respond well: ", $response->code; my $content = $response->content(); - $content =~ /^window\.google\.ac\.sendRPCDone\(frameElement, ".*?", new Array\((.*?)\), new Array\((.*?)\), new Array\(""\)\)\;$/ + $content =~ /^window\.google\.ac\.\w+\(frameElement, ".*?", new Array\((.*?)\), new Array\(""\)\)\;$/ or croak "Google returns unrecognized format: $content"; - my($queries, $results) = ($1, $2); - my @queries = map { s/^"(.*?)"$/$1/; $_ } split /, /, $queries; - my @results = map { s/^"([\d,]+) results?"$/$1/; tr/,//d; $_+0 } - split /, /, $results; - return map { +{ query => $queries[$_], - results => $results[$_] } } 0..$#queries; + my @queries = map { s/^"(.*?)"$/$1/; $_ } split /, /, $1; + shift @queries; # new Array(2, ...) + my @results; + while (my($query, $count) = splice @queries, 0, 2) { + $count =~ s/^([\d,]+) results?$/$1/; + $count =~ tr/,//d; + $count += 0; # numify + push @results, { query => $query, results => $count }; + } + + return @results; } 1; diff --git a/t/01_suggest.t b/t/01_suggest.t index 7db220e..07dfc6a 100644 --- a/t/01_suggest.t +++ b/t/01_suggest.t @@ -1,5 +1,5 @@ use strict; -use Test::More tests => 4; +use Test::More tests => 5; BEGIN { use_ok 'WebService::Google::Suggest' } @@ -9,5 +9,6 @@ isa_ok($suggest->ua, "LWP::UserAgent", "ua() retuens LWP"); my @data = $suggest->complete("google"); is($data[0]->{query}, "google", "google completes to google"); +ok($data[0]->{results}, "google has more than 0 results"); is_deeply( [ $suggest->complete("udfg67a") ], [ ], "empty list" ); |