blib/lib/WWW/Shopify/URLHandler.pm

filter: Statementstmt 28.4% 25 / 88 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 7 totaltotal 17.1% 33 / 193 CC 35 SCAR 54.7
CC
35
Cov
16%
get_url
380.0
use_url
272.0
parent
6.0
CRAP 236.7
« blib/lib/WWW/Shopify/Common/DBIx.pm ↑ Summary blib/lib/WWW/Shopify/Test.pm »
1 #!/usr/bin/perl
2  
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  
6 package WWW::Shopify::URLHandler;
7 1 ▶use JSON qw(from_json to_json encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new {
14 0 ▶ my ($package, $parent) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
15 0 my $self = bless {_parent => $parent, _default_headers => {}}, $package;
16 0 weaken($self->{_parent});
17 0 return $self;
18 }
19 0 ▶sub parent { $_[0]->{_parent} = $_[1] if defined $_[1]; return $_[0]->{_parent}; }
Subroutine parent: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
20  
21 sub get_url {
22 0 ▶ my ($self, $url, $parameters, $type) = @_;
Subroutine get_url: not called CC=19 SCAR=59.4
Pod undocumented
23 0 ▶ $type = "application/json" unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
24 0 my $uri = URI->new($url);
25 0 my %filtered = ();
26 0 ▶ if ($parameters->{fields} && ref($parameters->{fields}) && ref($parameters->{fields}) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameters->{"fields"} and ref $parameters->{"fields"} and ref $parameters->{"fields"} eq "ARRAY") 0 0 0
Condition0%0/6
$parameters->{"fields"} and ref $parameters->{"fields"} and ref $parameters->{"fields"} eq "ARRAY"
!ll&&!rl&&r
000
$parameters->{"fields"} and ref $parameters->{"fields"}
!ll&&!rl&&r
000
MC/DC0%0/3
$parameters->{"fields"} and ref $parameters->{"fields"} and ref $parameters->{"fields"} eq "ARRAY"
$parameters->{"fields"} ref $parameters->{"fields"} ref $parameters->{"fields"} eq "ARRAY"
Truth table0%0/4
A and B and C
A= $parameters->{"fields"}B= ref $parameters->{"fields"}C= ref $parameters->{"fields"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
27 0 $parameters->{fields} = join(",", @{$parameters->{fields}});
28 }
29 0 for (keys(%$parameters)) {
30 0 ▶ if ($parameters->{$_} && ref($parameters->{$_}) eq "DateTime") {
Branch: 2 branches
BranchTrueFalseTotal
if ($parameters->{$_} and ref $parameters->{$_} eq 'DateTime') { } 0 0 0
elsif ($_ ne 'parent') { } 0 0 0
Condition0%0/3
$parameters->{$_} and ref $parameters->{$_} eq "DateTime"
!ll&&!rl&&r
000
MC/DC0%0/2
$parameters->{$_} and ref $parameters->{$_} eq "DateTime"
$parameters->{$_} ref $parameters->{$_} eq "DateTime"
Truth table0%0/3
A and B
A= $parameters->{$_}B= ref $parameters->{$_} eq "DateTime"
ABresult
0X0
100
111
31 0 $filtered{$_} = $parameters->{$_}->strftime('%Y-%m-%d %H:%M:%S%z')
32 }
33 elsif ($_ ne "parent") {
34 0 $filtered{$_} = $parameters->{$_};
35 }
36 }
37 0 $uri->query_form(\%filtered);
38 0 my $request = HTTP::Request->new("GET", $uri);
39 0 $request->header("Accept" => $type);
40 0 $request->header($_ => $self->{_default_headers}->{$_}) for (keys(%{$self->{_default_headers}}));
41 0 my $response = $self->parent->ua->request($request);
42 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
43 0 ▶ die new WWW::Shopify::Exception::CallLimit($response) if $response->code() == 429;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code == 429 0 0 0
44 0 ▶ die new WWW::Shopify::Exception::InvalidKey($response) if $response->code() == 401;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code == 401 0 0 0
45 0 ▶ die new WWW::Shopify::Exception::NotFound($response) if $response->code() == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code == 404 0 0 0
46 0 die new WWW::Shopify::Exception($response);
47 }
48 0 my $limit = $response->header('x-shopify-shop-api-call-limit');
49 0 ▶ if ($limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 0 0 0
50 0 ▶ die new WWW::Shopify::Exception("Unrecognized limit.") unless $limit =~ m/(\d+)\/\d+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit =~ m[(\d+)/\d+] 0 0 0
51 0 $self->parent->api_calls($1);
52 }
53 0 my $content = $response->decoded_content;
54 # From JSON because decodec content is already a perl internal string.
55 # Sigh. No. It's not. As per https://rt.cpan.org/Public/Bug/Display.html?id=82963; decoded_content doesn't actually do anything.
56 0 ▶ $content = decode("UTF-8", $content) if ($response->header('Content-Type') =~ m/application\/json/);
Branch: 1 branch
BranchTrueFalseTotal
if $response->header("Content-Type") =~ m[application/json] 0 0 0
57 0 ▶ my $decoded = !$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? from_json($content) : $content;
Branch: 1 branch
BranchTrueFalseTotal
!$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? : 0 0 0
Condition0%0/3
not $response->header("Content-Type") or $response->header("Content-Type") =~ /json/
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response->header("Content-Type") or $response->header("Content-Type") =~ /json/
not $response->header("Content-Type") $response->header("Content-Type") =~ /json/
Truth table0%0/3
not(A) or B
A= not $response->header("Content-Type")B= $response->header("Content-Type") =~ /json/
ABresult
1X1
011
000
58 0 return ($decoded, $response);
59 }
60  
61 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 sub use_url{
64 0 ▶ my ($self, $method, $url, $hash, $needs_login, $type) = @_;
Subroutine use_url: not called CC=16 SCAR=56.1
Pod undocumented
65 0 ▶ $type = "application/json" unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
66 0 my $request = HTTP::Request->new($method, $url);
67 0 $request->header("Accept" => "application/json", "Content-Type" => $type);
68 0 ▶ if ($type =~ m/json/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /json/) { } 0 0 0
69 0 ▶ $request->content($hash ? to_json($hash) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$hash ? : 0 0 0
70 } else {
71 0 ▶ $request->content(join("&", map { my $name = uri_escape_utf8($_); map { $name . "=" . uri_escape_utf8($_) } ($hash->{$_} && ref($hash->{$_}) eq "ARRAY" ? @{$hash->{$_}} : ($hash->{$_})) } keys(%$hash))) if $hash;
Branch: 2 branches
BranchTrueFalseTotal
if $hash 0 0 0
$hash->{$_} && ref $hash->{$_} eq 'ARRAY' ? : 0 0 0
Condition0%0/3
$hash->{$_} and ref $hash->{$_} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$hash->{$_} and ref $hash->{$_} eq "ARRAY"
$hash->{$_} ref $hash->{$_} eq "ARRAY"
Truth table0%0/3
A and B
A= $hash->{$_}B= ref $hash->{$_} eq "ARRAY"
ABresult
0X0
100
111
72 }
73 0 my $response = $self->parent->ua->request($request);
74 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
75 0 ▶ die new WWW::Shopify::Exception::CallLimit($response) if $response->code() == 429;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code == 429 0 0 0
76 0 ▶ die new WWW::Shopify::Exception::InvalidKey($response) if $response->code() == 401;
Branch: 1 branch
BranchTrueFalseTotal
if $response->code == 401 0 0 0
77 0 die new WWW::Shopify::Exception($response);
78 }
79 0 my $limit = $response->header('x-shopify-shop-api-call-limit');
80 0 ▶ if ($limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 0 0 0
81 0 ▶ die new WWW::Shopify::Exception("Unrecognized limit.") unless $limit =~ m/(\d+)\/\d+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $limit =~ m[(\d+)/\d+] 0 0 0
82 0 $self->parent->api_calls($1);
83 }
84 0 my $content = $response->decoded_content;
85 # From JSON because decodec content is already a perl internal string.
86 # Sigh. No. It's not. As per https://rt.cpan.org/Public/Bug/Display.html?id=82963; decoded_content doesn't actually do anything.
87 0 ▶ $content = decode("UTF-8", $content) if ($response->header('Content-Type') =~ m/application\/json/);
Branch: 1 branch
BranchTrueFalseTotal
if $response->header("Content-Type") =~ m[application/json] 0 0 0
88 0 ▶ my $decoded = (length($content) >= 2 && (!$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? from_json($content) : undef));
Branch: 1 branch
BranchTrueFalseTotal
!$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? : 0 0 0
Condition0%0/6
length $content >= 2 && (!$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? &from_json($content) : undef)
!ll&&!rl&&r
000
not $response->header("Content-Type") or $response->header("Content-Type") =~ /json/
l!l&&r!l&&!r
000
MC/DC0%0/3
length $content >= 2 && (!$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? &from_json($content) : undef)
length $content >= 2 not $response->header("Content-Type") $response->header("Content-Type") =~ /json/
Truth table0%0/4
A && not(B) or C
A= length $content >= 2B= not $response->header("Content-Type")C= $response->header("Content-Type") =~ /json/
ABCresult
0XX0
1000
11X1
1011
89 0 return ($decoded, $response);
90 }
91 0 ▶sub put_url { return shift->use_url("PUT", @_); }
Subroutine put_url: not called CC=1 SCAR=6.9
Pod undocumented
92 0 ▶sub post_url { return shift->use_url("POST", @_); }
Subroutine post_url: not called CC=1 SCAR=6.9
Pod undocumented
93 0 ▶sub delete_url { return shift->use_url("DELETE", @_); }
Subroutine delete_url: not called CC=1 SCAR=6.9
Pod undocumented
94  
95 1 1;
« blib/lib/WWW/Shopify/Common/DBIx.pm ↑ Summary blib/lib/WWW/Shopify/Test.pm »