File Coverage

blib/lib/WWW/Ruten.pm
Criterion Covered Total %
statement 49 51 96.0
branch 1 2 50.0
condition 2 5 40.0
subroutine 11 11 100.0
pod 3 3 100.0
total 66 72 91.6


line stmt bran cond sub pod time code
1             package WWW::Ruten;
2             {
3             $WWW::Ruten::VERSION = '0.03';
4             }
5             # ABSTRACT: Scripting www.ruten.com.tw
6              
7 2     2   70840 use 5.008;
  2         9  
  2         68  
8 2     2   10 use strict;
  2         4  
  2         59  
9 2     2   10 use warnings;
  2         8  
  2         72  
10 2     2   2584 use WWW::Mechanize 1.66;
  2         675051  
  2         129  
11 2     2   27 use Encode;
  2         4  
  2         282  
12 2     2   3905 use HTML::TreeBuilder::XPath;
  2         349029  
  2         31  
13 2     2   3087 use HTML::Selector::XPath;
  2         12346  
  2         1121  
14              
15             sub new {
16 1     1 1 14 my ($class) = @_;
17 1         3 my $self = {};
18 1         3 bless $self, $class;
19 1         4 return $self;
20             }
21              
22             sub search {
23 1     1 1 9 my ($self, $term, @params) = @_;
24 1         10 $self->{search_params} = {
25             term => $term
26             };
27              
28 1         4 return $self;
29             }
30              
31             sub each {
32 1     1 1 12 my ($self, $cb) = @_;
33 1         7 $self->_do_search();
34              
35 1         3 foreach my $result (@{ $self->{search_results} }) {
  1         4  
36 0         0 $cb->($result);
37             }
38 1         3 return $self;
39             }
40              
41             sub _do_search {
42 1     1   3 my ($self) = @_;
43 1 50       6 die unless defined $self->{search_params}{term};
44              
45 1   33     14 $self->{mech} ||= WWW::Mechanize->new;
46              
47 1         33407 my $mech = $self->{mech};
48 1         9 $mech->get("http://www.ruten.com.tw");
49 1         1425516 $mech->submit_form(
50             form_name => "srch",
51             fields => {
52             k => $self->{search_params}{term}
53             }
54             );
55              
56 1         1457522 my $content = $mech->content;
57              
58 1         42 my @results = ();
59 1         21 my $html = HTML::TreeBuilder::XPath->new;
60 1         492 $html->parse($content);
61              
62 1         55382 my $selector = HTML::Selector::XPath->new("ul.items h3.title a");
63              
64 1         13 for my $node ($html->findnodes($selector->to_xpath)) {
65 0         0 push @results, {
66             url => $node->attr("href"),
67             title => join("", $node->content_list)
68             }
69             }
70              
71 1   50     48120 $self->{search_results} ||=[];
72 1         2 push @{ $self->{search_results} }, @results;
  1         3  
73              
74 1         211 return $self;
75             }
76              
77             1;
78              
79              
80             __END__