API/Action.pm

filter: Statementstmt 43.4% 23 / 53 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 6 / 14 Podpod 100% 6 / 6 totaltotal 41.1% 35 / 85 CC 7 SCAR 26.5
CC
7
Cov
35%
formatResponse
42.0
__ANON__
6.0
_build__rest_cli
2.0
CRAP 14.1
↑ Summary API/Request.pm »
1 #
2 # This file is part of Redmine-API
3 #
4 # This software is copyright (c) 2014 by celogeek <me@celogeek.com>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Redmine::API::Action;
10  
11 # ABSTRACT: Action to the API
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 our $VERSION = '0.04'; # VERSION
15 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 has 'request' => (
22 is => 'ro',
23 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
24 0 ▶ croak "request should be a Redmine::API::Request object"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "Redmine::API::Request" 0 0 0
25 unless ref $_[0] eq 'Redmine::API::Request';
26 },
27 3 required => 1,
28 );
29  
30 3 has 'action' => (
31 is => 'ro',
32 required => 1,
33 );
34  
35 3 has '_rest_cli' => ( is => 'lazy' );
36  
37 0 ▶sub _build__rest_cli {
Subroutine _build__rest_cli: not called CC=1 SCAR=6.9
38 0 my ($self) = @_;
39 0 my $api = $self->request->api;
40  
41 0 my $cli = REST::Client->new();
42 0 $cli->setHost( $api->base_url );
43 0 $cli->addHeader( 'X-Redmine-API-Key' => $api->auth_key );
44 0 $cli->addHeader( 'Content-Type' => 'application/json' );
45 0 $cli->addHeader( 'Accept' => 'application/json' );
46 0 return $cli;
47  
48 }
49  
50 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
51 0 my ( $self, %data ) = @_;
52 0 return $self->formatResponse(
53 $self->_rest_cli->POST(
54 '/' . $self->request->route . '.json',
55 encode_json( { $self->action => \%data } ),
56 )
57 );
58 }
59  
60 0 ▶sub all {
Subroutine all: not called CC=1 SCAR=6.9
Pod documented
61 0 my ( $self, %options ) = @_;
62 0 return $self->formatResponse(
63 $self->_rest_cli->GET(
64 '/'
65 . $self->request->route . '.json'
66 . $self->_rest_cli->buildQuery( \%options )
67 )
68 );
69 }
70  
71 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
72 0 my ( $self, $id, %options ) = @_;
73 0 return $self->formatResponse(
74 $self->_rest_cli->GET(
75 '/'
76 . $self->request->route . '/'
77 . $id . '.json'
78 . $self->_rest_cli->buildQuery( \%options )
79 )
80 );
81 }
82  
83 0 ▶sub del {
Subroutine del: not called CC=1 SCAR=6.9
Pod documented
84 0 my ( $self, $id ) = @_;
85 0 return $self->formatResponse(
86 $self->_rest_cli->DELETE(
87 '/' . $self->request->route . '/' . $id . '.json'
88 )
89 );
90 }
91  
92 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
93 0 my ( $self, $id, %data ) = @_;
94 0 return $self->formatResponse(
95 $self->_rest_cli->PUT(
96 '/' . $self->request->route . '/' . $id . '.json',
97 encode_json( { $self->action => \%data } ),
98 )
99 );
100 }
101  
102 0 ▶sub formatResponse {
Subroutine formatResponse: not called CC=6 SCAR=37.4
Pod documented
103 0 my ( $self, $req ) = @_;
104  
105 0 ▶ return {} if $req->responseCode == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $req->responseCode == 404 0 0 0
106  
107 0 ▶ croak "ERROR ", $req->responseCode,
Branch: 1 branch
BranchTrueFalseTotal
if $req->responseCode == 401 0 0 0
108 " : ACCESS FORBIDDEN, CHECK YOUR TOKEN !"
109 if $req->responseCode == 401;
110 0 ▶ croak "ERROR ", $req->responseCode, " : ", $req->responseContent
Branch: 1 branch
BranchTrueFalseTotal
if $req->responseCode >= 500 0 0 0
111 if $req->responseCode >= 500;
112  
113 0 ▶ return {} if !length( $req->responseContent );
Branch: 1 branch
BranchTrueFalseTotal
unless length $req->responseContent 0 0 0
114  
115 0 my $resp;
116 0 ▶ if ( !eval { $resp = decode_json( $req->responseContent ); 1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $resp = &decode_json(scalar $req->responseContent); 1 } }) 0 0 0
117 0 croak "Bad JSON format : ", $req->responseContent;
118 }
119  
120 0 return $resp;
121 }
122  
123 3 1;
124  
125 __END__
↑ Summary API/Request.pm »