blib/lib/WWW/ShipStation.pm

filter: Statementstmt 21.5% 22 / 102 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 28.0% 7 / 25 Podpod 82.3% 14 / 17 totaltotal 21.7% 43 / 198 CC 23 SCAR 28.5
CC
23
Cov
15%
new
56.0
request
30.0
__request
30.0
CRAP 17.2
↑ Summary
1 package WWW::ShipStation;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.06';
6  
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI::Escape qw/uri_escape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
14 0 my $class = shift;
15 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
16  
17 0 ▶ $args{user} or croak "user is required.";
Branch: 1 branch
BranchTrueFalseTotal
$args{"user"} or croak("user is required.") 0 0 0
18 0 ▶ $args{pass} or croak "pass is required.";
Branch: 1 branch
BranchTrueFalseTotal
$args{"pass"} or croak("pass is required.") 0 0 0
19  
20 0 ▶ $args{ua} ||= LWP::UserAgent->new();
Condition0%0/3
$args{"ua"} ||= "LWP::UserAgent"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"ua"} ||= "LWP::UserAgent"->new
$args{"ua"} "LWP::UserAgent"->new
Truth table0%0/3
A ||= B
A= $args{"ua"}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
21 0 ▶ $args{json} ||= JSON->new->allow_nonref->utf8;
Condition0%0/3
$args{"json"} ||= "JSON"->new->allow_nonref->utf8
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"json"} ||= "JSON"->new->allow_nonref->utf8
$args{"json"} "JSON"->new->allow_nonref->utf8
Truth table0%0/3
A ||= B
A= $args{"json"}B= "JSON"->new->allow_nonref->utf8
ABresult
1X1
011
000
22  
23 0 ▶ $args{API_BASE} ||= 'https://ssapi.shipstation.com/';
Condition0%0/2
$args{"API_BASE"} ||= "https://ssapi.shipstation.com/"
l!l
00
MC/DC0%0/1
$args{"API_BASE"} ||= "https://ssapi.shipstation.com/"
$args{"API_BASE"}
Truth table0%0/2
A
A= $args{"API_BASE"}
Aresult
11
00
24  
25 0 bless \%args, $class;
26 }
27  
28 0 ▶sub getCarriers {
Subroutine getCarriers: not called CC=1 SCAR=6.9
Pod documented
29 0 (shift)->request('carriers');
30 }
31  
32 0 ▶sub getCustomer {
Subroutine getCustomer: not called CC=1 SCAR=6.9
Pod documented
33 0 my ($self, $customerId) = @_;
34 0 $self->request("customers/$customerId");
35 }
36  
37 0 ▶sub getCustomers {
Subroutine getCustomers: not called CC=2 SCAR=17.9
Pod documented
38 0 my $self = shift;
39 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
40 0 $self->request('customers', %args);
41 }
42  
43 0 ▶sub getOrder {
Subroutine getOrder: not called CC=1 SCAR=6.9
Pod documented
44 0 my ($self, $orderId) = @_;
45 0 $self->request("orders/$orderId");
46 }
47  
48 0 ▶sub getOrders {
Subroutine getOrders: not called CC=2 SCAR=17.9
Pod documented
49 0 my $self = shift;
50 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
51 0 $self->request('orders', %args);
52 }
53  
54 0 ▶sub getProduct {
Subroutine getProduct: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my ($self, $productId) = @_;
56 0 $self->request("products/$productId");
57 }
58  
59 0 ▶sub getProducts {
Subroutine getProducts: not called CC=2 SCAR=17.9
Pod documented
60 0 my $self = shift;
61 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
62 0 $self->request('products/', %args);
63 }
64  
65 0 ▶sub getShipments {
Subroutine getShipments: not called CC=2 SCAR=17.9
Pod documented
66 0 my $self = shift;
67 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
68 0 $self->request('shipments', %args);
69 }
70  
71 0 ▶sub getMarketplaces {
Subroutine getMarketplaces: not called CC=2 SCAR=17.9
Pod documented
72 0 my $self = shift;
73 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
74 0 $self->request('stores/marketplaces', %args);
75 }
76  
77 0 ▶sub getStores {
Subroutine getStores: not called CC=2 SCAR=17.9
Pod documented
78 0 my $self = shift;
79 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
80 0 $self->request('stores', %args);
81 }
82  
83 0 ▶sub getStore {
Subroutine getStore: not called CC=1 SCAR=6.9
Pod undocumented
84 0 my ($self, $storeId) = @_;
85 0 $self->request("stores/$storeId");
86 }
87  
88 0 ▶sub getWarehouses {
Subroutine getWarehouses: not called CC=2 SCAR=17.9
Pod documented
89 0 my $self = shift;
90 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
91 0 $self->request('warehouses', %args);
92 }
93  
94 0 ▶sub getWarehouse {
Subroutine getWarehouse: not called CC=1 SCAR=6.9
Pod undocumented
95 0 my ($self, $warehouseId) = @_;
96 0 $self->request("warehouses/$warehouseId");
97 }
98  
99 0 ▶sub request {
Subroutine request: not called CC=5 SCAR=34.0
Pod documented
100 0 my ($self, $url, %params) = @_;
101  
102 0 ▶ if (%params and keys %params) {
Branch: 1 branch
BranchTrueFalseTotal
if (%params and keys %params) 0 0 0
Condition0%0/3
%params and keys %params
!ll&&!rl&&r
000
MC/DC0%0/2
%params and keys %params
%params keys %params
Truth table0%0/3
A and B
A= %paramsB= keys %params
ABresult
0X0
100
111
103 0 $url .= '?' . join('&', map { join('=', $_, uri_escape($params{$_})) } keys %params);
104 }
105  
106 0 my $req = HTTP::Request->new(GET => $self->{API_BASE} . $url);
107 0 $req->authorization_basic($self->{user}, $self->{pass});
108 0 $req->header('Accept', 'application/json'); # JSON is better
109 0 my $res = $self->{ua}->request($req);
110 # use Data::Dumper; print STDERR Dumper(\$res);
111 0 ▶ if ($res->header('Content-Type') =~ m{application/json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header("Content-Type") =~ m[application/json]) 0 0 0
112 0 return $self->{json}->decode($res->decoded_content);
113 }
114 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
115 return {
116 0 'error' => {
117 'code' => '',
118 'message' => {
119 'lang' => 'en-US',
120 'value' => $res->status_line,
121 }
122 }
123 };
124 }
125 }
126  
127 0 ▶sub createOrder {
Subroutine createOrder: not called CC=2 SCAR=17.9
Pod documented
128 0 my $self = shift;
129 0 ▶ my %args = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
130 0 $self->__request('POST', 'orders/createorder', $self->{json}->encode(\%args));
131 }
132  
133 0 ▶sub deleteOrder {
Subroutine deleteOrder: not called CC=1 SCAR=6.9
Pod documented
134 0 my ($self, $orderID) = @_;
135  
136 0 $self->__request('DELETE', "orders/$orderID");
137 }
138  
139 0 ▶sub __request {
Subroutine __request: not called CC=5 SCAR=34.0
140 0 my ($self, $method, $url, $content) = @_;
141  
142 0 my $req = HTTP::Request->new($method => $self->{API_BASE} . $url);
143 0 $req->authorization_basic($self->{user}, $self->{pass});
144 0 $req->header('Accept', 'application/json'); # JSON is better
145 0 $req->header('Accept-Charset' => 'UTF-8');
146 0 ▶ if ($method eq 'POST') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST") 0 0 0
147 0 $req->header('Content-Type' => 'application/json');
148 }
149 0 ▶ $req->content($content) if $content;
Branch: 1 branch
BranchTrueFalseTotal
if $content 0 0 0
150  
151 0 my $res = $self->{ua}->request($req);
152 # use Data::Dumper; print STDERR Dumper(\$res);
153 0 ▶ if ($res->header('Content-Type') =~ m{application/json}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header("Content-Type") =~ m[application/json]) 0 0 0
154 0 return $self->{json}->decode($res->decoded_content);
155 }
156 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
157 return {
158 0 'error' => {
159 'code' => '',
160 'message' => {
161 'lang' => 'en-US',
162 'value' => $res->status_line
163 }
164 }
165 };
166 }
167 0 return $res->decoded_content;
168 }
169  
170 1 1;
171 __END__
↑ Summary