| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$parameters->{"fields"} and ref $parameters->{"fields"} and ref $parameters->{"fields"} eq "ARRAY"
$parameters->{"fields"} and ref $parameters->{"fields"}
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | $parameters->{fields} = join(",", @{$parameters->{fields}}); | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | for (keys(%$parameters)) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if ($parameters->{$_} && ref($parameters->{$_}) eq "DateTime") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$parameters->{$_} and ref $parameters->{$_} eq "DateTime"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | die new WWW::Shopify::Exception::CallLimit($response) if $response->code() == 429; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | die new WWW::Shopify::Exception::InvalidKey($response) if $response->code() == 401; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | die new WWW::Shopify::Exception::NotFound($response) if $response->code() == 404; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | die new WWW::Shopify::Exception("Unrecognized limit.") unless $limit =~ m/(\d+)\/\d+/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $decoded = !$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? from_json($content) : $content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $response->header("Content-Type") or $response->header("Content-Type") =~ /json/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $request->content($hash ? to_json($hash) : undef); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$hash->{$_} and ref $hash->{$_} eq "ARRAY"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $response = $self->parent->ua->request($request); | |||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (!$response->is_success) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | die new WWW::Shopify::Exception::CallLimit($response) if $response->code() == 429; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | die new WWW::Shopify::Exception::InvalidKey($response) if $response->code() == 401; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | die new WWW::Shopify::Exception("Unrecognized limit.") unless $limit =~ m/(\d+)\/\d+/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my $decoded = (length($content) >= 2 && (!$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? from_json($content) : undef)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
length $content >= 2 && (!$response->header('Content-Type') || $response->header('Content-Type') =~ /json/ ? &from_json($content) : undef)
not $response->header("Content-Type") or $response->header("Content-Type") =~ /json/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||