Gett/Share.pm

filter: Statementstmt 36.3% 20 / 55 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 33.3% 4 / 12 Podpod 100% 5 / 5 totaltotal 29.5% 29 / 98 CC 14 SCAR 24.6
CC
14
Cov
25%
add_file
20.0
update
20.0
destroy
20.0
CRAP 11.8
« Gett/User.pm ↑ Summary Gett/Request.pm »
1 package Net::API::Gett::Share;
2  
3 =head1 NAME
4  
5 Net::API::Gett::Share - Gett share object
6  
7 =cut
8  
9 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Array::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use MooX::Types::MooseLike::Base qw(Int Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 our $VERSION = '1.06';
15  
16 =head1 PURPOSE
17  
18 Encapsulate Gett shares. You normally shouldn't instantiate this class on its own, as the
19 library will create and return this object as appropriate.
20  
21 =head1 ATTRIBUTES
22  
23 =over
24  
25 =item user
26  
27 L<Net::API::Gett::User> object. C<has_user()> predicate.
28  
29 =back
30  
31 =cut
32  
33 has 'user' => (
34 is => 'rw',
35 predicate => 'has_user',
36 0 ▶ isa => sub { die "$_[0] is not Net::API::Gett::User" unless ref($_[0]) =~ /User/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_[0]) =~ /User/ 0 0 0
37 5 lazy => 1,
38 );
39  
40 =over
41  
42 =item request
43  
44 L<Net::API::Gett::Request> object.
45  
46 =back
47  
48 =cut
49  
50 has 'request' => (
51 is => 'rw',
52 0 ▶ isa => sub { die "$_[0] is not Net::API::Gett::Request" unless ref($_[0]) =~ /Request/ },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_[0]) =~ /Request/ 0 0 0
53 0 ▶ default => sub { Net::API::Gett::Request->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 5 lazy => 1,
55 );
56  
57 =over
58  
59 =item sharename
60  
61 Scalar string. Read only.
62  
63 =item title
64  
65 Scalar string.
66  
67 =item created
68  
69 Scalar integer. Read only. This value is in Unix epoch seconds, suitable for use in a call to C<localtime()>.
70  
71 =item getturl
72  
73 Scalar string. The URL to use in a browser to access a share.
74  
75 =item files
76  
77 This attribute holds any L<Net::API::Gett:File> objects linked to a particular
78 share instance. It returns a list of L<Net::API::Gett:File> objects if
79 there are any, otherwise returns an empty list. See also
80 C<get_file_iterator()> below.
81  
82 =back
83  
84 =cut
85  
86 5 has 'sharename' => (
87 is => 'ro',
88 isa => Str,
89 );
90  
91 5 has 'title' => (
92 is => 'rw',
93 );
94  
95 5 has 'created' => (
96 is => 'ro',
97 isa => Int,
98 );
99  
100 5 has 'getturl' => (
101 is => 'ro',
102 isa => Str,
103 );
104  
105 0 ▶sub files {
Subroutine files: not called CC=2 SCAR=17.9
Pod documented
106 0 my $self = shift;
107  
108 0 ▶ return () unless exists $self->{'files'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"files"} 0 0 0
109  
110 0 return @{ $self->{'files'} };
111 }
112  
113 =head1 METHODS
114  
115 =over
116  
117 =item add_file()
118  
119 This method stores a new L<Net::API::Gett::File> object in the share object.
120 It returns undef if the value passed is not an L<Net::API::Gett::File> object.
121  
122 =back
123  
124 =cut
125  
126 0 ▶sub add_file {
Subroutine add_file: not called CC=4 SCAR=30.0
Pod documented
127 0 my $self = shift;
128 0 my $file = shift;
129  
130 0 ▶ return undef unless ref($file) =~ /File/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($file) =~ /File/ 0 0 0
131  
132 0 ▶ $file->sharename($self->sharename) unless $file->sharename;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->sharename 0 0 0
133  
134 0 ▶ $file->user($self->user) if $self->has_user;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_user 0 0 0
135  
136 0 push @{ $self->{'files'} }, $file;
137 }
138  
139 =over
140  
141 =item file_iterator()
142  
143 This method returns a L<Array::Iterator> object on the files in this share. For full details
144 about L<Array::Iterator> please read its documentation. It supports standard iterator
145 methods such as:
146  
147 =over
148  
149 =item * next()
150  
151 =item * has_next()
152  
153 =item * get_next()
154  
155 =item * peek()
156  
157 =back
158  
159 Example code using peek:
160  
161 my $share = $gett->get_share("4ddsfds");
162 my $file_iter = $share->file_iterator();
163  
164 while ( $file_iter->has_next ) {
165 if ( $file_iter->peek->size > 1_048_576 ) {
166 # Skip big file
167 warn $file_iter->peek->filename . " is too large, skipping\n";
168 $file_iter->next();
169 }
170 else {
171 my $file = $file_iter->next();
172 printf "name: %s\tsize: %d\n", $file->filename, $file->size;
173 }
174 }
175  
176 Example code using get_next:
177  
178 for my $file ( $file_iter->get_next() ) {
179 say "name: " . $file->filename . "\tsize: " . $file->size;
180 }
181  
182 =back
183  
184 This method returns undef if there are no files associated with a share.
185  
186 =cut
187  
188 0 ▶sub file_iterator {
Subroutine file_iterator: not called CC=2 SCAR=17.9
Pod documented
189 0 my $self = shift;
190  
191 0 ▶ return undef unless exists $self->{'files'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"files"} 0 0 0
192  
193 0 return Array::Iterator->new($self->{'files'});
194 }
195  
196 =over
197  
198 =item update()
199  
200 This method updates share attributes. At present, only the share title can be changed (or deleted),
201 so pass in a string to set a new title for a specific share.
202  
203 Calling this method with an empty parameter list or explicitly passing C<undef>
204 will B<delete> any title currently set on the share.
205  
206 Returns a L<Net::API::Gett:Share> object with updated values.
207  
208 =back
209  
210 =cut
211
212 0 ▶sub update {
Subroutine update: not called CC=4 SCAR=30.0
Pod documented
213 0 my $self = shift;
214  
215 0 ▶ croak "Cannot call update without a Net::API::Gett::User object" unless $self->has_user;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_user 0 0 0
216  
217 0 my $title = shift;
218 0 my $name = $self->sharename;
219  
220 0 ▶ $self->user->login unless $self->user->has_access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user->has_access_token 0 0 0
221  
222 0 my $response = $self->request->post("/shares/$name/update?accesstoken=".$self->user->access_token,
223 { title => $title } );
224  
225 0 ▶ if ( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) { } 0 0 0
226 0 $self->title( $response->{'title'} );
227 0 return $self;
228 }
229 else {
230 0 return undef;
231 }
232 }
233  
234 =over
235  
236 =item destroy()
237  
238 This method destroys the share and all of the share's files. Returns a true boolean
239 on success.
240  
241 =back
242  
243 =cut
244  
245 0 ▶sub destroy {
Subroutine destroy: not called CC=4 SCAR=30.0
Pod documented
246 0 my $self = shift;
247  
248 0 ▶ croak "Cannot call destroy without a Net::API::Gett::User object" unless $self->has_user;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_user 0 0 0
249  
250 0 my $name = $self->sharename;
251  
252 0 ▶ $self->user->login unless $self->user->has_access_token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->user->has_access_token 0 0 0
253  
254 0 my $response = $self->request->post("/shares/$name/destroy?accesstoken=".$self->user->access_token);
255  
256 0 ▶ if ( $response ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) { } 0 0 0
257 0 return 1;
258 }
259 else {
260 0 return undef;
261 }
262 }
263  
264 =head1 SEE ALSO
265  
266 L<Net::API::Gett>, L<Array::Iterator>
267  
268 =cut
269  
270 5 1;
« Gett/User.pm ↑ Summary Gett/Request.pm »