File Coverage

blib/lib/Lingua/JA/Expand/DataSource/YahooSearch.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Lingua::JA::Expand::DataSource::YahooSearch;
2 1     1   787 use strict;
  1         2  
  1         34  
3 1     1   5 use warnings;
  1         2  
  1         30  
4 1     1   7 use base qw(Lingua::JA::Expand::DataSource);
  1         2  
  1         794  
5 1     1   6 use Carp;
  1         2  
  1         61  
6 1     1   1265 use LWP::UserAgent;
  1         630332  
  1         45  
7 1     1   2606 use XML::LibXML::Simple;
  0            
  0            
8              
9             __PACKAGE__->mk_accessors($_) for qw(_xml);
10              
11             sub new {
12             my $class = shift;
13             my $self = $class->SUPER::new(@_);
14             $self->_prepare;
15             return $self;
16             }
17              
18             sub extract_text {
19             my $self = shift;
20             my $word_ref = shift;
21             my $xml = $self->raw_data($word_ref);
22             if ( my $error_msg = $xml->{'Error'}->{'Message'} ) {
23             $error_msg =~ s/\n//g;
24             carp("Yahoo API returns error message : $error_msg") and return;
25             }
26             my $text;
27             if ( ref $xml->{ResultSet}->{Result} eq 'ARRAY' ) {
28             my @items = @{ $xml->{ResultSet}->{Result} };
29             for my $item (@items) {
30             $text .= $item->{Title} if $item->{Title};
31             $text .= ' ';
32             $text .= $item->{Summary} if $item->{Summary};
33             }
34             }
35             return \$text;
36             }
37              
38             sub raw_data {
39             my $self = shift;
40             if ( @_ > 0 || !$self->_xml ) {
41             my $word_ref = shift;
42             $$word_ref =~ s/([^\w ])/'%'.unpack('H2', $1)/eg;
43             $$word_ref =~ tr/ /+/;
44             my $url = $self->{url} . $$word_ref;
45             my $req = HTTP::Request->new( GET => $url );
46             my $res = $self->{user_agent}->request($req);
47             my $xml = XML::LibXML::Simple::XMLin( $res->content, keepRoot => 1, );
48             $self->_xml($xml);
49             }
50             else {
51             return $self->_xml;
52             }
53             }
54              
55             sub _prepare {
56             my $self = shift;
57             my %LWP_UserAgent_config = ();
58             if ( ref $self->config->{LWP_UserAgent} eq 'HASH' ) {
59             %LWP_UserAgent_config = %{ $self->config->{LWP_UserAgent} };
60             }
61             $self->{user_agent} = LWP::UserAgent->new(%LWP_UserAgent_config);
62             my $yahoo_api_appid = $self->config->{yahoo_api_appid};
63             croak("you must set your own 'yahoo_api_app_id'") if !$yahoo_api_appid;
64              
65             if ( $self->config->{yahoo_api_premium} ) {
66             $self->{url}
67             = 'http://search.yahooapis.jp/PremiumWebSearchService/V1/webSearch?appid='
68             . $yahoo_api_appid
69             . '&results=20&adult_ok=1&query=';
70             }
71             else {
72             $self->{url}
73             = 'http://search.yahooapis.jp/WebSearchService/V2/webSearch?appid='
74             . $yahoo_api_appid
75             . '&results=20&adult_ok=1&query=';
76             }
77             }
78              
79             1;
80              
81             __END__