blib/lib/Cache/Reddit.pm

filter: Statementstmt 42.2% 19 / 45 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 40.0% 4 / 10 Podpod 50.0% 3 / 6 totaltotal 29.2% 26 / 89 SCAR 41.9
CC
12
Cov
28%
authenticate
30.0
get
30.0
set
12.0
CRAP 65.9
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Cache::Reddit;
4 1 $Cache::Reddit::VERSION = '0.03';
5 #ABSTRACT: a caching API that uses Reddit as the backend
6  
7 1 require Exporter;
8 1 our @ISA = qw/Exporter/;
9 1 our @EXPORT = qw/get set remove/;
10 1 ▶use Reddit::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 1 my $session_file = '~/.reddit';
14 1 my $reddit = Reddit::Client->new(
15 session_file => $session_file,
16 user_agent => 'Cache::Reddit/0.1',
17 );
18  
19 1 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 0 ▶sub serialize { Storable::nfreeze($_[0]) }
Subroutine serialize: not called CC=1 SCAR=6.9
Pod undocumented
22  
23 0 ▶sub deserialize { Storable::thaw($_[0]) }
Subroutine deserialize: not called CC=1 SCAR=6.9
Pod undocumented
24  
25 sub authenticate
26 {
27 die 'Cache::Reddit requires the following environment variables: reddit_username, reddit_password and reddit_subreddit'
28 0 ▶ unless $ENV{reddit_username} && $ENV{reddit_password} && $ENV{reddit_subreddit};
Subroutine authenticate: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"reddit_username"} and $ENV{"reddit_password"} and $ENV{"reddit_subreddit"} 0 0 0
Condition0%0/6
$ENV{"reddit_username"} and $ENV{"reddit_password"} and $ENV{"reddit_subreddit"}
!ll&&!rl&&r
000
$ENV{"reddit_username"} and $ENV{"reddit_password"}
!ll&&!rl&&r
000
MC/DC0%0/3
$ENV{"reddit_username"} and $ENV{"reddit_password"} and $ENV{"reddit_subreddit"}
$ENV{"reddit_username"} $ENV{"reddit_password"} $ENV{"reddit_subreddit"}
Truth table0%0/4
A and B and C
A= $ENV{"reddit_username"}B= $ENV{"reddit_password"}C= $ENV{"reddit_subreddit"}
ABCresult
0XX0
10X0
1100
1111
29 0 ▶ unless ($reddit->is_logged_in) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reddit->is_logged_in) 0 0 0
30 0 $reddit->login($ENV{reddit_username}, $ENV{reddit_password});
31 0 $reddit->save_session();
32 }
33 }
34  
35  
36 sub set
37 {
38 0 ▶ my ($value) = @_;
Subroutine set: not called CC=3 SCAR=24.8
Pod documented
39  
40 0 ▶ die 'set() only accepts references' unless $value && ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value and ref $value 0 0 0
Condition0%0/3
$value and ref $value
!ll&&!rl&&r
000
MC/DC0%0/2
$value and ref $value
$value ref $value
Truth table0%0/3
A and B
A= $valueB= ref $value
ABresult
0X0
100
111
41  
42 0 authenticate();
43  
44 0 my $data = serialize($value);
45  
46 $reddit->submit_text(
47 subreddit => $ENV{reddit_subreddit},
48 0 title => 'Cache::Reddit::' . (int rand 100000),
49 text => $data,
50 );
51 }
52  
53  
54 sub get
55 {
56 0 ▶ my $key = shift;
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
57  
58 0 ▶ die 'get() requires a key argument' unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
59  
60 0 authenticate();
61  
62 0 my $data;
63  
64 0 for my $link ( @{$reddit->fetch_links(subreddit => $ENV{reddit_subreddit})->{items}} )
65 {
66 0 ▶ if ($link->{name} eq $key)
Branch: 1 branch
BranchTrueFalseTotal
if ($link->{"name"} eq $key) 0 0 0
67 {
68 0 $data = $link->{selftext};
69 0 last;
70 }
71 }
72 0 ▶ deserialize($data) if $data;
Branch: 1 branch
BranchTrueFalseTotal
if $data 0 0 0
73 }
74  
75  
76 sub remove
77 {
78 0 ▶ my $key = shift;
Subroutine remove: not called CC=2 SCAR=17.9
Pod documented
79 0 ▶ die 'remove() requires a key argument' unless $key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
80  
81 0 authenticate();
82  
83 0 $reddit->delete_item(name => $key);
84 0 1;
85 }
86  
87 1 1;
88  
89 __END__
90  
91 =pod
92  
93 =encoding UTF-8
94  
95 =head1 NAME
96  
97 Cache::Reddit - a caching API that uses Reddit as the backend
98  
99 =head1 VERSION
100  
101 version 0.03
102  
103 =head1 SYNOPSIS
104  
105 use Cache::Reddit;
106  
107 my $id = set($data_ref); # serialize data and post to subreddit
108 my $data = get($id); # retrieve the data back
109 remove($id); # delete the data from Reddit
110  
111 =head1 DESCRIPTION
112  
113 Cache::Reddit is a module for cacheing your application data on Reddit.
114 Data is serialized using L<Storable> and posted to a subreddit. The data
115 is posted as a text post, and the title of the post set to Cache::Reddit::
116 + a random number.
117  
118 Due to the list-like search function, data retrieval performs at 0(n). However
119 deletion and insertion performs at 0(1).
120  
121 It requires three environment variables to be set:
122  
123 =over 4
124  
125 =item * reddit_username - the reddit username to login with
126  
127 =item * reddit_password - the reddit password to login with
128  
129 =item * reddit_subreddit - the target subreddit to post data to
130  
131 =back
132  
133 =head1 EXPORTED FUNCTIONS
134  
135 =head2 set($dataref)
136  
137 Serializes and saves the data in the subreddit as a text post.
138 Returns the key for cached entry;
139  
140 =head2 get ($key)
141  
142 Deserializes and returns the cached entry.
143  
144 =head2 remove ($key)
145  
146 Deletes the item from the subreddit.
147  
148 =head1 AUTHOR
149  
150 David Farrell <dfarrell@cpan.org>
151  
152 =head1 COPYRIGHT AND LICENSE
153  
154 This software is Copyright (c) 2015 by David Farrell.
155  
156 This is free software, licensed under:
157  
158 The (two-clause) FreeBSD License
159  
160 =cut
↑ Summary