File Coverage

blib/lib/Net/Lighthouse/User.pm
Criterion Covered Total %
statement 36 51 70.5
branch 4 10 40.0
condition 0 3 0.0
subroutine 7 8 87.5
pod 4 4 100.0
total 51 76 67.1


line stmt bran cond sub pod time code
1             package Net::Lighthouse::User;
2 11     11   1673 use Any::Moose;
  11         31858  
  11         78  
3 11     11   7152 use Params::Validate ':all';
  11         10444  
  11         2625  
4 11     11   691 use Net::Lighthouse::Util;
  11         24  
  11         905  
5             extends 'Net::Lighthouse::Base';
6              
7             # read only attr
8             has 'id' => (
9             isa => 'Int',
10             is => 'ro',
11             );
12              
13             has 'avatar_url' => (
14             isa => 'Maybe[Str]',
15             is => 'ro',
16             );
17              
18             # read&write attr
19             has [qw/name job website/] => (
20             isa => 'Maybe[Str]',
21             is => 'rw',
22             );
23              
24 11     11   63 no Any::Moose;
  11         21  
  11         73  
25             __PACKAGE__->meta->make_immutable;
26              
27             sub load {
28 1     1 1 4545 my $self = shift;
29 1         62 validate_pos( @_, { type => SCALAR, regex => qr/^\d+$/ } );
30 1         17 my $id = shift;
31 1         9 my $ua = $self->ua;
32 1         11 my $url = $self->base_url . '/users/' . $id . '.xml';
33 1         9 my $res = $ua->get($url);
34 1 50       65 if ( $res->is_success ) {
35 1         62 $self->load_from_xml( $res->content );
36             }
37             else {
38 0         0 die "try to get $url failed: "
39             . $res->status_line . "\n"
40             . $res->content;
41             }
42             }
43              
44             sub load_from_xml {
45 1     1 1 161 my $self = shift;
46 1         9 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         5 for my $k ( keys %$ref ) {
51 5         27 $self->{$k} = $ref->{$k};
52             }
53 1         9 return $self;
54             }
55              
56             sub update {
57 0     0 1 0 my $self = shift;
58 0         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         0 my %args = ( ( map { $_ => $self->$_ } qw/name job website/ ), @_ );
  0         0  
67              
68 0         0 my $xml =
69             Net::Lighthouse::Util->translate_to_xml( \%args, root => 'user', );
70              
71 0         0 my $ua = $self->ua;
72 0         0 my $url = $self->base_url . '/users/' . $self->id . '.xml';
73              
74 0         0 my $request = HTTP::Request->new( 'PUT', $url, undef, $xml );
75 0         0 my $res = $ua->request($request);
76              
77             # the current server returns 302 moved temporarily even updated with
78             # success
79 0 0 0     0 if ( $res->is_success || $res->code == 302 ) {
80 0         0 $self->load( $self->id ); # let's reload
81 0         0 return 1;
82             }
83             else {
84 0         0 die "try to PUT $url failed: "
85             . $res->status_line . "\n"
86             . $res->content;
87             }
88             }
89              
90             sub memberships {
91 1     1 1 2140 my $self = shift;
92 1         7 my $ua = $self->ua;
93 1         7 my $url = $self->base_url . '/users/' . $self->id . '/memberships.xml';
94 1         5 my $res = $ua->get($url);
95 1         768 require Net::Lighthouse::User::Membership;
96 1 50       8 if ( $res->is_success ) {
97 1         66 my $ms = Net::Lighthouse::Util->read_xml( $res->content )->{memberships}{membership};
98 1         12 my @list = map {
99 1 50       1637 my $m = Net::Lighthouse::User::Membership->new;
100 1         71 $m->load_from_xml($_);
101             } ref $ms eq 'ARRAY' ? @$ms : $ms;
102 1 50       9 return wantarray ? @list : \@list;
103             }
104             else {
105 0           die "try to get $url failed: "
106             . $res->status_line . "\n"
107             . $res->content;
108             }
109             }
110              
111             1;
112              
113             __END__