blib/lib/Net/Zendesk.pm

filter: Statementstmt 66.6% 52 / 78 Branchbran 58.0% 36 / 62 Conditioncond 17.1% 6 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 60.0% 6 / 10 Podpod 75.0% 3 / 4 totaltotal 49.5% 103 / 208 CC 48 SCAR 39.1
CC
48
Cov
54%
make_request
156.0
_parse_search_args
28.0
new
22.5
CRAP 50.1
↑ Summary
1 package Net::Zendesk;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.02';
8  
9 1 ▶sub new {
Subroutine new: called CC=10 SCAR=31.1
Pod undocumented
10 1 my ($class, %args) = @_;
11 die 'please provide a zendesk domain name (e.g. domain => "obscura.zendesk.com")'
12 1 ▶ unless $args{domain} && $args{domain} =~ /\.zendesk\.com\z/ && $args{domain} !~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"domain"} and $args{"domain"} =~ /\.zendesk\.com\z/ and not $args{"domain"} =~ m[/] 0 1 1
Condition33%2/6
$args{"domain"} and $args{"domain"} =~ /\.zendesk\.com\z/ and not $args{"domain"} =~ m[/]
!ll&&!rl&&r
001
$args{"domain"} and $args{"domain"} =~ /\.zendesk\.com\z/
!ll&&!rl&&r
001
MC/DC0%0/3
$args{"domain"} and $args{"domain"} =~ /\.zendesk\.com\z/ and not $args{"domain"} =~ m[/]
$args{"domain"} $args{"domain"} =~ /\.zendesk\.com\z/ not $args{"domain"} =~ m[/]
Truth table25%1/4
A and B and not(C)
A= $args{"domain"}B= $args{"domain"} =~ /\.zendesk\.com\z/C= not $args{"domain"} =~ m[/]
ABCresult
0XX0
10X0
1100
1111
13  
14 die 'sorry! only API version 2 is supported at the moment'
15 1 ▶ if exists $args{api} && $args{api} != 2;
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"api"} and $args{"api"} != 2 0 1 1
Condition33%1/3
exists $args{"api"} and $args{"api"} != 2
!ll&&!rl&&r
100
MC/DC0%0/2
exists $args{"api"} and $args{"api"} != 2
exists $args{"api"} $args{"api"} != 2
Truth table33%1/3
A and B
A= exists $args{"api"}B= $args{"api"} != 2
ABresult
0X0
100
111
16  
17 1 ▶ die 'please provide the email of a valid zendesk account' unless $args{email};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"email"} 0 1 1
18 1 ▶ if ($args{token}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'token'}) { } 1 0 1
elsif ($args{'password'}) { } 0 0 0
19 1 $args{auth} = "$args{email}/token:$args{token}";
20 }
21 elsif ($args{password}) {
22 0 $args{auth} = "$args{email}:$args{password}";
23 }
24 else {
25 0 die 'please provide a password or a token for zendesk authentication. Oauth is not yet supported by this module';
26 }
27  
28 return bless {
29 _domain => $args{domain},
30 _api => $args{api},
31 _auth => MIME::Base64::encode($args{auth}),
32 _ua => $args{ua} || undef,
33 1 ▶ }, $class;
Condition50%1/2
$args{'ua'} || undef
l!l
01
MC/DC0%0/1
$args{'ua'} || undef
$args{'ua'}
Truth table50%1/2
A
A= $args{'ua'}
Aresult
11
00
34 }
35  
36 0 ▶sub create_ticket {
Subroutine create_ticket: not called CC=2 SCAR=17.9
Pod documented
37 0 my ($self, $ticket, %extra) = @_;
38 0 my $path = 'tickets.json';
39 0 ▶ if (%extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (%extra) 0 0 0
40 0 $path .= '?' . join('&', map("$_=$extra{$_}", keys %extra));
41 }
42 0 return $self->make_request('POST', $path, { ticket => $ticket });
43 }
44  
45 0 ▶sub search {
Subroutine search: not called CC=1 SCAR=6.9
Pod documented
46 0 my ($self, $search_args) = @_;
47 0 my $parsed_args = $self->_parse_search_args($search_args);
48  
49 0 require URI::Escape;
50 0 my $query = URI::Escape::uri_escape(join(' ' => @$parsed_args));
51  
52 0 return $self->make_request('GET', 'search.json?query=' . $query, {});
53 }
54  
55 0 ▶sub make_request {
Subroutine make_request: not called CC=12 SCAR=50.5
Pod documented
56 0 my ($self, $type, $path, $params) = @_;
57 0 ▶ die 'please provide a type' unless $type
Branch: 1 branch
BranchTrueFalseTotal
unless $type and $type eq "GET" || $type eq "POST" || $type eq "PUT" || $type eq "DELETE" 0 0 0
Condition0%0/12
$type and $type eq "GET" || $type eq "POST" || $type eq "PUT" || $type eq "DELETE"
!ll&&!rl&&r
000
$type eq "GET" or $type eq "POST" or $type eq "PUT" or $type eq "DELETE"
l!l&&r!l&&!r
000
$type eq "GET" or $type eq "POST" or $type eq "PUT"
l!l&&r!l&&!r
000
$type eq "GET" or $type eq "POST"
l!l&&r!l&&!r
000
MC/DC0%0/5
$type and $type eq "GET" || $type eq "POST" || $type eq "PUT" || $type eq "DELETE"
$type $type eq "GET" $type eq "POST" $type eq "PUT" $type eq "DELETE"
Truth table0%0/6
A and B or C or D or E
A= $typeB= $type eq "GET"C= $type eq "POST"D= $type eq "PUT"E= $type eq "DELETE"
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
58 && ($type eq 'GET' || $type eq 'POST' || $type eq 'PUT' || $type eq 'DELETE');
59 0 ▶ die 'please provide a relative path' unless $path && $path !~ m{\A/api};
Branch: 1 branch
BranchTrueFalseTotal
unless $path and not $path =~ m[\A/api] 0 0 0
Condition0%0/3
$path and not $path =~ m[\A/api]
!ll&&!rl&&r
000
MC/DC0%0/2
$path and not $path =~ m[\A/api]
$path not $path =~ m[\A/api]
Truth table0%0/3
A and not(B)
A= $pathB= not $path =~ m[\A/api]
ABresult
0X0
100
111
60 0 ▶ die 'please provide a HASHREF with parameters' unless $params && ref $params eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $params and ref $params eq "HASH" 0 0 0
Condition0%0/3
$params and ref $params eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$params and ref $params eq "HASH"
$params ref $params eq "HASH"
Truth table0%0/3
A and B
A= $paramsB= ref $params eq "HASH"
ABresult
0X0
100
111
61 0 my $method = lc $type;
62 return $self->_ua->$method(
63 0 ▶ 'https://' . $self->{_domain} . '/api/v2/' . $path,
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'post' || $method eq 'put' ? : 0 0 0
Condition0%0/3
$method eq "post" or $method eq "put"
l!l&&r!l&&!r
000
MC/DC0%0/2
$method eq "post" or $method eq "put"
$method eq "post" $method eq "put"
Truth table0%0/3
A or B
A= $method eq "post"B= $method eq "put"
ABresult
1X1
011
000
64 [
65 ($method eq 'post' || $method eq 'put'
66 ? ('Content-Type' => 'application/json') : ()
67 ),
68 ],
69 encode_json($params),
70 );
71 }
72  
73 20 ▶sub _parse_search_args {
Subroutine _parse_search_args: called CC=26 SCAR=33.3
74 20 my ($self, $search_args) = @_;
75 20 my @query;
76 20 foreach my $keyword (keys %$search_args) {
77 20 ▶ die "Net::Zendesk: malformed search keyword '$keyword' contains spaces"
Branch: 1 branch
BranchTrueFalseTotal
if $keyword =~ /\s/ 0 20 20
78 if $keyword =~ /\s/;
79 20 my $value = $search_args->{$keyword};
80 20 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 15 5 20
81 15 ▶ if (ref $value eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 13 2 15
elsif (ref $value eq 'ARRAY') { } 2 0 2
82 13 foreach my $inner_key (keys %$value) {
83 15 my %tokens = (
84 '=' => ':',
85 '>' => '>',
86 '<' => '<',
87 '>=' => '>=',
88 '<=' => '<=',
89 '!=' => ':',
90 'or' => ':',
91 'and' => ':',
92 );
93 15 ▶ die "Net::Zendesk: invalid token '$inner_key' for keyword '$keyword'. Available tokens are " . join(', ', keys %tokens) unless exists $tokens{$inner_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tokens{$inner_key} 0 15 15
94  
95 15 my $inner_value = $value->{$inner_key};
96 15 ▶ $inner_value = 'none' unless defined $inner_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inner_value 2 13 15
97  
98 15 ▶ if (ref $inner_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inner_value) { } 3 12 15
99 3 ▶ die 'Net::Zendesk: only scalar values or ARRAY references are supported. Got ' . ref($inner_value) . " for keyword '$keyword' under '$inner_key'." unless ref $inner_value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $inner_value eq "ARRAY" 0 3 3
100 3 ▶ if ($inner_key eq 'and') {
Branch: 2 branches
BranchTrueFalseTotal
if ($inner_key eq 'and') { } 1 2 3
elsif ($inner_key eq '=' or $inner_key eq 'or') { } 2 0 2
101 push @query, $keyword . ':'
102 . join ' ', map {
103 1 ▶ defined $_ ? $_ =~ /\s/ ? qq("$_") : $_ : 'none'
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 1 1 2
$_ =~ /\s/ ? : 0 1 1
104 } @$inner_value;
105 }
106 ▶ elsif ($inner_key eq '=' || $inner_key eq 'or') {
Condition66%2/3
$inner_key eq "=" or $inner_key eq "or"
l!l&&r!l&&!r
110
MC/DC0%0/2
$inner_key eq "=" or $inner_key eq "or"
$inner_key eq "=" $inner_key eq "or"
Truth table66%2/3
A or B
A= $inner_key eq "="B= $inner_key eq "or"
ABresult
1X1
011
000
107 2 foreach my $or (@$inner_value) {
108 4 ▶ $or = 'none' unless defined $or;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $or 2 2 4
109 4 ▶ $or = qq("$or") if $or =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $or =~ /\s/ 0 4 4
110 4 push @query, "$keyword$tokens{$inner_key}$or";
111 }
112 }
113 else {
114 0 die 'Net::Zendesk: only =,and,or tokens are allowed for references';
115 }
116 }
117 else {
118 12 ▶ $inner_value = qq("$inner_value") if $inner_value =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $inner_value =~ /\s/ 2 10 12
119 12 ▶ push @query, ($inner_key eq '!=' ? '-' : '')
Branch: 1 branch
BranchTrueFalseTotal
$inner_key eq '!=' ? : 1 11 12
120 . "$keyword$tokens{$inner_key}$inner_value";
121 }
122 }
123 }
124 elsif (ref $value eq 'ARRAY') {
125 2 foreach my $or (@$value) {
126 4 ▶ $or = 'none' unless defined $or;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $or 1 3 4
127 4 ▶ $or = qq("$or") if $or =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $or =~ /\s/ 0 4 4
128 4 push @query, "$keyword:$or";
129 }
130 }
131 else {
132 0 die 'Net::Zendesk: unsuported reference ' . ref($value) . '. Please use either a scalar or an ARRAY/HASH reference as a value for ' . $keyword;
133 }
134 }
135 else {
136 5 ▶ $value = 'none' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 4 5
137 5 ▶ $value = qq("$value") if $value =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\s/ 1 4 5
138 5 push @query, "$keyword:$value";
139 }
140 }
141 20 return \@query;
142 }
143  
144 0 ▶sub _ua {
Subroutine _ua: not called CC=2 SCAR=17.9
145 0 my ($self) = @_;
146 0 ▶ return $self->{_ua} if $self->{_ua};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_ua"} 0 0 0
147 0 require Furl;
148 0 require IO::Socket::SSL;
149 0 IO::Socket::SSL->import;
150 $self->{_ua} = Furl->new(
151 headers => [
152 'Accept' => 'application/json',
153 'Authorization' => 'Basic ' . $self->{_auth},
154 0 ],
155 ssl_opts => {
156 SSL_verify_mode => SSL_VERIFY_PEER(),
157 },
158 );
159 }
160  
161 2 1;
162 __END__
↑ Summary