blib/lib/Snapforce/API.pm

filter: Statementstmt 85.7% 12 / 14 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 89.4% 17 / 19 CC 1 SCAR 0.6
CC
1
Cov
86%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Snapforce::API;
2  
3 1 ▶use 5.018002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use HTTP::Request::Common qw/GET POST/;
9 use LWP::UserAgent;
10  
11 require Exporter;
12  
13 our $VERSION = '0.02';
14 our $SNAPURL = 'https://crm2.snapforce.com/sf_receive_request.inc.php';
15  
16 sub new {
17 my ($class, $authuser, $authkey) = @_;
18 croak "Authentication user and/or key are missing" unless $authkey;
19 my $self = bless {
20 authuser => $authuser,
21 authkey => $authkey
22 }, $class;
23 return $self;
24 }
25  
26 sub fetchRecords {
27 my $self = shift;
28 return runAPI($self, 'method' => "fetchRecords", @_);
29 }
30  
31 sub runAPI {
32 my ($self, %args) = @_;
33 carp "No method specified for explicit call of runAPI. Defaulting to fetchRecords" if not $args{'method'};
34 my %params = (
35 format => "xml",
36 module => "Accounts",
37 api_user => $self->{authuser},
38 api_key => $self->{authkey},
39 status => "Active",
40 method => "fetchRecords"
41 );
42 @params{keys %args} = values %args;
43 my $ua = new LWP::UserAgent;
44 my $resp = $ua->request(POST $SNAPURL, [%params]);
45 return $resp->content;
46 }
47  
48 sub parseXML {
49 my ($self, $info) = @_;
50 my %data = ();
51 my @tree = ();
52 my $spot = "";
53 my $word = "";
54  
55 my $sthdl = sub {
56 my ($prsr, $elem, %atts) = @_;
57 my $obj = \%data;
58 $obj = ${$obj}{$_} foreach (@tree);
59 $spot = $elem.(%atts ? "-".$atts{(keys %atts)[0]} : "");
60 push @tree, $spot;
61 ${$obj}{$spot} = {};
62 $word = "";
63 };
64 my $edhdl = sub {
65 my ($prsr, $elem) = @_;
66 pop @tree;
67 my $obj = \%data;
68 $obj = ${$obj}{$_} foreach @tree;
69 ${$obj}{$spot} = $word;
70 $spot = "";
71 };
72 my $chhdl = sub {
73 $word .= $_[1];
74 };
75  
76 my $parser = XML::Parser->new(Handlers => {
77 Start => \&$sthdl,
78 End => \&$edhdl,
79 Char => \&$chhdl
80 });
81
82 $parser->parse($info);
83 delete $data{""};
84 delete $data{(keys %data)[0]}{''};
85 return %data;
86 }
87 1;
88 __END__
↑ Summary