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