| 1 | package Net::CapsuleCRM; | ||||||||||||||||||
| 2 | 1 | $Net::CapsuleCRM::VERSION = '1.151910'; | |||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 1 | ▶ | use Sub::Quote; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 1 | ▶ | use Method::Signatures; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 1 | ▶ | use Cpanel::JSON::XS; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 1 | ▶ | use HTTP::Request::Common; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 1 | ▶ | use XML::Simple; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | # ABSTRACT: Connect to the Capsule API (www.capsulecrm.com) | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | 1 | has 'debug' => (is => 'rw', predicate => 'is_debug'); | |||||||||||||||||
| 17 | 1 | has 'error' => (is => 'rw', predicate => 'has_error'); | |||||||||||||||||
| 18 | 1 | has 'token' => (is => 'rw', required => 1); | |||||||||||||||||
| 19 | has 'ua' => (is => 'rw', | ||||||||||||||||||
| 20 | 0 | ▶ | default => sub { LWP::UserAgent->new( agent => 'Perl Net-CapsuleCRM'); } ); | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||
| 21 | 0 | ▶ | has 'target_domain' => (is => 'rw', default => sub { 'test.capsulecrm.com' } ); | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||
| 22 | 0 | ▶ | has 'xmls' => ( is => 'rw', default => sub { return XML::Simple->new( | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||
| 23 | NoAttr => 1, KeyAttr => [], XMLDecl => 1, SuppressEmpty => 1, ); } | ||||||||||||||||||
| 24 | 1 | ); | |||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | 0 | ▶ | method endpoint_uri { return 'https://' . $self->target_domain . '/api/'; } | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.1
Subroutine
endpoint_uri: not called CC=2 SCAR=12.4
Branch: 1 branch
| |||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | 0 | ▶ | method _talk($command,$method,$content?) { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=5.0
Subroutine
_talk: not called CC=19 SCAR=58.6
Branch: 3 branches
| |||||||||||||||||||
| 29 | 0 | my $uri = URI->new($self->endpoint_uri); | |||||||||||||||||
| 30 | 0 | $uri->path("api/$command"); | |||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | 0 | $self->ua->credentials( | |||||||||||||||||
| 33 | $uri->host . ':'.$uri->port, | ||||||||||||||||||
| 34 | 'seamApp', | ||||||||||||||||||
| 35 | $self->token => 'x' | ||||||||||||||||||
| 36 | ); | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | 0 | ▶ | print "$uri\n" if $self->debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 0 | my $res; | |||||||||||||||||
| 41 | 0 | ▶ | my $type = ref $content eq 'HASH' ? 'json' : 'xml'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | 0 | ▶ | if($method =~ /get/i){ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 43 | 0 | ▶ | if(ref $content eq 'HASH') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | 0 | $uri->query_form($content); | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | 0 | $res = $self->ua->request( | |||||||||||||||||
| 47 | GET $uri, #content is ID in this instance. | ||||||||||||||||||
| 48 | Accept => 'application/json', | ||||||||||||||||||
| 49 | Content_Type => 'application/json', | ||||||||||||||||||
| 50 | ); | ||||||||||||||||||
| 51 | } else { | ||||||||||||||||||
| 52 | #$content = $self->_template($content) if $content; | ||||||||||||||||||
| 53 | 0 | ▶ | if($type eq 'json') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 54 | 0 | ▶ | print "Encoding as JSON\n" if $self->debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 55 | 0 | $content = encode_json $content; | |||||||||||||||||
| 56 | 0 | ▶ | print "$content\n" if $self->debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 57 | 0 | $res = $self->ua->request( | |||||||||||||||||
| 58 | POST $uri, | ||||||||||||||||||
| 59 | Accept => 'application/json', | ||||||||||||||||||
| 60 | Content_Type => 'application/json', | ||||||||||||||||||
| 61 | Content => $content, | ||||||||||||||||||
| 62 | ); | ||||||||||||||||||
| 63 | } else { | ||||||||||||||||||
| 64 | #otherwise XML | ||||||||||||||||||
| 65 | 0 | $content = $self->xmls->XMLout($content, RootName => $command); | |||||||||||||||||
| 66 | 0 | ▶ | print "Encoding as XML\n" if $self->debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 67 | 0 | $res = $self->ua->request( | |||||||||||||||||
| 68 | POST $uri, | ||||||||||||||||||
| 69 | Accept => 'text/xml', | ||||||||||||||||||
| 70 | Content_Type => 'text/xml', | ||||||||||||||||||
| 71 | Content => $content, | ||||||||||||||||||
| 72 | ); | ||||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 0 | ▶ | if ($res->is_success) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 79 | 0 | ▶ | print "Server said: ", $res->status_line, "\n" if $self->debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 80 | 0 | ▶ | if($res->status_line =~ /^201/) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 81 | 0 | return (split '/', $res->header('Location'))[-1] | |||||||||||||||||
| 82 | } else { | ||||||||||||||||||
| 83 | 0 | ▶ | print $res->content. "\n" if $self->debug; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | 0 | ▶ | if($type eq 'json') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 85 | 0 | return decode_json $res->content; | |||||||||||||||||
| 86 | } elsif($res->content) { | ||||||||||||||||||
| 87 | 0 | return XMLin $res->content; | |||||||||||||||||
| 88 | } else { | ||||||||||||||||||
| 89 | 0 | return 1; | |||||||||||||||||
| 90 | } | ||||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | } else { | ||||||||||||||||||
| 93 | 0 | $self->error($res->status_line); | |||||||||||||||||
| 94 | 0 | warn $self->error; | |||||||||||||||||
| 95 | 0 | ▶ | if ($self->debug) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 96 | 0 | print $res->content; | |||||||||||||||||
| 97 | } | ||||||||||||||||||
| 98 | } | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | 0 | ▶ | method find_party_by_email($email) { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.1
Subroutine
find_party_by_email: not called CC=4 SCAR=26.4
Branch: 2 branches
| |||||||||||||||||||
| 105 | 0 | my $res = $self->_talk('party', 'GET', { | |||||||||||||||||
| 106 | email => $email, | ||||||||||||||||||
| 107 | start => 0, | ||||||||||||||||||
| 108 | }); | ||||||||||||||||||
| 109 | 0 | ▶ | return $res->{'parties'}->{'person'}->{'id'} || undef; | ||||||||||||||||
|
Condition0%0/2
$res->{'parties'}{'person'}{'id'} || undef
MC/DC0%0/1
$res->{'parties'}{'person'}{'id'} || undef
$res->{'parties'}{'person'}{'id'}
Truth table0%0/2
A
A= $res->{'parties'}{'person'}{'id'}
| |||||||||||||||||||
| 110 | } | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | |||||||||||||||||||
| 113 | 0 | ▶ | method find_party($id) { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.1
Subroutine
find_party: not called CC=4 SCAR=26.4
Branch: 2 branches
| |||||||||||||||||||
| 114 | 0 | my $res = $self->_talk('party/'.$id, 'GET', $id); | |||||||||||||||||
| 115 | 0 | ▶ | return $res->{'parties'}->{'person'}->{'id'} || undef; | ||||||||||||||||
|
Condition0%0/2
$res->{'parties'}{'person'}{'id'} || undef
MC/DC0%0/1
$res->{'parties'}{'person'}{'id'} || undef
$res->{'parties'}{'person'}{'id'}
Truth table0%0/2
A
A= $res->{'parties'}{'person'}{'id'}
| |||||||||||||||||||
| 116 | } | ||||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | 0 | ▶ | method create_person($data) { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.1
Subroutine
create_person: not called CC=3 SCAR=20.7
Branch: 2 branches
| |||||||||||||||||||
| 120 | 0 | return $self->_talk('person', 'POST', { person => $data } ); | |||||||||||||||||
| 121 | } | ||||||||||||||||||
| 122 | |||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | 0 | ▶ | method create_organisation($data) { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.1
Subroutine
create_organisation: not called CC=3 SCAR=20.7
Branch: 2 branches
| |||||||||||||||||||
| 125 | 0 | return $self->_talk('organisation', 'POST', { organisation => $data } ); | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 0 | ▶ | method add_tag($id, @tags) { | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=3.5
Subroutine
add_tag: not called CC=3 SCAR=20.3
Branch: 1 branch
| |||||||||||||||||||
| 129 | # my $data = $self->xmls->XMLout( | ||||||||||||||||||
| 130 | # { tag => [ map { name => $_ }, @tags ] }, RootName => 'tags' | ||||||||||||||||||
| 131 | # ); | ||||||||||||||||||
| 132 | 0 | foreach(@tags) { | |||||||||||||||||
| 133 | 0 | $self->_talk("party/$id/tag/$_", 'POST'); | |||||||||||||||||
| 134 | } | ||||||||||||||||||
| 135 | } | ||||||||||||||||||
| 136 | |||||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | 1 | 1; | |||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||