Intis.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 141 CC 9 SCAR 32.5
CC
9
Cov
0%
connect
72.0
new
6.0
BEGIN
2.0
CRAP 25.7
1 package API::Intis;
2 use Modern::Perl;
3 use utf8;
4  
5 BEGIN {
6 use Exporter ();
7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
8 $VERSION = '1.00';
9 @ISA = qw(Exporter);
10 #Give a hoot don't pollute, do not export more than needed by default
11 @EXPORT = qw();
12 @EXPORT_OK = qw();
13 %EXPORT_TAGS = ();
14 }
15  
16 package API::Intis::APIGrab;
17 use YAML::Tiny;
18 use WWW::Mechanize;
19 use Crypt::SSLeay;
20 use Digest::Perl::MD5 'md5_hex';
21 use JSON;
22 use error_codes;
23  
24  
25 sub readConfig {
26 my $conf = YAML::Tiny->read( 'config.yaml' );
27 return (login => $conf->[0]->{APIconnector}->{login}, APIkey => $conf->[0]->{APIconnector}->{APIkey}, host => $conf->[0]->{APIconnector}->{host});
28 };
29  
30 sub build_signature {
31 my (%params) = @_;
32 delete $params{host};
33 my $APIkey = delete $params{APIkey};
34 my @ssignature;
35 foreach my $key(sort keys %params){
36 say "$key => $params{$key}";
37 push @ssignature, $params{$key};
38 };
39 return md5_hex join('', @ssignature).$APIkey;
40 };
41  
42 sub connect {
43 my ($method, $other_params) = @_;
44 my $ua = WWW::Mechanize->new(ssl_opts => { verify_hostname => 0 } );
45 $ua->cookie_jar(HTTP::Cookies->new());
46 $ua->agent_alias('Linux Mozilla');
47 my %config = &readConfig();
48 my $timestamp = $ua->get('https://go.intistele.com/external/get/timestamp.php')->content( raw => 1 );
49 my %timestamp = (timestamp => $timestamp);
50 my $output_format;
51 if ($other_params ne '') {
52 my %other_params = %{$other_params};
53 foreach my $key (keys %other_params) {
54 if ($key eq 'return') {
55 $output_format = delete $other_params{$key};
56 };
57 };
58 %config = (%config, %timestamp, %other_params);
59 } else {
60 %config = (%config, %timestamp);
61 };
62 my @o_formats = ('xml', 'json');
63 my $request_json; my $request_xml;
64 foreach my $format (@o_formats) {
65 $config{return} = $format;
66 my $signature = &build_signature(%config);
67 my $url = "$config{host}$method.php?login=$config{login}&signature=$signature";
68 while((my $key, my $value) = each %config){
69 say "$key => $value\n";
70 next if $key eq 'host' || $key eq 'login' || $key eq 'APIkey';
71 $url .= "&$key=$value";
72 };
73 my $request = $ua->get("$url&return=$format")->decoded_content(charset => 'utf-8', raw => 1);
74 $request_json = $request if $format eq 'json';
75 $request_xml = $request if $format eq 'xml';
76 };
77 my $r = from_json($request_json);
78 my @error;
79 if ($r->{error}) {
80 @error = &error_codes::get_name_from_code($r->{error});
81 } else {
82 @error = &error_codes::get_name_from_code();
83 };
84 return (request_json => $request_json, error => \@error, request_xml => $request_xml, request_object => \%{$r}, out_format => !defined $output_format ? 'json' : $output_format );
85 };
86  
87 package API::Intis::APIRequest;
88 use JSON;
89 sub new {
90 my($class, $method, $other_params) = @_;
91 my %request_params;
92 if (defined $other_params) {
93 %request_params = &API::Intis::APIGrab::connect($method, $other_params);
94 } else {
95 %request_params = &API::Intis::APIGrab::connect($method, '');
96 };
97 my $self = {
98 name => 'API::Intis::APIRequest',
99 version => '1.0',
100 method => $method,
101 request_json => $request_params{request_json},
102 error => $request_params{error},
103 request_xml => $request_params{request_xml},
104 request_object => $request_params{request_object},
105 out_format => $request_params{out_format},
106 };
107 bless $self, $class;
108 return $self;
109 };
110  
111 1;
112 # The preceding line will help the module return a true value
113