File Coverage

blib/lib/HTTP/Request/JSON.pm
Criterion Covered Total %
statement 35 35 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package HTTP::Request::JSON;
2              
3 5     5   35153 use strict;
  5         9  
  5         117  
4 5     5   25 use warnings;
  5         9  
  5         124  
5 5     5   22 no warnings 'uninitialized';
  5         16  
  5         177  
6              
7 5     5   1833 use parent 'HTTP::Message::JSON', 'HTTP::Request';
  5         1145  
  5         28  
8              
9             our $VERSION = $LWP::JSON::Tiny::VERSION;
10              
11 5     5   74307 use Encode ();
  5         38526  
  5         99  
12 5     5   1425 use LWP::JSON::Tiny;
  5         14  
  5         108  
13 5     5   429 use JSON::MaybeXS ();
  5         4207  
  5         740  
14              
15             =head1 NAME
16              
17             HTTP::Request::JSON - a subclass of HTTP::Request that understands JSON
18              
19             =head1 SYNOPSIS
20              
21             my $request = HTTP::Request::JSON->new(PATCH => "$base_url/death_ray");
22             # $request has an Accept header saying it's OK to send JSON back
23             $request->json_content(
24             {
25             self_destruct_mechanism => 'disabled',
26             users_allowed_to_override => [],
27             }
28             );
29             # Request content is JSON-encoded, and the content-type is set.
30              
31             =head1 DESCRIPTION
32              
33             This is a simple subclass of HTTP::Request::JSON that does two things.
34             First of all, it sets the Accept header to C as soon
35             as it's created. Secondly, it implements a L
36             method that adds the supplied data structure to the request, as JSON,
37             or returns the current JSON contents as a Perl structure.
38              
39             =head2 new
40              
41             In: ...
42             Out: $request
43              
44             As HTTP::Request->new, but also sets the Accept header.
45              
46             =cut
47              
48             sub new {
49 13     13 1 5341 my $class = shift;
50 13         70 my $self = $class->SUPER::new(@_);
51 13         700 $self->header('Accept' => 'application/json');
52 13         768 return $self;
53             }
54              
55             =head2 json_content
56              
57             In: $perl_data (optional)
58             Out: $converted_content
59              
60             A mutator for the request's JSON contents.
61              
62             As a setter, supplied with a valid JSON data structure, sets the request
63             contents to be the JSON-encoded version of that data structure, and sets the
64             Content-Type header to C. Will throw an exception if the
65             data structure cannot be converted to JSON. Returns the resulting string
66             contents.
67              
68             All strings in $perl_data must be Unicode strings, or you will get
69             encoding errors.
70              
71             As a getter, decodes the request contents from JSON
72             into a Perl structure, and returns the resulting data structure.
73              
74             =cut
75              
76             sub json_content {
77 29     29 1 8952 my $self = shift;
78              
79 29         86 my $json = $self->json_object;
80              
81             # Setter
82 29 100       71 if (@_) {
83 27         227 $self->content(Encode::encode('UTF8', $json->encode(shift)));
84 26         1605 $self->content_type('application/json');
85 26         607 return $self->decoded_content;
86             }
87              
88             # Getter
89 2         7 my $perl_data = $json->decode($self->decoded_content);
90 2         371 return $perl_data;
91             }
92              
93             =head2 json_object
94              
95             Out: $json_object
96              
97             Returns an object that knows how to handle the C and C
98             methods. By default whatever LWP::JSON::Tiny->json_object returns.
99             This is what you'd subclass if you wanted to use some other kind of JSON
100             object instead.
101              
102             =cut
103              
104             sub json_object {
105 20     20 1 41 my ($self) = @_;
106              
107 20         82 return LWP::JSON::Tiny->json_object;
108             }
109              
110             =head1 AUTHOR
111              
112             Sam Kington
113              
114             The source code for this module is hosted on GitHub
115             L - this is probably the
116             best place to look for suggestions and feedback.
117              
118             =head1 COPYRIGHT
119              
120             Copyright (c) 2015 Sam Kington.
121              
122             =head1 LICENSE
123              
124             This library is free software and may be distributed under the same terms as
125             perl itself.
126              
127             =cut
128              
129             1;