| 1 | package Net::Lighthouse::Project::Milestone; | ||||||||||
| 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 [qw/open_tickets_count id project_id tickets_count/] => ( | |||||||||
| 13 | isa => 'Int', | ||||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | ); | ||||||||||
| 16 | |||||||||||
| 17 | 11 | has [ 'goals_html', 'user_name', 'permalink', 'url', ] => ( | |||||||||
| 18 | isa => 'Maybe[Str]', | ||||||||||
| 19 | is => 'ro', | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | # read&write attr | ||||||||||
| 23 | 11 | has [qw/title goals due_on/] => ( | |||||||||
| 24 | isa => 'Maybe[Str]', | ||||||||||
| 25 | is => 'rw', | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | 11 | ▶ | no Any::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 11 | __PACKAGE__->meta->make_immutable; | |||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | sub load { | ||||||||
|
Subroutine
load: called CC=4 SCAR=17.5
Pod
documented
| |||||||||||
| 32 | 1 | my $self = shift; | |||||||||
| 33 | 1 | validate_pos( @_, { type => SCALAR, regex => qr/^\d+|\w+$/ } ); | |||||||||
| 34 | 1 | my $id = shift; | |||||||||
| 35 | |||||||||||
| 36 | 1 | ▶ | if ( $id !~ /^\d+$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | # so we got a title, let's find it | ||||||||||
| 39 | 0 | my ($milestone) = grep { $_->title eq $id } $self->list; | |||||||||
| 40 | 0 | ▶ | if ($milestone) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | $id = $milestone->id; | |||||||||
| 42 | } | ||||||||||
| 43 | else { | ||||||||||
| 44 | 0 | die "can't find milestone $id in project " | |||||||||
| 45 | . $self->project_id | ||||||||||
| 46 | . ' in account ' | ||||||||||
| 47 | . $self->account; | ||||||||||
| 48 | } | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 1 | my $ua = $self->ua; | |||||||||
| 52 | 1 | my $url = | |||||||||
| 53 | $self->base_url | ||||||||||
| 54 | . '/projects/' | ||||||||||
| 55 | . $self->project_id . '/milestones/' | ||||||||||
| 56 | . $id . '.xml'; | ||||||||||
| 57 | 1 | my $res = $ua->get( $url ); | |||||||||
| 58 | 1 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 1 | $self->load_from_xml( $res->content ); | |||||||||
| 60 | } | ||||||||||
| 61 | else { | ||||||||||
| 62 | 0 | die "try to get $url failed: " | |||||||||
| 63 | . $res->status_line . "\n" | ||||||||||
| 64 | . $res->content; | ||||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 4 | ▶ | sub load_from_xml { | ||||||||
|
Subroutine
load_from_xml: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 69 | 4 | my $self = shift; | |||||||||
| 70 | 4 | my $ref = Net::Lighthouse::Util->translate_from_xml( shift ); | |||||||||
| 71 | |||||||||||
| 72 | # dirty hack: some attrs are read-only, and Mouse doesn't support | ||||||||||
| 73 | # writer => '...' | ||||||||||
| 74 | 4 | for my $k ( keys %$ref ) { | |||||||||
| 75 | 52 | $self->{$k} = $ref->{$k}; | |||||||||
| 76 | } | ||||||||||
| 77 | 4 | return $self; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | sub create { | ||||||||
|
Subroutine
create: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 81 | 0 | my $self = shift; | |||||||||
| 82 | 0 | validate( | |||||||||
| 83 | @_, | ||||||||||
| 84 | { | ||||||||||
| 85 | goals => { type => SCALAR }, | ||||||||||
| 86 | title => { type => SCALAR }, | ||||||||||
| 87 | due_on => { optional => 1, type => SCALAR }, | ||||||||||
| 88 | } | ||||||||||
| 89 | ); | ||||||||||
| 90 | 0 | my %args = @_; | |||||||||
| 91 | |||||||||||
| 92 | 0 | my $xml = | |||||||||
| 93 | Net::Lighthouse::Util->translate_to_xml( \%args, root => 'milestone', ); | ||||||||||
| 94 | 0 | my $ua = $self->ua; | |||||||||
| 95 | |||||||||||
| 96 | 0 | my $url = $self->base_url . '/projects/' . $self->project_id . '/milestones.xml'; | |||||||||
| 97 | |||||||||||
| 98 | 0 | my $request = HTTP::Request->new( 'POST', $url, undef, $xml ); | |||||||||
| 99 | 0 | my $res = $ua->request( $request ); | |||||||||
| 100 | 0 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | $self->load_from_xml( $res->content ); | |||||||||
| 102 | 0 | return 1; | |||||||||
| 103 | } | ||||||||||
| 104 | else { | ||||||||||
| 105 | 0 | die "try to POST $url failed: " | |||||||||
| 106 | . $res->status_line . "\n" | ||||||||||
| 107 | . $res->content; | ||||||||||
| 108 | } | ||||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 0 | ▶ | sub update { | ||||||||
|
Subroutine
update: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 112 | 0 | my $self = shift; | |||||||||
| 113 | 0 | validate( | |||||||||
| 114 | @_, | ||||||||||
| 115 | { | ||||||||||
| 116 | goals => { optional => 1, type => SCALAR }, | ||||||||||
| 117 | title => { optional => 1, type => SCALAR }, | ||||||||||
| 118 | due_on => { optional => 1, type => SCALAR }, | ||||||||||
| 119 | } | ||||||||||
| 120 | ); | ||||||||||
| 121 | 0 | my %args = ( ( map { $_ => $self->$_ } qw/title goals due_on/ ), @_ ); | |||||||||
| 122 | |||||||||||
| 123 | 0 | my $xml = | |||||||||
| 124 | Net::Lighthouse::Util->translate_to_xml( \%args, root => 'milestone', ); | ||||||||||
| 125 | 0 | my $ua = $self->ua; | |||||||||
| 126 | 0 | my $url = | |||||||||
| 127 | $self->base_url | ||||||||||
| 128 | . '/projects/' | ||||||||||
| 129 | . $self->project_id . '/milestones/' | ||||||||||
| 130 | . $self->id . '.xml'; | ||||||||||
| 131 | |||||||||||
| 132 | 0 | my $request = HTTP::Request->new( 'PUT', $url, undef, $xml ); | |||||||||
| 133 | 0 | my $res = $ua->request( $request ); | |||||||||
| 134 | 0 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | $self->load( $self->id ); # let's reload | |||||||||
| 136 | 0 | return 1; | |||||||||
| 137 | } | ||||||||||
| 138 | else { | ||||||||||
| 139 | 0 | die "try to PUT $url failed: " | |||||||||
| 140 | . $res->status_line . "\n" | ||||||||||
| 141 | . $res->content; | ||||||||||
| 142 | } | ||||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 146 | 0 | my $self = shift; | |||||||||
| 147 | 0 | my $ua = $self->ua; | |||||||||
| 148 | 0 | my $url = | |||||||||
| 149 | $self->base_url | ||||||||||
| 150 | . '/projects/' | ||||||||||
| 151 | . $self->project_id . '/milestones/' | ||||||||||
| 152 | . $self->id . '.xml'; | ||||||||||
| 153 | |||||||||||
| 154 | 0 | my $request = HTTP::Request->new( 'DELETE', $url ); | |||||||||
| 155 | 0 | my $res = $ua->request( $request ); | |||||||||
| 156 | 0 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | return 1; | |||||||||
| 158 | } | ||||||||||
| 159 | else { | ||||||||||
| 160 | 0 | die "try to DELETE $url failed: " | |||||||||
| 161 | . $res->status_line . "\n" | ||||||||||
| 162 | . $res->content; | ||||||||||
| 163 | } | ||||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 3 | ▶ | sub list { | ||||||||
|
Subroutine
list: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 167 | 3 | my $self = shift; | |||||||||
| 168 | 3 | my $url = | |||||||||
| 169 | $self->base_url . '/projects/' . $self->project_id . '/milestones.xml'; | ||||||||||
| 170 | 3 | my $ua = $self->ua; | |||||||||
| 171 | 3 | my $res = $ua->get($url); | |||||||||
| 172 | 3 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | my $ms = | ||||||||||
| 174 | 3 | Net::Lighthouse::Util->read_xml( $res->content )->{milestones}{milestone}; | |||||||||
| 175 | my @list = map { | ||||||||||
| 176 | 3 | my $t = Net::Lighthouse::Project::Milestone->new( | |||||||||
| 177 | 9 | map { $_ => $self->$_ } | |||||||||
| 178 | 3 | grep { $self->$_ } qw/account auth project_id/ | |||||||||
| 179 | ); | ||||||||||
| 180 | 3 | ▶ | $t->load_from_xml($_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | } ref $ms eq 'ARRAY' ? @$ms : $ms; | ||||||||||
| 182 | 3 | ▶ | return wantarray ? @list : \@list; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | } | ||||||||||
| 184 | else { | ||||||||||
| 185 | 0 | die "try to get $url failed: " | |||||||||
| 186 | . $res->status_line . "\n" | ||||||||||
| 187 | . $res->content; | ||||||||||
| 188 | } | ||||||||||
| 189 | |||||||||||
| 190 | } | ||||||||||
| 191 | |||||||||||
| 192 | 1 | ▶ | sub initial_state { | ||||||||
|
Subroutine
initial_state: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 193 | 1 | my $self = shift; | |||||||||
| 194 | 1 | my $ua = $self->ua; | |||||||||
| 195 | 1 | my $url = | |||||||||
| 196 | $self->base_url . '/projects/' . $self->project_id . '/milestones/new.xml'; | ||||||||||
| 197 | 1 | my $res = $ua->get( $url ); | |||||||||
| 198 | 1 | ▶ | if ( $res->is_success ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | 1 | return Net::Lighthouse::Util->translate_from_xml( $res->content ); | |||||||||
| 200 | } | ||||||||||
| 201 | else { | ||||||||||
| 202 | 0 | die "try to get $url failed: " | |||||||||
| 203 | . $res->status_line . "\n" | ||||||||||
| 204 | . $res->content; | ||||||||||
| 205 | } | ||||||||||
| 206 | } | ||||||||||
| 207 | |||||||||||
| 208 | 11 | 1; | |||||||||
| 209 | |||||||||||
| 210 | __END__ | ||||||||||