lib/Mojolicious/Plugin/Pingen.pm

filter: Statementstmt 84.1% 106 / 126 Branchbran 57.5% 23 / 40 Conditioncond 53.3% 8 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 91.6% 22 / 24 Podpod 100% 1 / 1 totaltotal 75.2% 161 / 214 CC 31 SCAR 14.7
CC
31
Cov
76%
_document_upload
8.6
_document_send
8.2
_tx_to_json
6.5
CRAP 4.3
↑ Summary
1 package Mojolicious::Plugin::Pingen;
2 3 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Mojo::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use constant DEBUG => $ENV{MOJO_PINGEN_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_PINGEN_DEBUG'} || 0
l!l
03
MC/DC0%0/1
$ENV{'MOJO_PINGEN_DEBUG'} || 0
$ENV{'MOJO_PINGEN_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_PINGEN_DEBUG'}
Aresult
11
00
8 3 our $VERSION = '0.2.3';
9  
10 =head1 NAME
11  
12 Mojolicious::Plugin::Pingen - Print Package Send Physical letters
13  
14 =head1 DESCRIPTION
15  
16 L<Mojolicious::Plugin::Pingen> is a plugin for the L<Mojolicious> web
17 framework which allows you to do communicate with pingen.com.
18  
19 This module is EXPERIMENTAL. The API can change at any time. Let me know
20 if you are using it.
21  
22 =head1 SYNOPSIS
23  
24 =head2 Production mode
25  
26 use Mojolicious::Lite;
27 plugin Pingen => { apikey => $ENV{SUPER_SECRET_PINGEN_KEY} };
28  
29 post '/send' => sub {
30 my $c = shift;
31 $c->delay(
32 sub { $c->pingen->document->upload($c->param('pdf'), shift->begin) },
33 sub {
34 my ($delay, $res) = @_;
35 return $c->reply->exception($res->{errormessage}) if $res->{error};
36 $c->pingen->document->send($res->{id},{
37 speed => 1
38 },$delay->begin)
39 },
40 sub {
41 my ($delay, $res) = @_;
42 return $c->reply->exception($err) if $err;
43 return $c->render(text => "Delivery of $res->{id} is scheduled!");
44 }
45 );
46 );
47  
48 =head2 Testing mode
49  
50 use Mojolicious::Lite;
51 plugin Pingen => { mocked => 1 };
52  
53 Setting C<mocked> will enable this plugin to work without an actual connection
54 to pingen.com. This is done by replicating the behavior of Pingen for those API we support. This is
55 especially useful when writing unit tests.
56  
57 The apikey for the mocked interface is: sk_test_super_secret_key
58  
59 The following routes will be added to your application to mimic Pidgen:
60  
61 =over
62  
63 =item * POST /mocked/pingen/document/upload
64  
65 =item * POST /mocked/pingen/document/send
66  
67 =item * POST /mocked/pingen/document/delete
68  
69 =item * POST /mocked/pingen/send/cancel
70  
71 =back
72  
73 =cut
74  
75  
76 =head1 ATTRIBUTES
77  
78 =head2 base_url
79  
80 $str = $self->base_url;
81  
82 This is the location to Stripe payment solution. Will be set to
83 L<https://api.pingen.com/>.
84  
85 =head2 apikey
86  
87 $str = $self->apikey;
88  
89 The value for the private API key. Available in the Stripe admin gui.
90  
91 =head2 exceptions
92  
93 This will cause exceptions to be thrown if there is any problem
94 with submitting the invoice to pingen.
95  
96 =cut
97  
98 3 has base_url => 'https://api.pingen.com/';
99 3 has apikey => 'sk_test_super_secret_key';
100 3 has exceptions => 0;
101 3 ▶has _ua => sub { Mojo::UserAgent->new; };
Subroutine __ANON__: called CC=1 SCAR=0.0
102  
103 =head1 HELPERS
104  
105 =head2 pingen.document.upload
106  
107 my $json = $c->pingen->document->upload($asset[,\%args]);
108 $c->pingen->document->upload($mojoUpload[,\%args], sub { my ($c, $json) = @_; });
109  
110 Upload a L<Mojo::Upload> object containint a (PDF file). Pingen will analyze the content of the pdf and figure out
111 how and where to mail it.
112  
113 C<$json> is the response object from pingen.
114  
115 C<%args> can contain the following optional parameters:
116  
117 =over
118  
119 =item * color
120  
121 Print color: 0 = B/W, 1 = Color, 2 = Mixed (optimized)
122  
123 =item * duplex
124  
125 Paper hadling: 0 = simplex, 1 = duplex
126  
127 =item * rightaddress
128  
129 Where in the document is the address: 0 = Address left, 1 = Address right.
130  
131 =back
132  
133 =head2 pingen.document.send
134  
135 my $json = $c->pingen->document->send($id,\%args);
136 $c->pingen->document->send($id,\%args, sub { my ($c, $json) = @_; });
137  
138 Use the C<$id> returned from the upload call to actually send the document.
139  
140 C<$json> is a response object from pingen.
141  
142 C<%args> can contain the following parameters:
143  
144 =over
145  
146 =item * speed
147  
148 Delivery Speed. Varies by country. Use L<https://pingen.com/en/developer/testapi-send.html> to get a list
149 of speeds for your country. In general 1 = Priority, 2 = Economy.
150  
151 =item * envelope
152  
153 If you have designed your own envelope in the pingen webinterface, hunt take the html inspector to
154 the source code of the admin ui to determine the id of your envelope. You can then refer to it using that id.
155  
156 =back
157  
158 =head2 pingen.document.delete
159  
160 my $json = $c->pingen->document->delete($id);
161 $c->pingen->document->delete($id, sub { my ($c, $json) = @_; });
162  
163 Use the C<$id> returned from upload call to delete the document. Note that this will not
164 cancel any pending send tasks.
165  
166 C<$json> is a response object from pingen.
167  
168 =head2 pingen.send.cancel
169  
170 my $json = $c->pingen->send->cancel($id);
171 $c->pingen->send->cancel($id, sub { my ($c, $json) = @_; });
172  
173 cancel the given send task. Use the ID returned from the send call.
174  
175 C<$err> is a string describing the error. Will be empty string on success.
176 C<$json> is a response object from pingen.
177  
178 =head1 METHODS
179  
180 =head2 register
181  
182 $app->plugin(Pingen => \%config);
183  
184 Called when registering this plugin in the main L<Mojolicious> application.
185  
186 =cut
187  
188 3 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
189 3 my ($self, $app, $config) = @_;
190  
191 # copy config to this object
192 3 for (grep { $self->can($_) } keys %$config) {
193 3 $self->{$_} = $config->{$_};
194 }
195 # self contained
196 3 ▶ $self->_mock_interface($app, $config) if $config->{mocked};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"mocked"} 3 0 3
197  
198 3 ▶ $app->helper('pingen.document.upload' => sub { $self->_document_upload(@_); });
Subroutine __ANON__: called CC=1 SCAR=0.0
199 3 ▶ $app->helper('pingen.document.send' => sub { $self->_document_send(@_); });
Subroutine __ANON__: called CC=1 SCAR=0.0
200 3 ▶ $app->helper('pingen.document.delete' => sub { $self->_document_delete(@_); });
Subroutine __ANON__: called CC=1 SCAR=0.0
201 3 ▶ $app->helper('pingen.send.cancel' => sub { $self->_send_cancel(@_); });
Subroutine __ANON__: called CC=1 SCAR=0.0
202 }
203  
204 2 ▶sub _document_upload {
Subroutine _document_upload: called CC=7 SCAR=21.5
205 2 my ($self, $c, $upload, $args, $cb) = @_;
206 2 my $ua = $self->_ua;
207 2 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
02
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
208 2 ▶ if (ref $args eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "CODE") 0 2 2
209 0 $cb = $args;
210 0 $args = {};
211 }
212 2 my %form = ( send => 0 );
213 2 ▶ if (ref $args eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH") 2 0 2
214 2 for my $key (qw(color duplex rightaddress)){
215 6 ▶ $form{$key} = $args->{$key} if defined $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$key} 0 6 6
216 }
217 }
218  
219 2 my $URL = $self->base_url.'/document/upload/token/'.$self->apikey;
220 2 my $data = {
221 %form,
222 file => { file => $upload->asset, filename => $upload->filename },
223 };
224 2 ▶ if (ref $cb eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 0 2 2
225 0 $ua->post( $URL => form => $data => $self->_build_res_cb($cb));
226 }
227 else {
228 2 return $self->_tx_to_json($ua->post( $URL => form => $data));
229 }
230 }
231  
232 1 ▶sub _document_send {
Subroutine _document_send: called CC=7 SCAR=21.1
233 1 my ($self, $c, $id, $args, $cb) = @_;
234 1 my $ua = $self->_ua;
235  
236 1 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
10
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
237 1 ▶ if (ref $args eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "CODE") 0 1 1
238 0 $cb = $args;
239 0 $args = {};
240 }
241 1 my %data;
242 1 ▶ if (ref $args eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH") 1 0 1
243 1 for my $key (qw(speed envelope)){
244 2 ▶ $data{$key} = $args->{$key} if defined $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$key} 1 1 2
245 }
246 }
247  
248 1 my $URL = $self->base_url.'/document/send/id/'.$id.'/token/'.$self->apikey;
249  
250 1 ▶ if (ref $cb eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 0 1 1
251 0 $ua->post( $URL => json => \%data => $self->_build_res_cb($cb));
252 }
253 else {
254 1 return $self->_tx_to_json($ua->post( $URL => json => \%data));
255 }
256 }
257  
258 1 ▶sub _document_delete {
Subroutine _document_delete: called CC=2 SCAR=7.2
259 1 my ($self, $c, $id, $cb) = @_;
260 1 my $ua = $self->_ua;
261  
262 1 my $URL = $self->base_url.'/document/delete/id/'.$id.'/token/'.$self->apikey;
263  
264 1 ▶ if (ref $cb eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 0 1 1
265 0 $ua->post( $URL => $self->_build_res_cb($cb));
266 }
267 else {
268 1 return $self->_tx_to_json($ua->post( $URL ));
269 }
270 }
271  
272 1 ▶sub _send_cancel {
Subroutine _send_cancel: called CC=2 SCAR=7.2
273 1 my ($self, $c, $id, $cb) = @_;
274 1 my $ua = $self->_ua;
275  
276 1 my $URL = $self->base_url.'/send/cancel/id/'.$id.'/token/'.$self->apikey;
277  
278 1 ▶ if (ref $cb eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 0 1 1
279 0 $ua->post( $URL => $self->_build_res_cb($cb));
280 }
281 else {
282 1 return $self->_tx_to_json($ua->post( $URL ));
283 }
284 }
285  
286 5 ▶sub _tx_to_json {
Subroutine _tx_to_json: called CC=6 SCAR=18.7
287 5 my $self = shift;
288 5 my $tx = shift;
289 5 ▶ my $error = $tx->error || {};
Condition50%1/2
$tx->error || {}
l!l
05
MC/DC0%0/1
$tx->error || {}
$tx->error
Truth table50%1/2
A
A= $tx->error
Aresult
11
00
290 5 my $json;
291 5 ▶ if ($error->{code}){
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{'code'}) { } 0 5 5
292 $json = {
293 error => Mojo::JSON::true,
294 errormessage => $error->{message},
295 errorcode => $error->{code}
296 0 };
297 }
298 else {
299 5 ▶ $json = $tx->res->json || {};
Condition50%1/2
$tx->res->json || {}
l!l
50
MC/DC0%0/1
$tx->res->json || {}
$tx->res->json
Truth table50%1/2
A
A= $tx->res->json
Aresult
11
00
300 }
301 5 ▶ if ($self->exceptions and $json->{error}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->exceptions and $json->{"error"}) 1 4 5
Condition66%2/3
$self->exceptions and $json->{"error"}
!ll&&!rl&&r
041
MC/DC50%1/2
$self->exceptions and $json->{"error"}
$self->exceptions $json->{"error"}
Truth table66%2/3
A and B
A= $self->exceptionsB= $json->{"error"}
ABresult
0X0
100
111
302 Mojo::Exception->throw($json->{errormessage})
303 1 }
304 4 return $json;
305 }
306  
307 0 ▶sub _build_res_cb {
Subroutine _build_res_cb: not called CC=1 SCAR=6.9
308 0 my $self = shift;
309 0 my $cb = shift;
310 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
311 0 my ($c,$tx) = @_;
312 0 $c->$cb($self->_tx_to_json($tx));
313 }
314 0 }
315  
316 3 ▶sub _mock_interface {
Subroutine _mock_interface: called CC=1 SCAR=0.2
317 3 my ($self, $app) = @_;
318 3 my $apikey = 'sk_test_super_secret_key';
319  
320 3 $self->_ua->server->app($app);
321 3 $self->base_url('/mocked/pingen');
322 3 push @{$app->renderer->classes}, __PACKAGE__;
323  
324 2 ▶ $app->routes->post( '/mocked/pingen/document/upload/token/:apikey' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
325 2 my $c = shift;
326 2 ▶ if ($c->stash('apikey') eq $apikey){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash('apikey') eq $apikey) { } 1 1 2
327 1 ▶ $c->render(json => {
Condition50%1/2
$c->param("rightaddress") // 0
l!l
01
MC/DC0%0/1
$c->param("rightaddress") // 0
$c->param("rightaddress")
Truth table50%1/2
A
A= $c->param("rightaddress")
Aresult
11
00
328 'error' => Mojo::JSON::false,
329 'item' => {
330 'sent' => 0,
331 'size' => $c->param('file')->size,
332 'pages' => 1,
333 'fileremoved' => 0,
334 'requirement_failure' => 0,
335 'pagetype' => [
336 {
337 'type' => 2,
338 'number' => 1,
339 'color' => 0
340 }
341 ],
342 'filename' => $c->param('file')->filename,
343 'date' => strftime("%Y-%m-%d %H:%M:%S",gmtime(time)),
344 'rightaddress' => $c->param('rightaddress') // 0,
345 'status' => 1,
346 'country' => 'CH',
347 'user_id' => 902706832,
348 'id' => 253193787,
349 'address' => "Tobias Oetiker\nAarweg 15\n4600 Olten\nSchweiz",
350 },
351 'id' => 253193787
352 });
353 }
354 else {
355 1 $c->render(json => {
356 error => Mojo::JSON::true,
357 errormessage => 'Your token is invalid or expired',
358 errorcode => 99400
359 });
360 }
361 3 });
362 1 ▶ $app->routes->post( '/mocked/pingen/document/send/id/:id/token/:apikey' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
363 1 my $c = shift;
364 1 ▶ if ($c->stash('apikey') eq $apikey){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash('apikey') eq $apikey) { } 1 0 1
365 1 ▶ if ($c->stash('id') == 253193787){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash('id') == 253193787) { } 1 0 1
366 1 $c->render(json => {
367 error => Mojo::JSON::false,
368 id => 21830180,
369 });
370 }
371 else {
372 0 $c->render(json => {
373 error => Mojo::JSON::true,
374 errorcode => 15016,
375 errormessage => 'You do not have rights to access this object'
376 });
377 }
378 }
379 else {
380 0 $c->render(json => {
381 error => Mojo::JSON::true,
382 errormessage => 'Your token is invalid or expired',
383 errorcode => 99400
384 });
385 }
386 3 });
387 1 ▶ $app->routes->post( '/mocked/pingen/document/delete/id/:id/token/:apikey' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
388 1 my $c = shift;
389 1 ▶ if ($c->stash('apikey') eq $apikey){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash('apikey') eq $apikey) { } 1 0 1
390 1 ▶ if ($c->stash('id') == 253193787){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash('id') == 253193787) { } 1 0 1
391 1 $c->render(json => {
392 error => Mojo::JSON::false,
393 });
394 }
395 else {
396 0 $c->render(json => {
397 error => Mojo::JSON::true,
398 errorcode => 15016,
399 errormessage => 'You do not have rights to access this object'
400 });
401 }
402 }
403 else {
404 0 $c->render(json => {
405 error => Mojo::JSON::true,
406 errormessage => 'Your token is invalid or expired',
407 errorcode => 99400
408 });
409 }
410 3 });
411 1 ▶ $app->routes->post( '/mocked/pingen/send/cancel/id/:id/token/:apikey' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
412 1 my $c = shift;
413 1 ▶ if ($c->stash('apikey') eq $apikey){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash('apikey') eq $apikey) { } 1 0 1
414 1 ▶ if ($c->stash('id') == 21830180){
Branch: 1 branch
BranchTrueFalseTotal
if ($c->stash('id') == 21830180) { } 1 0 1
415 1 $c->render(json => {
416 error => Mojo::JSON::false,
417 });
418 }
419 else {
420 0 $c->render(json => {
421 error => Mojo::JSON::true,
422 errorcode => 15016,
423 errormessage => 'You do not have rights to access this object'
424 });
425 }
426 }
427 else {
428 0 $c->render(json => {
429 error => Mojo::JSON::true,
430 errormessage => 'Your token is invalid or expired',
431 errorcode => 99400
432 });
433 }
434 3 });
435 }
436 3 1;
437  
438 __END__
↑ Summary