| 1 | package WWW::SPOJ::User; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | WWW::SPOJ::User - Object representation of a SPOJ user | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use WWW::SPOJ; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $user = new WWW::SPOJ::User('john_jones'); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | See L<WWW::SPOJ> for a description of this project. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use Class::Accessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | use HTML::TableExtract; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 3 | ▶ | use WWW::SPOJ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | our @ISA = qw(Class::Accessor); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | my @user_data = qw(name username country institution email motto); | |||||||||||||||||||||||||||
| 32 | 3 | __PACKAGE__->mk_ro_accessors(@user_data); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | This module declares one constructor: | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over 4 | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item WWW::SPOJ::User->new( USERNAME ) | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Constructs a L<WWW::SPOJ::User> to represent the user with the specified | ||||||||||||||||||||||||||||
| 43 | username. Returns C<undef> if such a user doesn't exist, so you can | ||||||||||||||||||||||||||||
| 44 | assume that all L<WWW::SPOJ::User> objects are valid users. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =back | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=40.1
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 2 | my($class, $username) = @_; | |||||||||||||||||||||||||||
| 52 | 2 | my $self = undef; | |||||||||||||||||||||||||||
| 53 | 2 | ▶ | if($username =~ /^[a-z][a-z0-9_]+$/si) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | $username = lc($username); | |||||||||||||||||||||||||||
| 55 | 1 | my $response = WWW::SPOJ::ua()->get(sprintf('%susers/%s/', | |||||||||||||||||||||||||||
| 56 | WWW::SPOJ::service(), $username)); | ||||||||||||||||||||||||||||
| 57 | 1 | ▶ | $response->is_success or croak $response->status_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $content = $response->decoded_content || $response->content | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response->decoded_content || $response->content
MC/DC0%0/2
$response->decoded_content || $response->content
$response->decoded_content
$response->content
Truth table0%0/3
A || B
A= $response->decoded_contentB= $response->content
| |||||||||||||||||||||||||||||
| 59 | or croak 'Problem reading page content'; | ||||||||||||||||||||||||||||
| 60 | 0 | my $te = new HTML::TableExtract; | |||||||||||||||||||||||||||
| 61 | 0 | $te->parse($content); | |||||||||||||||||||||||||||
| 62 | 0 | eval { | |||||||||||||||||||||||||||
| 63 | 0 | local %_ = map { $_->[0] =~ s/\/.*//; | |||||||||||||||||||||||||||
| 64 | 0 | lc(join('', $_->[0] =~ /[a-z]+/ig)) => $_->[1] | |||||||||||||||||||||||||||
| 65 | } $te->table(1, 1)->rows; | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $_{name} = $1 if ($te->table(0, 0)->rows)[1]->[1] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | =~ /^\s(.*)\'s user data/s; | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | $self = {map {$_ => $_{$_} || ''} @user_data} | ||||||||||||||||||||||||||
|
Condition0%0/2
$_{$_} || ''
MC/DC0%0/1
$_{$_} || ''
$_{$_}
Truth table0%0/2
A
A= $_{$_}
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if defined $_{username} && $_{username} eq $username; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_{"username"} and $_{"username"} eq $username
MC/DC0%0/2
defined $_{"username"} and $_{"username"} eq $username
defined $_{"username"}
$_{"username"} eq $username
Truth table0%0/3
A and B
A= defined $_{"username"}B= $_{"username"} eq $username
| |||||||||||||||||||||||||||||
| 70 | }; | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | croak 'Problem parsing page content' if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $self->{email} =~ s/\[at\]/@/; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | bless($self, $class) if $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | return $self; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 METHODS | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =over 4 | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =item $user->name( ) | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Returns the user's real name (what the user gave for the "Your name:" field | ||||||||||||||||||||||||||||
| 85 | on the user data update page). | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =item $user->username( ) | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Returns the user's username. Should be the same as what was passed to the | ||||||||||||||||||||||||||||
| 90 | constructor when this object was created, except for possible differences | ||||||||||||||||||||||||||||
| 91 | in case. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item $user->country( ) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Returns the country the user has chosen to represent. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =item $user->institution( ) | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | Returns the user's institution or the empty string if the user didn't specify | ||||||||||||||||||||||||||||
| 100 | anything. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item $user->email( ) | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Returns the user's e-mail address. I hope you're not checking this so you | ||||||||||||||||||||||||||||
| 105 | can spam. Probability suggests you'll get the empty string from this method | ||||||||||||||||||||||||||||
| 106 | because most users will likely choose not to make their e-mail address | ||||||||||||||||||||||||||||
| 107 | publicly visible. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =item $user->motto( ) | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Returns the user's motto or the empty string if the user didn't specify | ||||||||||||||||||||||||||||
| 112 | anything. Many users put a URL here. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =back | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | 1; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||||||||||