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.09';
3             # ABSTRACT: base class for formats middlewares
4              
5 6     6   3088 use Moose;
  6         76  
  6         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 7     7 1 25 my $self = shift;
20 7         283 $self->_check_serializer( shift, $self->serializer_key );
21             }
22              
23             sub should_deserialize {
24 7     7 1 19 my $self = shift;
25 7         279 $self->_check_serializer( shift, $self->deserializer_key );
26             }
27              
28             sub _check_serializer {
29 14     14   54 my ( $self, $env, $key ) = @_;
30 14 50 33     70 if ( exists $env->{$key} && $env->{$key} == 1 ) {
31 0         0 return 0;
32             }
33             else {
34 14         79 return 1;
35             }
36             }
37              
38             sub call {
39 7     7 1 28 my ( $self, $req ) = @_;
40              
41 7 50       268 return unless $self->should_serialize( $req->env );
42              
43 7         46 $req->header( $self->accept_type );
44              
45 7 100       622 if ( $req->env->{'spore.payload'} ) {
46             $req->env->{'spore.payload'} =
47 2         45 $self->encode( $req->env->{'spore.payload'} );
48 2         11 $req->header( $self->content_type );
49             }
50              
51 7         287 $req->env->{ $self->serializer_key } = 1;
52              
53             return $self->response_cb(
54             sub {
55 7     7   21 my $res = shift;
56 7 50       26 if ( $res->body ) {
57 7 50       31 return if $res->code >= 500;
58 7 50       36 return unless $self->should_deserialize( $res->env );
59 7         29 my $content = $self->decode( $res->body );
60 7         45394 $res->body($content);
61 7         27 $res->env->{ $self->deserializer_key } = 1;
62             }
63             }
64 7         86 );
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.09
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