Skip Menu |

This queue is for tickets about the Scrappy CPAN distribution.

Report information
The Basics
Id: 67246
Status: resolved
Priority: 0/
Queue: Scrappy

People
Owner: Nobody in particular
Requestors:
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



From: FOOLISH [...] cpan.org
Subject: I'd like to be able to accumulate grabbed things
#vim:ft=perl use Test::More; use Scrappy ':syntax'; my $expected_critters = [ { 'views' => '69', 'display_name' => 'Sexy Coder', 'username' => 'sexycoder' }, { 'views' => '22', 'display_name' => 'Lady Feline', 'username' => 'ladyfeline' }, { 'views' => '0', 'display_name' => 'Pesky', 'username' => 'peskycat' }, { 'views' => '1', 'display_name' => 'Nobody Cares', 'username' => 'nobodycares' }, ]; my $html = sprintf "<html><ul>%s</ul></html>", join "\n", map { my ($user,$name, $views) = @{ $_ }{ qw[ username display_name views ]}; $s=$views == 1 ? '' : 's'; qq[ <li> <a href="/~${user}">View <span class="username">${name}</span>'s profile</a> (viewed <span class="views">$views</span> time$s) </li> ]# vim's confused! } @$expected_critters; # diag "the html looks like this: $html or something"; var critters => grab '//li', sub { my %critter; @critter{ qw[ display_name views ]} = map $_->as_text, $_[0]->look_down(_tag => 'span'); $_[0]->look_down (_tag => 'a', sub { ($critter{ username }) = shift->attr('href') =~ /~(\w+)/ }); \%critter; }, $html; is_deeply( $expected_critters, var->{critters}, "It'd be great if grab accumunated stuff returned by the callback when var'ed" ); grab '//li', sub { my %critter; @critter{ qw[ display_name views ]} = map $_->as_text, $_[0]->look_down(_tag => 'span'); $_[0]->look_down (_tag => 'a', sub { ($critter{ username }) = shift->attr('href') =~ /~(\w+)/ }); var more_critters => [ @{ var->{more_critters} || [] }, \%critter ]; }, $html; is_deeply( $expected_critters, var->{more_critters}, "I can accumulate them by myself, but it's pesky" );
Checkout the all new Scrappy, e.g. $s = Scrappy->new; $s->get('http://search.cpan.org/recent'); $s->stash( 'links' => $s->scrape('#cpansearch li a') ); On Mon Apr 04 23:55:21 2011, FOOLISH wrote: Show quoted text
> #vim:ft=perl > use Test::More; > use Scrappy ':syntax'; > > my $expected_critters = [ > { > 'views' => '69', > 'display_name' => 'Sexy Coder', > 'username' => 'sexycoder' > }, > { > 'views' => '22', > 'display_name' => 'Lady Feline', > 'username' => 'ladyfeline' > }, > { > 'views' => '0', > 'display_name' => 'Pesky', > 'username' => 'peskycat' > }, > { > 'views' => '1', > 'display_name' => 'Nobody Cares', > 'username' => 'nobodycares' > }, > ]; > > my $html = sprintf "<html><ul>%s</ul></html>", > join "\n", map { > my ($user,$name, $views) = @{ $_ }{ qw[ username display_name > views ]}; > $s=$views == 1 ? '' : 's'; > qq[ > <li> > <a href="/~${user}">View <span > class="username">${name}</span>'s profile</a> > (viewed <span class="views">$views</span> time$s) > </li> ]# vim's confused! > } @$expected_critters; > > # diag "the html looks like this: $html or something"; > var critters => grab '//li', sub { > my %critter; > @critter{ qw[ display_name views ]} = > map $_->as_text, > $_[0]->look_down(_tag => 'span'); > > $_[0]->look_down (_tag => 'a', sub { > ($critter{ username }) = shift->attr('href') =~ /~(\w+)/ > }); > \%critter; > > }, $html; > > is_deeply( > $expected_critters, > var->{critters}, > "It'd be great if grab accumunated stuff returned by the callback > when var'ed" > ); > > grab '//li', sub { > my %critter; > @critter{ qw[ display_name views ]} = > map $_->as_text, > $_[0]->look_down(_tag => 'span'); > > $_[0]->look_down (_tag => 'a', sub { > ($critter{ username }) = shift->attr('href') =~ /~(\w+)/ > }); > > var more_critters => [ > @{ var->{more_critters} || [] }, > \%critter > ]; > }, $html; > > is_deeply( $expected_critters, > var->{more_critters}, > "I can accumulate them by myself, but it's pesky" > ); >