Lighthouse/Project/Ticket.pm

filter: Statementstmt 66.3% 81 / 122 Branchbran 53.1% 17 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod 100% 7 / 7 totaltotal 65.9% 114 / 173 CC 20 SCAR 17.3
CC
20
Cov
64%
_translate_from_xml
9.2
list
6.7
create
6.0
CRAP 5.6
« Lighthouse/Util.pm ↑ Summary Lighthouse/User.pm »
1 package Net::Lighthouse::Project::Ticket;
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 # read only attr
7 11 has [qw/created_at updated_at milestone_due_on/] => (
8 isa => 'Maybe[DateTime]',
9 is => 'ro',
10 );
11  
12 11 has [qw/number priority user_id project_id creator_id attachments_count/] => (
13 isa => 'Maybe[Int]',
14 is => 'ro',
15 );
16  
17 11 has [qw/closed /] => (
18 isa => 'Bool',
19 is => 'ro',
20 );
21  
22 11 has [
23 'raw_data', 'user_name',
24 'permalink', 'url',
25 'latest_body', 'creator_name',
26 'assigned_user_name', 'milestone_title',
27 ] => (
28 isa => 'Maybe[Str]',
29 is => 'ro',
30 );
31  
32 11 has 'attachments' => (
33 isa => 'ArrayRef[Net::Lighthouse::Project::Ticket::Attachment]',
34 is => 'ro',
35 auto_deref => 1,
36 );
37  
38 11 has 'versions' => (
39 isa => 'ArrayRef[Net::Lighthouse::Project::Ticket::Version]',
40 is => 'ro',
41 auto_deref => 1,
42 );
43  
44 # read&write attr
45 11 has [qw/assigned_user_id milestone_id/] => (
46 isa => 'Maybe[Int]',
47 is => 'rw',
48 );
49  
50 11 has [qw/title state tag/] => (
51 isa => 'Maybe[Str]',
52 is => 'rw',
53 );
54  
55 11 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 11 __PACKAGE__->meta->make_immutable;
57  
58 1 ▶sub load {
Subroutine load: called CC=2 SCAR=7.1
Pod documented
59 1 my $self = shift;
60 1 validate_pos( @_, { type => SCALAR, regex => qr/^\d+$/ } );
61 1 my $number = shift;
62 1 my $ua = $self->ua;
63 1 my $url =
64 $self->base_url
65 . '/projects/'
66 . $self->project_id . '/tickets/'
67 . $number . '.xml';
68 1 my $res = $ua->get( $url );
69 1 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
70 1 $self->load_from_xml( $res->content );
71 }
72 else {
73 0 die "try to get $url failed: "
74 . $res->status_line . "\n"
75 . $res->content;
76 }
77 }
78  
79 7 ▶sub load_from_xml {
Subroutine load_from_xml: called CC=2 SCAR=6.9
Pod documented
80 7 my $self = shift;
81 7 my $ref = $self->_translate_from_xml( shift );
82  
83 # dirty hack: some attrs are read-only, and Mouse doesn't support
84 # writer => '...'
85 7 for my $k ( keys %$ref ) {
86 153 $self->{$k} = $ref->{$k};
87 }
88 7 return $self;
89 }
90  
91 8 ▶sub _translate_from_xml {
Subroutine _translate_from_xml: called CC=9 SCAR=22.2
92 8 my $self = shift;
93 8 my $ref = Net::Lighthouse::Util->translate_from_xml( shift );
94 8 for my $k ( keys %$ref ) {
95 172 ▶ if ( $k eq 'versions' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'versions') { } 1 171 172
elsif ($k eq 'attachments') { } 1 170 171
96 1 my $versions = $ref->{versions}{version};
97 1 ▶ $versions = [ $versions ] unless ref $versions eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $versions eq "ARRAY" 1 0 1
98 1 require Net::Lighthouse::Project::Ticket::Version;
99 $ref->{versions} = [
100 map {
101 1 my $v = Net::Lighthouse::Project::Ticket::Version->new;
102 1 $v->load_from_xml($_)
103 } @$versions
104 ];
105 }
106 elsif ( $k eq 'attachments' ) {
107 1 my @attachments;
108 1 for ( keys %{$ref->{attachments}} ) {
109 3 my $att = $ref->{attachments}{$_};
110 3 ▶ next unless ref $att;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $att 1 2 3
111 2 ▶ if ( ref $att eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $att eq 'ARRAY') { } 0 2 2
112 0 push @attachments, @{$att};
113 }
114 else {
115 2 push @attachments, $att;
116 }
117 }
118 1 ▶ next unless @attachments;
Branch: 1 branch
BranchTrueFalseTotal
unless @attachments 0 1 1
119  
120 1 require Net::Lighthouse::Project::Ticket::Attachment;
121 $ref->{attachments} = [
122 map {
123 1 my $v =
124 Net::Lighthouse::Project::Ticket::Attachment->new(
125 ua => $self->ua );
126 2 $v->load_from_xml($_)
127 } @attachments
128 ];
129 }
130 }
131 8 return $ref;
132 }
133  
134 0 ▶sub create {
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
135 0 my $self = shift;
136 0 validate(
137 @_,
138 {
139 title => { type => SCALAR },
140 body => { type => SCALAR },
141 state => { optional => 1, type => SCALAR },
142 assigned_user_id => {
143 optional => 1,
144 type => SCALAR | UNDEF,
145 regex => qr/^(\d+|)$/,
146 },
147 milestone_id => {
148 optional => 1,
149 type => SCALAR | UNDEF,
150 regex => qr/^(\d+|)$/,
151 },
152 tag => { optional => 1, type => SCALAR },
153 }
154 );
155 0 my %args = @_;
156  
157 0 my $xml =
158 Net::Lighthouse::Util->translate_to_xml( \%args, root => 'ticket', );
159  
160 0 my $ua = $self->ua;
161  
162 0 my $url = $self->base_url . '/projects/' . $self->project_id . '/tickets.xml';
163  
164 0 my $request = HTTP::Request->new( 'POST', $url, undef, $xml );
165 0 my $res = $ua->request( $request );
166 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
167 0 $self->load_from_xml( $res->content );
168 0 return 1;
169 }
170 else {
171 0 die "try to POST $url failed: "
172 . $res->status_line . "\n"
173 . $res->content;
174 }
175 }
176  
177 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
178 0 my $self = shift;
179 0 validate(
180 @_,
181 {
182 title => { optional => 1, type => SCALAR },
183 body => { optional => 1, type => SCALAR },
184 state => { optional => 1, type => SCALAR },
185 assigned_user_id => {
186 optional => 1,
187 type => SCALAR | UNDEF,
188 regex => qr/^(\d+|)$/,
189 },
190 milestone_id => {
191 optional => 1,
192 type => SCALAR | UNDEF,
193 regex => qr/^(\d+|)$/,
194 },
195 tag => { optional => 1, type => SCALAR },
196 }
197 );
198 my %args = (
199 (
200 0 map { $_ => $self->$_ }
201 qw/title body state assigned_user_id milestone_id tag/
202 ),
203 @_
204 );
205  
206 0 my $xml =
207 Net::Lighthouse::Util->translate_to_xml( \%args, root => 'ticket', );
208  
209 0 my $ua = $self->ua;
210 0 my $url =
211 $self->base_url
212 . '/projects/'
213 . $self->project_id . '/tickets/'
214 . $self->number . '.xml';
215  
216 0 my $request = HTTP::Request->new( 'PUT', $url, undef, $xml );
217 0 my $res = $ua->request( $request );
218 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
219 0 $self->load( $self->number ); # let's reload
220 0 return 1;
221 }
222 else {
223 0 die "try to PUT $url failed: "
224 . $res->status_line . "\n"
225 . $res->content;
226 }
227 }
228  
229 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
230 0 my $self = shift;
231 0 my $ua = $self->ua;
232 0 my $url =
233 $self->base_url
234 . '/projects/'
235 . $self->project_id . '/tickets/'
236 . $self->number . '.xml';
237  
238 0 my $request = HTTP::Request->new( 'DELETE', $url );
239 0 my $res = $ua->request( $request );
240 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
241 0 return 1;
242 }
243 else {
244 0 die "try to DELETE $url failed: "
245 . $res->status_line . "\n"
246 . $res->content;
247 }
248 }
249  
250 3 ▶sub list {
Subroutine list: called CC=6 SCAR=19.0
Pod documented
251 3 my $self = shift;
252 3 validate(
253 @_,
254 {
255 query => { optional => 1, type => SCALAR },
256 page => { optional => 1, type => SCALAR, regex => qr/^\d+$/ },
257 }
258 );
259 3 my %args = @_;
260  
261 3 my $url =
262 $self->base_url . '/projects/' . $self->project_id . '/tickets.xml?';
263 3 ▶ if ( $args{query} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"query"}) 0 3 3
264 0 require URI::Escape;
265 0 $url .= 'q=' . URI::Escape::uri_escape( $args{query} ) . '&';
266 }
267 3 ▶ if ( $args{page} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"page"}) 0 3 3
268 0 $url .= 'page=' . uri_escape( $args{page} );
269 }
270  
271 3 my $ua = $self->ua;
272 3 my $res = $ua->get($url);
273 3 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 3 0 3
274 3 my $ts = Net::Lighthouse::Util->read_xml( $res->content )->{tickets}{ticket};
275 my @list = map {
276 3 my $t = Net::Lighthouse::Project::Ticket->new(
277 18 map { $_ => $self->$_ }
278 6 grep { $self->$_ } qw/account auth project_id/
279 );
280 6 ▶ $t->load_from_xml($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $ts eq 'ARRAY' ? : 3 0 3
281 } ref $ts eq 'ARRAY' ? @$ts : $ts;
282 3 ▶ return wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
283 }
284 else {
285 0 die "try to get $url failed: "
286 . $res->status_line . "\n"
287 . $res->content;
288 }
289  
290 }
291  
292 1 ▶sub initial_state {
Subroutine initial_state: called CC=2 SCAR=7.1
Pod documented
293 1 my $self = shift;
294 1 my $ua = $self->ua;
295 1 my $url =
296 $self->base_url . '/projects/' . $self->project_id . '/tickets/new.xml';
297 1 my $res = $ua->get( $url );
298 1 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
299 1 return $self->_translate_from_xml( $res->content );
300 }
301 else {
302 0 die "try to get $url failed: "
303 . $res->status_line . "\n"
304 . $res->content;
305 }
306 }
307  
308 11 1;
309  
310 __END__
« Lighthouse/Util.pm ↑ Summary Lighthouse/User.pm »