blib/lib/LWP/UserAgent/Caching/Simple.pm

filter: Statementstmt 83.3% 30 / 36 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 100% 2 / 2 totaltotal 77.3% 41 / 53 CC 3 SCAR 9.3
CC
3
Cov
75%
_default_useragent
6.0
get_from_json
6.0
BEGIN
1.0
CRAP 2.5
↑ Summary
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.02
10  
11 =cut
12  
13 1 our $VERSION = '0.02';
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use parent 'LWP::UserAgent::Caching';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 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<LWP::UserAgent::Caching> 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 ▶ return CHI->new(
Subroutine _chi_cache: called CC=1 SCAR=0.0
54 driver => 'File',
55 root_dir => '/tmp/LWP_UserAgent_Caching',
56 file_extension => '.cache',
57 )
58 }
59  
60 sub new {
61 2 ▶ my ( $class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
62
63 2 my $self = $class->SUPER::new(
64 cache => _chi_cache(),
65 );
66 # $self->agent('_ ' . __PACKAGE__ . '/' . $VERSION);
67 # $self->agent('_LWP');
68 2 return $self
69 }
70  
71 {
72 1 my $ua;
73 sub _default_useragent {
74 0 ▶ $ua = __PACKAGE__->new() unless $ua;
Subroutine _default_useragent: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $ua 0 0 0
75 0 return $ua
76 }
77 }
78  
79 1 sub get_from_json {
80 0 ▶ my $rqst = HTTP::Request->new(
Subroutine get_from_json: not called CC=2 SCAR=17.9
Pod documented
81 GET => $_[0],
82 [ Accept => 'application/json' ]
83 );
84 0 my $resp = _default_useragent()->request($rqst);
85 0 ▶ return unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
86 0 return from_json($resp->content());
87 }
88  
89 =head1 METHODS
90  
91 Since this is a subclass of L<LWP::UserAgent::Caching> it has it's methods, like
92 the following object methods:
93  
94 =over
95  
96 =item request
97  
98 =item get
99  
100 =item post
101  
102 =item put
103  
104 =item delete
105  
106 =back
107  
108 And to make life realy simpel, when imported, one function
109  
110 =over
111  
112 =item get_from_json
113  
114 this will simply make a GET request to a server, with the C<Accept> Header set
115 to C<application/json>. On succes, it will turn the returned json (as requested)
116 into a perl data structure. Otherwise it will be C<undef>
117  
118 =back
119  
120 =head1 CAVEATS
121  
122 This is a super simplified way of making straightforward request. It can handle
123 mnore complex requests as well, using
124  
125 my $resp = $ua->request($http_rqst);
126  
127 which will give a full C<HTTP::Response> object back. The UserAgent is a full
128 subclass of the standard L<LWP::UserAgent>, and one can still change the setting
129 of that, like e.g. the C<<$ua->agent('SecretAgent/007')>>. But if you need more
130 control over your cache, you definitly need to fall back to
131 L<LWP::UserAgent::Caching>
132  
133 =head1 AUTHOR
134  
135 Theo van Hoesel, C<< <Th.J.v.Hoesel at THEMA-MEDIA.nl> >>
136  
137 =head1 LICENSE AND COPYRIGHT
138  
139 Copyright 2016 Theo van Hoesel.
140  
141 =cut
142  
143 1 1;
↑ Summary