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

filter: Statementstmt 76.9% 30 / 39 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.8% 9 / 11 Podpod 100% 2 / 2 totaltotal 73.2% 41 / 56 CC 3 SCAR 9.3
CC
3
Cov
70%
_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.03
10  
11 =cut
12  
13 1 our $VERSION = '0.03';
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 http_caching => {
65 cache => _chi_cache(),
66 }
67 );
68
69 2 return $self
70 }
71  
72 {
73 1 my $ua;
74 sub _default_useragent {
75 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
76 0 return $ua
77 }
78 }
79  
80 1 sub get_from_json {
81 0 ▶ my $rqst = HTTP::Request->new(
Subroutine get_from_json: not called CC=2 SCAR=17.9
Pod documented
82 GET => $_[0],
83 [ Accept => 'application/json' ]
84 );
85 0 my $resp = _default_useragent()->request($rqst);
86 0 ▶ return from_json($resp->content()) if $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->is_success 0 0 0
87 0 warn "HTTP Status message ${\$resp->code} [${\$resp->message}] GET $_[0]\n";
88 return
89
90 0 }
91  
92 =head1 METHODS
93  
94 Since this is a subclass of L<LWP::UserAgent::Caching> 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<Accept> Header set
118 to C<application/json>. On succes, it will turn the returned json (as requested)
119 into a perl data structure. Otherwise it will be C<undef> 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<HTTP::Response> object back. The UserAgent is a full
131 subclass of the standard L<LWP::UserAgent>, 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<LWP::UserAgent::Caching>
135  
136 =head1 AUTHOR
137  
138 Theo van Hoesel, C<< <Th.J.v.Hoesel at THEMA-MEDIA.nl> >>
139  
140 =head1 LICENSE AND COPYRIGHT
141  
142 Copyright 2016 Theo van Hoesel.
143  
144 =cut
145  
146 1 1;
↑ Summary