File Coverage

blib/lib/Eixo/Rest/Request.pm
Criterion Covered Total %
statement 28 38 73.6
branch 4 8 50.0
condition 1 2 50.0
subroutine 8 12 66.6
pod 0 7 0.0
total 41 67 61.1


line stmt bran cond sub pod time code
1             package Eixo::Rest::Request;
2              
3 3     3   1903 use strict;
  3         6  
  3         114  
4 3     3   16 use Eixo::Base::Clase;
  3         5  
  3         34  
5              
6 3     3   314 use Attribute::Handlers;
  3         7  
  3         27  
7 3     3   140 use Carp;
  3         5  
  3         1301  
8              
9             has (
10             callback=>undef,
11              
12             onProgress => undef,
13             onSuccess => undef,
14             onError => undef,
15             onStart => undef,
16            
17             __format=>'json',
18              
19             buffer=>'',
20              
21             );
22              
23             sub start{
24 7     7 0 12 my ($self) = @_;
25              
26 7         15 $self->{buffer} = ''; # truncate the buffer
27              
28 7 50       33 if($self->onStart){
29 0         0 $self->onStart->();
30             }
31             }
32              
33             sub end{
34            
35 7     7 0 20 my ($self, $response) = @_;
36              
37 7         48 my $content = $self->unmarshall($response);
38            
39 7         82 &{$self->onSuccess}(
  7         6708  
40            
41             $self->callback->($content, $self),
42              
43             $content
44              
45             );
46              
47             }
48              
49             sub error{
50 0     0 0 0 my ($self, $response) = @_;
51              
52 0         0 &{$self->onError}($response);
  0         0  
53             # $response->code,
54             # $response->content,
55             # );
56              
57             }
58              
59             sub progress{
60 0     0 0 0 my ($self, $chunk, $req) = @_;
61              
62 0         0 $self->buffer($self->buffer . $chunk);
63              
64 0 0       0 $self->onProgress->($chunk, $req) if($self->onProgress);
65             }
66              
67 0     0 0 0 sub process {die ref($_[0]) . "::process: MUST BE DEFINED"}
68              
69 0     0 0 0 sub send {die ref($_[0]) . "::send: MUST BE DEFINED"}
70              
71             sub unmarshall{
72 7     7 0 18 my ($self, $response) = @_;
73              
74 7         64 my $content = $response->decoded_content(
75             default_charset=> 'UTF-8'
76             );
77              
78             # nowadays (HTTP::Message v6.11)
79             # decoded_content isn't decoding utf8 charset
80             # if content_type is application/json
81 7 50       15053 if($response->content_type eq 'application/json'){
82 3     3   2580 use Encode;
  3         35552  
  3         683  
83 0         0 $content = Encode::decode('UTF-8', $content);
84             }
85              
86 7 100       271 if($self->__format eq 'json'){
87              
88 5   50     223 return JSON->new->decode($content || '{}')
89             }
90             else{
91 2         30 return $content;
92             }
93             }
94              
95              
96              
97             1;