File Coverage

blib/lib/LWP/UserAgent/Caching/Simple.pm
Criterion Covered Total %
statement 25 34 73.5
branch 0 4 0.0
condition n/a
subroutine 9 11 81.8
pod 2 2 100.0
total 36 51 70.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.03
10              
11             =cut
12              
13             our $VERSION = '0.03';
14              
15 1     1   1792 use strict;
  1         2  
  1         25  
16 1     1   3 use warnings;
  1         1  
  1         25  
17              
18 1     1   443 use parent 'LWP::UserAgent::Caching';
  1         227  
  1         5  
19 1     1   79826 use HTTP::Request;
  1         2  
  1         24  
20              
21 1     1   677 use CHI;
  1         35628  
  1         29  
22 1     1   6 use JSON;
  1         2  
  1         8  
23              
24 1     1   155 use parent 'Exporter';
  1         2  
  1         8  
25             our @EXPORT_OK = qw(get_from_json);
26              
27             =head1 SYNOPSIS
28              
29             use LWP::UserAgent::Caching::Simple;
30            
31             my $ua = LWP::UserAgent::Caching::Simple->new;
32            
33             my $resp = $ua->get( 'http://example.com/cached?' );
34              
35             and maybe even something quick:
36              
37             # use a built-in default User-Agent for quick one timers
38            
39             use LWP::UserAgent::Caching::Simple qw(get_from_json);
40            
41             my $hashref = get_from_json ( 'http://example.com/cached?' );
42              
43              
44             =head1 DESCRIPTION
45              
46             This is a simplified version of L with sensible
47             defaults and less options. For more control and more options, please use that
48             module.
49              
50             =cut
51              
52             sub _chi_cache {
53 2     2   9 return CHI->new(
54             driver => 'File',
55             root_dir => '/tmp/LWP_UserAgent_Caching',
56             file_extension => '.cache',
57             )
58             }
59              
60             sub new {
61 2     2 1 4120 my ( $class) = @_;
62            
63 2         5 my $self = $class->SUPER::new(
64             http_caching => {
65             cache => _chi_cache(),
66             }
67             );
68            
69 2         67134 return $self
70             }
71              
72             {
73             my $ua;
74             sub _default_useragent {
75 0 0   0     $ua = __PACKAGE__->new() unless $ua;
76 0           return $ua
77             }
78             }
79              
80             sub get_from_json {
81 0     0 1   my $rqst = HTTP::Request->new(
82             GET => $_[0],
83             [ Accept => 'application/json' ]
84             );
85 0           my $resp = _default_useragent()->request($rqst);
86 0 0         return from_json($resp->content()) if $resp->is_success;
87 0           warn "HTTP Status message ${\$resp->code} [${\$resp->message}] GET $_[0]\n";
  0            
  0            
88             return
89            
90 0           }
91              
92             =head1 METHODS
93              
94             Since this is a subclass of L it has it's methods, like
95             the following object methods:
96              
97             =over
98              
99             =item request
100              
101             =item get
102              
103             =item post
104              
105             =item put
106              
107             =item delete
108              
109             =back
110              
111             And to make life realy simpel, when imported, one function
112              
113             =over
114              
115             =item get_from_json
116              
117             this will simply make a GET request to a server, with the C Header set
118             to C. On succes, it will turn the returned json (as requested)
119             into a perl data structure. Otherwise it will be C and print a warning.
120              
121             =back
122              
123             =head1 CAVEATS
124              
125             This is a super simplified way of making straightforward request. It can handle
126             mnore complex requests as well, using
127              
128             my $resp = $ua->request($http_rqst);
129              
130             which will give a full C object back. The UserAgent is a full
131             subclass of the standard L, and one can still change the setting
132             of that, like e.g. the C<<$ua->agent('SecretAgent/007')>>. But if you need more
133             control over your cache, you definitly need to fall back to
134             L
135              
136             =head1 AUTHOR
137              
138             Theo van Hoesel, C<< >>
139              
140             =head1 LICENSE AND COPYRIGHT
141              
142             Copyright 2016 Theo van Hoesel.
143              
144             =cut
145              
146             1;