Lighthouse/User.pm

filter: Statementstmt 73.6% 42 / 57 Branchbran 40.0% 4 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 67.8% 57 / 84 CC 8 SCAR 14.6
CC
8
Cov
66%
update
12.0
memberships
4.2
load
2.0
CRAP 4.3
« Lighthouse/Project/Ticket.pm ↑ Summary Lighthouse/Project/TicketBin.pm »
1 package Net::Lighthouse::User;
2 11 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Net::Lighthouse::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 extends 'Net::Lighthouse::Base';
6  
7 # read only attr
8 11 has 'id' => (
9 isa => 'Int',
10 is => 'ro',
11 );
12  
13 11 has 'avatar_url' => (
14 isa => 'Maybe[Str]',
15 is => 'ro',
16 );
17  
18 # read&write attr
19 11 has [qw/name job website/] => (
20 isa => 'Maybe[Str]',
21 is => 'rw',
22 );
23  
24 11 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 __PACKAGE__->meta->make_immutable;
26  
27 1 ▶sub load {
Subroutine load: called CC=2 SCAR=7.1
Pod documented
28 1 my $self = shift;
29 1 validate_pos( @_, { type => SCALAR, regex => qr/^\d+$/ } );
30 1 my $id = shift;
31 1 my $ua = $self->ua;
32 1 my $url = $self->base_url . '/users/' . $id . '.xml';
33 1 my $res = $ua->get($url);
34 1 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
35 1 $self->load_from_xml( $res->content );
36 }
37 else {
38 0 die "try to get $url failed: "
39 . $res->status_line . "\n"
40 . $res->content;
41 }
42 }
43  
44 1 ▶sub load_from_xml {
Subroutine load_from_xml: called CC=2 SCAR=6.9
Pod documented
45 1 my $self = shift;
46 1 my $ref = Net::Lighthouse::Util->translate_from_xml(shift);
47  
48 # dirty hack: some attrs are read-only, and Mouse doesn't support
49 # writer => '...'
50 1 for my $k ( keys %$ref ) {
51 5 $self->{$k} = $ref->{$k};
52 }
53 1 return $self;
54 }
55  
56 0 ▶sub update {
Subroutine update: not called CC=3 SCAR=24.8
Pod documented
57 0 my $self = shift;
58 0 validate(
59 @_,
60 {
61 name => { optional => 1, type => SCALAR },
62 job => { optional => 1, type => SCALAR },
63 website => { optional => 1, type => SCALAR },
64 }
65 );
66 0 my %args = ( ( map { $_ => $self->$_ } qw/name job website/ ), @_ );
67  
68 0 my $xml =
69 Net::Lighthouse::Util->translate_to_xml( \%args, root => 'user', );
70  
71 0 my $ua = $self->ua;
72 0 my $url = $self->base_url . '/users/' . $self->id . '.xml';
73  
74 0 my $request = HTTP::Request->new( 'PUT', $url, undef, $xml );
75 0 my $res = $ua->request($request);
76  
77 # the current server returns 302 moved temporarily even updated with
78 # success
79 0 ▶ if ( $res->is_success || $res->code == 302 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success or $res->code == 302) { } 0 0 0
Condition0%0/3
$res->is_success or $res->code == 302
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->is_success or $res->code == 302
$res->is_success $res->code == 302
Truth table0%0/3
A or B
A= $res->is_successB= $res->code == 302
ABresult
1X1
011
000
80 0 $self->load( $self->id ); # let's reload
81 0 return 1;
82 }
83 else {
84 0 die "try to PUT $url failed: "
85 . $res->status_line . "\n"
86 . $res->content;
87 }
88 }
89  
90 1 ▶sub memberships {
Subroutine memberships: called CC=4 SCAR=14.3
Pod documented
91 1 my $self = shift;
92 1 my $ua = $self->ua;
93 1 my $url = $self->base_url . '/users/' . $self->id . '/memberships.xml';
94 1 my $res = $ua->get($url);
95 1 require Net::Lighthouse::User::Membership;
96 1 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
97 1 my $ms = Net::Lighthouse::Util->read_xml( $res->content )->{memberships}{membership};
98 my @list = map {
99 1 my $m = Net::Lighthouse::User::Membership->new;
100 1 ▶ $m->load_from_xml($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $ms eq 'ARRAY' ? : 0 1 1
101 } ref $ms eq 'ARRAY' ? @$ms : $ms;
102 1 ▶ return wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
103 }
104 else {
105 0 die "try to get $url failed: "
106 . $res->status_line . "\n"
107 . $res->content;
108 }
109 }
110  
111 11 1;
112  
113 __END__
« Lighthouse/Project/Ticket.pm ↑ Summary Lighthouse/Project/TicketBin.pm »