Lighthouse/Project/Message.pm

filter: Statementstmt 54.8% 62 / 113 Branchbran 41.6% 10 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 69.2% 9 / 13 Podpod 100% 8 / 8 totaltotal 56.3% 89 / 158 CC 15 SCAR 13.8
CC
15
Cov
53%
create
6.0
create_comment
6.0
update
6.0
CRAP 4.0
« Lighthouse/Project/Milestone.pm ↑ Summary Lighthouse/Project/Changeset.pm »
1 package Net::Lighthouse::Project::Message;
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/] => (
8 isa => 'Maybe[DateTime]',
9 is => 'ro',
10 );
11  
12 11 has [
13 qw/id user_id parent_id comments_count project_id
14 all_attachments_count attachments_count/
15 ] => (
16 isa => 'Maybe[Int]',
17 is => 'ro',
18 );
19  
20 11 has [ 'body_html', 'user_name', 'permalink', 'url', ] => (
21 isa => 'Maybe[Str]',
22 is => 'ro',
23 );
24  
25 11 has 'comments' => (
26 isa => 'ArrayRef[Net::Lighthouse::Project::Message]',
27 is => 'ro',
28 auto_deref => 1,
29 );
30  
31 # read&write attr
32 11 has [qw/title body/] => (
33 isa => 'Maybe[Str]',
34 is => 'rw',
35 );
36  
37 11 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 11 __PACKAGE__->meta->make_immutable;
39  
40 1 ▶sub load {
Subroutine load: called CC=2 SCAR=7.1
Pod documented
41 1 my $self = shift;
42 1 validate_pos( @_, { type => SCALAR, regex => qr/^\d+$/ } );
43 1 my $id = shift;
44 1 my $ua = $self->ua;
45 1 my $url =
46 $self->base_url
47 . '/projects/'
48 . $self->project_id . '/messages/'
49 . $id . '.xml';
50 1 my $res = $ua->get( $url );
51 1 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
52 1 $self->load_from_xml( $res->content );
53 }
54 else {
55 0 die "try to get $url failed: "
56 . $res->status_line . "\n"
57 . $res->content;
58 }
59 }
60  
61 16 ▶sub load_from_xml {
Subroutine load_from_xml: called CC=2 SCAR=6.9
Pod documented
62 16 my $self = shift;
63 16 my $ref = $self->_translate_from_xml( shift );
64  
65 # dirty hack: some attrs are read-only, and Mouse doesn't support
66 # writer => '...'
67 16 for my $k ( keys %$ref ) {
68 256 $self->{$k} = $ref->{$k};
69 }
70 16 return $self;
71 }
72  
73 17 ▶sub _translate_from_xml {
Subroutine _translate_from_xml: called CC=5 SCAR=16.8
74 17 my $self = shift;
75 17 my $ref = Net::Lighthouse::Util->translate_from_xml( shift );
76 17 for my $k ( keys %$ref ) {
77 267 ▶ if ( $k eq 'comments' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "comments") 4 263 267
78 # if has parent_id, then it's comment, comment can't have comments
79 4 ▶ if ( $ref->{parent_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"parent_id"}) 0 4 4
80 0 delete $ref->{comments};
81 0 next;
82 }
83  
84 4 ▶ if ( $ref->{comments} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{'comments'}) { } 4 0 4
85 4 my $comments = $ref->{comments}{comment};
86 $ref->{comments} = [
87 map {
88 4 my $v = Net::Lighthouse::Project::Message->new;
89 12 $v->load_from_xml($_)
90 } @$comments
91 ];
92 }
93 else {
94 0 $ref->{comments} = [];
95 }
96 }
97 }
98 17 return $ref;
99 }
100  
101 0 ▶sub create {
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
102 0 my $self = shift;
103 0 validate(
104 @_,
105 {
106 title => { type => SCALAR },
107 body => { type => SCALAR },
108 }
109 );
110 0 my %args = @_;
111  
112 0 my $xml =
113 Net::Lighthouse::Util->translate_to_xml( \%args, root => 'message', );
114 0 my $ua = $self->ua;
115  
116 0 my $url = $self->base_url . '/projects/' . $self->project_id . '/messages.xml';
117  
118 0 my $request = HTTP::Request->new( 'POST', $url, undef, $xml );
119 0 my $res = $ua->request( $request );
120 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
121 0 $self->load_from_xml( $res->content );
122 0 return 1;
123 }
124 else {
125 0 die "try to POST $url failed: "
126 . $res->status_line . "\n"
127 . $res->content;
128 }
129 }
130  
131 0 ▶sub create_comment {
Subroutine create_comment: not called CC=2 SCAR=17.9
Pod documented
132 0 my $self = shift;
133 0 validate(
134 @_,
135 {
136 body => { type => SCALAR },
137 }
138 );
139 0 my %args = @_;
140  
141 # TODO doc says <message>, but it's wrong, should be <comment>
142 # see also http://help.lighthouseapp.com/discussions/api-developers/121-create-message-comment-bug
143  
144 0 my $xml =
145 Net::Lighthouse::Util->translate_to_xml( \%args, root => 'comment', );
146  
147 0 my $ua = $self->ua;
148  
149 0 my $url =
150 $self->base_url
151 . '/projects/'
152 . $self->project_id
153 . '/messages/'
154 . $self->id
155 . '/comments.xml';
156  
157 0 my $request = HTTP::Request->new( 'POST', $url, undef, $xml );
158 0 my $res = $ua->request( $request );
159  
160 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
161 0 $self->load_from_xml( $res->content );
162 0 return 1;
163 }
164 else {
165 0 die "try to POST $url failed: "
166 . $res->status_line . "\n"
167 . $res->content;
168 }
169 }
170  
171 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
172 0 my $self = shift;
173 0 validate(
174 @_,
175 {
176 title => { optional => 1, type => SCALAR },
177 body => { optional => 1, type => SCALAR },
178 }
179 );
180 0 my %args = ( ( map { $_ => $self->$_ } qw/title body/ ), @_ );
181  
182 0 my $xml =
183 Net::Lighthouse::Util->translate_to_xml( \%args, root => 'message', );
184  
185 0 my $ua = $self->ua;
186 0 my $url =
187 $self->base_url
188 . '/projects/'
189 . $self->project_id . '/messages/'
190 . $self->id . '.xml';
191  
192 0 my $request = HTTP::Request->new( 'PUT', $url, undef, $xml );
193 0 my $res = $ua->request( $request );
194 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
195 0 $self->load( $self->id ); # let's reload
196 0 return 1;
197 }
198 else {
199 0 die "try to PUT $url failed: "
200 . $res->status_line . "\n"
201 . $res->content;
202 }
203 }
204  
205 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
206 0 my $self = shift;
207 0 my $ua = $self->ua;
208 0 my $url =
209 $self->base_url
210 . '/projects/'
211 . $self->project_id . '/messages/'
212 . $self->id . '.xml';
213  
214 0 my $request = HTTP::Request->new( 'DELETE', $url );
215 0 my $res = $ua->request( $request );
216 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
217 0 return 1;
218 }
219 else {
220 0 die "try to DELETE $url failed: "
221 . $res->status_line . "\n"
222 . $res->content;
223 }
224 }
225  
226 3 ▶sub list {
Subroutine list: called CC=4 SCAR=14.0
Pod documented
227 3 my $self = shift;
228 3 my $url =
229 $self->base_url . '/projects/' . $self->project_id . '/messages.xml';
230  
231 3 my $ua = $self->ua;
232 3 my $res = $ua->get($url);
233 3 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 3 0 3
234 3 my $ms = Net::Lighthouse::Util->read_xml( $res->content )->{messages}{message};
235 my @list = map {
236 3 my $t = Net::Lighthouse::Project::Message->new(
237 9 map { $_ => $self->$_ }
238 3 grep { $self->$_ } qw/account auth project_id/
239 );
240 3 ▶ $t->load_from_xml($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $ms eq 'ARRAY' ? : 0 3 3
241 } ref $ms eq 'ARRAY' ? @$ms : $ms;
242 3 ▶ return wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
243 }
244 else {
245 0 die "try to get $url failed: "
246 . $res->status_line . "\n"
247 . $res->content;
248 }
249  
250 }
251  
252 1 ▶sub initial_state {
Subroutine initial_state: called CC=2 SCAR=7.1
Pod documented
253 1 my $self = shift;
254 1 my $ua = $self->ua;
255 1 my $url =
256 $self->base_url . '/projects/' . $self->project_id . '/messages/new.xml';
257 1 my $res = $ua->get( $url );
258 1 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
259 1 return $self->_translate_from_xml( $res->content );
260 }
261 else {
262 0 die "try to get $url failed: "
263 . $res->status_line . "\n"
264 . $res->content;
265 }
266 }
267  
268 11 1;
269  
270 __END__
« Lighthouse/Project/Milestone.pm ↑ Summary Lighthouse/Project/Changeset.pm »