File Coverage

blib/lib/Net/HTTP/Spore/Middleware/Format.pm
Criterion Covered Total %
statement 25 30 83.3
branch 7 12 58.3
condition 1 3 33.3
subroutine 6 10 60.0
pod 7 7 100.0
total 46 62 74.1


line stmt bran cond sub pod time code
1             package Net::HTTP::Spore::Middleware::Format;
2             $Net::HTTP::Spore::Middleware::Format::VERSION = '0.07';
3             # ABSTRACT: base class for formats middlewares
4              
5 5     5   2657 use Moose;
  5         128  
  5         38  
6             extends 'Net::HTTP::Spore::Middleware';
7              
8             has serializer_key =>
9             ( is => 'ro', isa => 'Str', lazy => 1, default => 'sporex.serialization' );
10             has deserializer_key =>
11             ( is => 'ro', isa => 'Str', lazy => 1, default => 'sporex.deserialization' );
12              
13 0     0 1 0 sub encode { die "must be implemented" }
14 0     0 1 0 sub decode { die "must be implemented" }
15 0     0 1 0 sub accept_type { die "must be implemented" }
16 0     0 1 0 sub content_type { die "must be implemented" }
17              
18             sub should_serialize {
19 5     5 1 11 my $self = shift;
20 5         173 $self->_check_serializer( shift, $self->serializer_key );
21             }
22              
23             sub should_deserialize {
24 5     5 1 13 my $self = shift;
25 5         175 $self->_check_serializer( shift, $self->deserializer_key );
26             }
27              
28             sub _check_serializer {
29 10     10   27 my ( $self, $env, $key ) = @_;
30 10 50 33     51 if ( exists $env->{$key} && $env->{$key} == 1 ) {
31 0         0 return 0;
32             }
33             else {
34 10         36 return 1;
35             }
36             }
37              
38             sub call {
39 5     5 1 16 my ( $self, $req ) = @_;
40              
41 5 50       158 return unless $self->should_serialize( $req->env );
42              
43 5         29 $req->header( $self->accept_type );
44              
45 5 100       372 if ( $req->env->{'spore.payload'} ) {
46             $req->env->{'spore.payload'} =
47 1         23 $self->encode( $req->env->{'spore.payload'} );
48 1         5 $req->header( $self->content_type );
49             }
50              
51 5         184 $req->env->{ $self->serializer_key } = 1;
52              
53             return $self->response_cb(
54             sub {
55 5     5   11 my $res = shift;
56 5 50       17 if ( $res->body ) {
57 5 50       27 return if $res->code >= 500;
58 5 50       21 return unless $self->should_deserialize( $res->env );
59 5         19 my $content = $self->decode( $res->body );
60 5         45599 $res->body($content);
61 5         20 $res->env->{ $self->deserializer_key } = 1;
62             }
63             }
64 5         60 );
65             }
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             Net::HTTP::Spore::Middleware::Format - base class for formats middlewares
78              
79             =head1 VERSION
80              
81             version 0.07
82              
83             =head1 SYNOPSIS
84              
85             my $client = Net::HTTP::Spore->new_from_spec('twitter.json');
86             $client->enable('Format::JSON');
87              
88             my $res = $client->public_timeline();
89             # $res->body contains an hashref build from the JSON returned by the API
90              
91             =head1 DESCRPITION
92              
93             This middleware is a base class for others format's middleware. Thoses middlewares must set the appropriate B<Content-Type> and B<Accept> header to the request.
94              
95             If the environment contains a B<payload> (under the name 'spore.payload'), it should also serialize this data to the appropriate format (eg: if payload contains an hashref, and the format is json, the hashref B<MUST> be serialized to JSON).
96              
97             =head1 METHODS
98              
99             =over 4
100              
101             =item serializer_key
102              
103             name of the extension serializer should check to be sure to not encode a payload already encoded, or set the headers that have already been defined
104              
105             =item deserializer_key
106              
107             as previously, but for the response instead of the request
108              
109             =item encode
110              
111             this method B<MUST> be implemented in class extending this one. This method B<MUST> return an encoded string from the argument passed.
112              
113             =item decode
114              
115             this method B<MUST> be implemented in class extending this one. This method B<MUST> return a reference from the undecoded string passed as argument.
116              
117             =item accept_type
118              
119             this method B<MUST> be implemented in class extending this one. This method B<MUST> return a string that will be used as the B<Accept> HTTP header.
120              
121             =item content_type
122              
123             this method B<MUST> be implemented in class extending this one. This method B<MUST> return a string that will be used as the B<Content-Type> HTTP header.
124              
125             =item should_serialize
126              
127             this method returns 1 if serialization have not already been done
128              
129             =item should_deserialize
130              
131             this method returns 1 if deserialization have not already been done
132              
133             =item call
134              
135             =back
136              
137             =head1 AUTHORS
138              
139             =over 4
140              
141             =item *
142              
143             Franck Cuny <franck.cuny@gmail.com>
144              
145             =item *
146              
147             Ash Berlin <ash@cpan.org>
148              
149             =item *
150              
151             Ahmad Fatoum <athreef@cpan.org>
152              
153             =back
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2012 by Linkfluence.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =cut