File Coverage

blib/lib/LWP/UserAgent/Caching/Simple.pm
Criterion Covered Total %
statement 22 30 73.3
branch 0 4 0.0
condition n/a
subroutine 8 10 80.0
pod 2 2 100.0
total 32 46 69.5


line stmt bran cond sub pod time code
1             package LWP::UserAgent::Caching::Simple;
2              
3             =head1 NAME
4              
5             LWP::UserAgent::Caching::Simple - The first 'hard thing' made easy --- simple
6              
7             =head1 VERSION
8              
9             Version 0.06
10              
11             =cut
12              
13             our $VERSION = '0.06';
14              
15 1     1   1970 use strict;
  1         2  
  1         23  
16 1     1   4 use warnings;
  1         2  
  1         21  
17              
18 1     1   366 use parent 'LWP::UserAgent::Caching';
  1         236  
  1         4  
19              
20 1     1   98418 use CHI;
  1         36962  
  1         29  
21 1     1   555 use JSON;
  1         6687  
  1         8  
22              
23 1     1   117 use parent 'Exporter';
  1         3  
  1         5  
24             our @EXPORT_OK = qw(get_from_json);
25              
26             =head1 SYNOPSIS
27              
28             use LWP::UserAgent::Caching::Simple;
29            
30             my $ua = LWP::UserAgent::Caching::Simple->new;
31            
32             my $resp = $ua->get( 'http://example.com/cached?' );
33              
34             and maybe even something quick:
35              
36             # use a built-in default User-Agent for quick one timers
37            
38             use LWP::UserAgent::Caching::Simple qw(get_from_json);
39            
40             my $hashref = get_from_json (
41             'http://example.com/cached?',
42             'Cache-Control' => 'max-stale', # without delta-seconds, unlimited
43             'Cache-Control' => 'no-transform', # something not implemented
44             );
45              
46              
47             =head1 DESCRIPTION
48              
49             This is a simplified version of L with sensible
50             defaults and less options. For more control and more options, please use that
51             module.
52              
53             =cut
54              
55             sub _chi_cache {
56 2     2   19 return CHI->new(
57             driver => 'File',
58             root_dir => '/tmp/LWP_UserAgent_Caching',
59             file_extension => '.cache',
60             )
61             }
62              
63             sub new {
64 2     2 1 5390 my ( $class) = @_;
65            
66 2         7 my $self = $class->SUPER::new(
67             http_caching => {
68             cache => _chi_cache(),
69             }
70             );
71            
72 2         93161 return $self
73             }
74              
75             {
76             my $ua;
77             sub _default_useragent {
78 0 0   0     $ua = __PACKAGE__->new() unless $ua;
79 0           return $ua
80             }
81             }
82              
83             sub get_from_json {
84 0     0 1   my $resp = _default_useragent()->get(@_, Accept => 'application/json');
85 0 0         return decode_json($resp->decoded_content()) if $resp->is_success;
86 0           warn "HTTP Status message ${\$resp->code} [${\$resp->message}] GET $_[0]\n";
  0            
  0            
87             return
88            
89 0           }
90              
91             =head1 METHODS
92              
93             Since this is a subclass of L it has it's methods, like
94             the following object methods:
95              
96             =over
97              
98             =item request
99              
100             =item get
101              
102             =item post
103              
104             =item put
105              
106             =item delete
107              
108             =back
109              
110             =head1 EXPORT_OK
111              
112             =head2 get_from_json
113              
114             This will simply make a GET request to a server, with the C Header set
115             to C. On succes, it will turn the returned json (as requested)
116             into a perl data structure. Otherwise it will be C and print a warning.
117              
118             =head1 CAVEATS
119              
120             This is a super simplified way of making a straightforward request. It can
121             handle more complex requests as well, using
122              
123             my $resp = $ua->request($http_rqst);
124              
125             which will give a full C object back. The UserAgent is a full
126             subclass of the standard L, and one can still change the setting
127             of that, like e.g. the C<< $ua->agent('SecretAgent/007') >>.
128              
129             =head1 AUTHOR
130              
131             Theo van Hoesel, C<< >>
132              
133             =head1 LICENSE AND COPYRIGHT
134              
135             Copyright 2016 .. 2018 Theo van Hoesel.
136              
137             =cut
138              
139             1;