[Bps-public-commit] r19659 - in Net-Google-Code/trunk/lib/Net/Google: . Code/Issue
sunnavy at bestpractical.com
sunnavy at bestpractical.com
Thu May 14 03:38:37 EDT 2009
Author: sunnavy
Date: Thu May 14 03:38:33 2009
New Revision: 19659
Added:
Net-Google-Code/trunk/lib/Net/Google/Code/Role/Pageable.pm
Modified:
Net-Google-Code/trunk/lib/Net/Google/Code.pm
Net-Google-Code/trunk/lib/Net/Google/Code/Issue/Search.pm
Log:
make a Pageable role to encapsulate the action of getting all the rows in all the pages. ( currently we just want the list of 1st columns, which is like the primary key for each row)
Modified: Net-Google-Code/trunk/lib/Net/Google/Code.pm
==============================================================================
--- Net-Google-Code/trunk/lib/Net/Google/Code.pm (original)
+++ Net-Google-Code/trunk/lib/Net/Google/Code.pm Thu May 14 03:38:33 2009
@@ -1,7 +1,7 @@
package Net::Google::Code;
use Moose;
-with 'Net::Google::Code::Role';
+with 'Net::Google::Code::Role', 'Net::Google::Code::Role::Pageable';
our $VERSION = '0.04';
@@ -112,62 +112,17 @@
sub load_downloads {
my $self = shift;
-
my $content = $self->fetch( $self->base_feeds_url . 'downloads/list' );
- require HTML::TreeBuilder;
- my $tree = HTML::TreeBuilder->new;
- $tree->parse_content($content);
- $tree->elementify;
+ my @names = $self->first_columns( $content );
my @downloads;
- my $pagination = $tree->look_down( class => 'pagination' );
- if ( my ( $start, $end, $total ) =
- $pagination->as_text =~ /(\d+)\s+-\s+(\d+)\s+of\s+(\d+)/ )
- {
-
- require Net::Google::Code::Download;
- my @name_tags = $tree->look_down( class => 'vt id col_0' );
- my @names;
- for my $tag (@name_tags) {
- my $name = $tag->as_text;
- $name =~ s/^\s+//;
- $name =~ s/\s+$//;
- my $download = Net::Google::Code::Download->new(
- name => $name,
- project => $self->project,
- );
- push @downloads, $download;
- }
-
- while ( scalar @downloads < $total ) {
- if ( $self->mech->follow_link( text_regex => qr/Next\s+/ ) ) {
- if ( $self->mech->response->is_success ) {
- my $content = $self->mech->content;
- my $tree = HTML::TreeBuilder->new;
- $tree->parse_content($content);
- my @name_tags = $tree->look_down( class => 'vt id col_0' );
- my @names;
- for my $tag (@name_tags) {
- my $name = $tag->as_text;
- $name =~ s/^\s+//;
- $name =~ s/\s+$//;
- my $download =
- Net::Google::Code::Download->new( name => $name );
- push @downloads, $download;
- }
- }
- else {
- die "failed to follow 'Next' link";
- }
- }
- else {
- warn "didn't find enough downloads";
- last;
- }
- }
- }
-
- for my $download (@downloads) {
+ require Net::Google::Code::Download;
+ for my $name ( @names ) {
+ my $download = Net::Google::Code::Download->new(
+ project => $self->project,
+ name => $name,
+ );
$download->load;
+ push @downloads, $download;
}
$self->downloads( \@downloads );
}
Modified: Net-Google-Code/trunk/lib/Net/Google/Code/Issue/Search.pm
==============================================================================
--- Net-Google-Code/trunk/lib/Net/Google/Code/Issue/Search.pm (original)
+++ Net-Google-Code/trunk/lib/Net/Google/Code/Issue/Search.pm Thu May 14 03:38:33 2009
@@ -2,7 +2,7 @@
use Moose;
use Params::Validate qw(:all);
use Moose::Util::TypeConstraints;
-with 'Net::Google::Code::Role';
+with 'Net::Google::Code::Role', 'Net::Google::Code::Role::Pageable';
our %CAN = (
'all' => 1,
@@ -41,10 +41,10 @@
if ( scalar @_ ) {
my %args = @_;
$self->_can( $args{_can} ) if defined $args{_can};
- $self->_q( $args{_q} ) if defined $args{_q};
+ $self->_q( $args{_q} ) if defined $args{_q};
}
- $self->fetch( $self->base_url . 'issues/list');
+ $self->fetch( $self->base_url . 'issues/list' );
my $mech = $self->mech;
$mech->submit_form(
form_number => 2,
@@ -53,65 +53,24 @@
'q' => $self->_q,
}
);
- die "Server threw an error "
- . $mech->response->status_line
- . 'when search'
+ die "Server threw an error " . $mech->response->status_line . 'when search'
unless $mech->response->is_success;
my $content = $mech->response->content;
if ( $mech->title =~ /Issue\s+(\d+)/ ) {
-# only get one ticket
- @{$self->ids} = $1;
- return 1;
+ # get only one ticket
+ $self->ids( [$1] );
}
elsif ( $mech->title =~ /Issues/ ) {
-# get a ticket list
- $self->ids([]); # clean previous ids
- require HTML::TreeBuilder;
- my $tree = HTML::TreeBuilder->new;
- $tree->parse_content($content);
- my $pagination = $tree->look_down( class => 'pagination' );
- if ( my ( $start, $end, $total ) =
- $pagination->content_array_ref->[0] =~
- /(\d+)\s+-\s+(\d+)\s+of\s+(\d+)/ )
- {
-
- my @ids = $tree->look_down( class => 'vt id col_0' );
- @ids =
- map { $_->content_array_ref->[0]->content_array_ref->[0] } @ids;
- $self->ids( [ @{$self->ids}, @ids ] );
-
- while ( scalar @{$self->ids} < $total ) {
- if ($mech->follow_link( text_regex => qr/Next\s+/ ) ) {
- if ( $mech->response->is_success ) {
- my $content = $mech->content;
- my $tree = HTML::TreeBuilder->new;
- $tree->parse_content($content);
- my @ids = $tree->look_down( class => 'vt id col_0' );
- @ids =
- map {
- $_->content_array_ref->[0]->content_array_ref->[0]
- } @ids;
- $self->ids( [ @{$self->ids}, @ids ] );
- }
- else {
- die "failed to follow link: Next";
- }
- }
- else {
- # XXX sometimes google's result number is wrong. google--
- warn "didn't find enough tickets, sometimes it's google's fault instead of ours ;)";
- last;
- }
- }
- }
- return 1;
+ # get a ticket list
+ my @ids = $self->first_columns($content);
+ $self->ids( \@ids );
}
else {
warn "no idea what the content like";
- return
+ return;
}
}
Added: Net-Google-Code/trunk/lib/Net/Google/Code/Role/Pageable.pm
==============================================================================
--- (empty file)
+++ Net-Google-Code/trunk/lib/Net/Google/Code/Role/Pageable.pm Thu May 14 03:38:33 2009
@@ -0,0 +1,101 @@
+package Net::Google::Code::Role::Pageable;
+use Moose::Role;
+use Params::Validate ':all';
+use WWW::Mechanize;
+with 'Net::Google::Code::Role::Fetchable';
+use Scalar::Util qw/blessed/;
+no Moose::Role;
+
+sub first_columns {
+ my $self = shift;
+ my $html = shift;
+ my $tree;
+ if ( blessed $html ) {
+ $tree = $html;
+ }
+ else {
+ require HTML::TreeBuilder;
+ $tree = HTML::TreeBuilder->new;
+ $tree->parse_content($html);
+ $tree->elementify;
+ }
+
+ my @columns;
+
+ my $pagination = $tree->look_down( class => 'pagination' );
+ if ( my ( $start, $end, $total ) =
+ $pagination->as_text =~ /(\d+)\s+-\s+(\d+)\s+of\s+(\d+)/ )
+ {
+ push @columns, $self->_first_columns($tree);
+
+ while ( scalar @columns < $total ) {
+ if ( $self->mech->follow_link( text_regex => qr/Next\s+/ ) ) {
+ if ( $self->mech->response->is_success ) {
+ push @columns,
+ $self->_first_columns( $self->mech->content );
+ }
+ else {
+ die "failed to follow 'Next' link";
+ }
+ }
+ else {
+ warn "didn't find enough rows";
+ last;
+ }
+ }
+ }
+ return @columns;
+}
+
+sub _first_columns {
+ my $self = shift;
+ my $html = shift;
+ my $tree;
+ if ( blessed $html ) {
+ $tree = $html;
+ }
+ else {
+ require HTML::TreeBuilder;
+ $tree = HTML::TreeBuilder->new;
+ $tree->parse_content($html);
+ $tree->elementify;
+ }
+
+ my @columns;
+ my @tags = $tree->look_down( class => 'vt id col_0' );
+ for my $tag (@tags) {
+ my $column = $tag->as_text;
+ $column =~ s/^\s+//;
+ $column =~ s/\s+$//;
+ push @columns, $column;
+ }
+ return @columns;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Net::Google::Code::Role::Pageable - Pageable Role
+
+
+=head1 DESCRIPTION
+
+=head1 INTERFACE
+
+=head2 first_columns
+
+=head1 AUTHOR
+
+sunnavy C<< <sunnavy at bestpractical.com> >>
+
+=head1 LICENCE AND COPYRIGHT
+
+Copyright 2009 Best Practical Solutions.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+
More information about the Bps-public-commit
mailing list