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