File Coverage

blib/lib/API/Instagram/User.pm
Criterion Covered Total %
statement 68 68 100.0
branch 21 22 95.4
condition 3 3 100.0
subroutine 23 23 100.0
pod 10 11 90.9
total 125 127 98.4


line stmt bran cond sub pod time code
1             package API::Instagram::User;
2              
3             # ABSTRACT: Instagram User Object
4              
5 15     15   7143 use Moo;
  15         41  
  15         146  
6 15     15   6015 use Carp;
  15         27  
  15         15673  
7              
8             has id => ( is => 'ro', required => 1 );
9             has username => ( is => 'lazy' );
10             has full_name => ( is => 'lazy' );
11             has bio => ( is => 'lazy' );
12             has website => ( is => 'lazy' );
13             has profile_picture => ( is => 'lazy' );
14             has _api => ( is => 'lazy' );
15             has _data => ( is => 'rwp', lazy => 1, builder => 1, clearer => 1 );
16              
17             sub media {
18 2     2 1 530 my $self = shift;
19 2 100       14 $self->_clear_data if shift;
20 2         563 return $_->{media} for $self->_data->{counts}
21             }
22              
23             sub follows {
24 2     2 1 4 my $self = shift;
25 2 100       38 $self->_clear_data if shift;
26 2         55 return $_->{follows} for $self->_data->{counts}
27             }
28              
29             sub followed_by {
30 2     2 1 4 my $self = shift;
31 2 100       33 $self->_clear_data if shift;
32 2         56 return $_->{followed_by} for $self->_data->{counts}
33             }
34              
35              
36             sub feed {
37 3     3 1 5226 my $self = shift;
38 3 100       19 my @list = $self->_self_requests( 'feed', '/users/self/feed', @_ ) or return;
39 1         155 [ map { $self->_api->media($_) } @list ];
  1         30  
40             }
41              
42              
43             sub liked_media {
44 3     3 1 53 my $self = shift;
45 3 100       13 my @list = $self->_self_requests( 'liked-media', '/users/self/media/liked', @_ ) or return;
46 1         153 [ map { $self->_api->media($_) } @list ];
  1         27  
47             }
48              
49              
50             sub requested_by {
51 3     3 1 8 my $self = shift;
52 3 100       14 my @list = $self->_self_requests( 'requested-by', '/users/self/requested-by', @_ ) or return;
53 1         83 [ map { $self->_api->user($_) } @list ];
  1         26  
54             }
55              
56              
57             sub get_follows {
58 1     1 1 8 shift->_get_relashions( @_, relationship => 'follows' );
59             }
60              
61              
62             sub get_followers {
63 1     1 1 7 shift->_get_relashions( @_, relationship => 'followed-by' );
64             }
65              
66              
67             sub recent_medias {
68 1     1 1 2 my $self = shift;
69 1         7 my $url = sprintf "users/%s/media/recent", $self->id;
70 1 50       28 $self->_api->_medias( $url, { @_%2?():@_ }, { token_not_required => 1 } );
71             }
72              
73             sub relationship {
74 5     5 1 1266 my $self = shift;
75 5         8 my $action = shift;
76 5         28 my $url = sprintf "users/%s/relationship", $self->id;
77 5         14 my @actions = qw/ follow unfollow block unblock approve ignore/;
78              
79 15     15   15931 use experimental 'smartmatch';
  15         16153  
  15         99  
80 5 100       14 if ( $action ) {
81 3 100       28 if ( $action ~~ @actions ){
82 2         56 return $self->_api->_post( $url, { action => $action } )
83             }
84 1         179 carp "Invalid action";
85             }
86              
87 3         186 $self->_api->_get( $url );
88             }
89              
90              
91             sub _get_relashions {
92 2     2   4 my $self = shift;
93 2         7 my %opts = @_;
94 2         12 my $url = sprintf "users/%s/%s", $self->id, $opts{relationship};
95 2         56 my $api = $self->_api;
96 2         25 [ map { $api->user($_) } $api->_get_list( { %opts, url => $url } ) ]
  2         138  
97             }
98              
99             sub _self_requests {
100 9     9   24 my ($self, $type, $url, %opts) = @_;
101              
102 9 100       231 if ( $self->id ne $self->_api->user->id ){
103 6         952 carp "The $type is only available for the authenticated user";
104 6         767 return;
105             }
106              
107 3         85 $self->_api->_get_list( { %opts, url => $url } )
108             }
109              
110              
111             sub BUILDARGS {
112 29     29 0 15154 my $self = shift;
113 29         57 my $opts = shift;
114              
115 29 100 100     137 $opts->{profile_picture} //= delete $opts->{profile_pic_url} if exists $opts->{profile_pic_url};
116              
117 29         647 return $opts;
118             }
119              
120              
121 6     6   2168 sub _build__api { API::Instagram->instance }
122 2     2   2478 sub _build_username { shift->_data->{username} }
123 1     1   391 sub _build_full_name { shift->_data->{full_name} }
124 1     1   849 sub _build_bio { shift->_data->{bio} }
125 1     1   897 sub _build_website { shift->_data->{website} }
126 1     1   911 sub _build_profile_picture { shift->_data->{profile_picture} }
127              
128             sub _build__data {
129 5     5   879 my $self = shift;
130 5         35 my $url = sprintf "users/%s", $self->id;
131 5         130 $self->_api->_get( $url );
132             }
133              
134              
135              
136             1;
137              
138             __END__