Reddit.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 96 CC 7 SCAR 19.9
CC
7
Cov
0%
get
20.0
authenticate
6.0
set
6.0
CRAP 7.3
1 use strict;
2 use warnings;
3 package Cache::Reddit;
4 $Cache::Reddit::VERSION = '0.04';
5 use Cache::Reddit::Bleach;
6  
7 #ABSTRACT: a caching API that uses Reddit as the backend
8  
9 require Exporter;
10 our @ISA = qw/Exporter/;
11 our @EXPORT = qw/get set remove/;
12 use Reddit::Client;
13  
14 die 'Cache::Reddit requires the following environment variables: reddit_username, reddit_password and reddit_subreddit'
15 unless $ENV{reddit_username} && $ENV{reddit_password} && $ENV{reddit_subreddit};
16  
17 my $session_file = '~/.reddit';
18 my $reddit = Reddit::Client->new(
19 session_file => $session_file,
20 user_agent => 'Cache::Reddit/0.1',
21 );
22  
23 use Storable ();
24  
25 sub serialize { Storable::nfreeze($_[0]) }
26  
27 sub deserialize { Storable::thaw($_[0]) }
28  
29 sub authenticate
30 {
31 unless ($reddit->is_logged_in) {
32 $reddit->login($ENV{reddit_username}, $ENV{reddit_password});
33 $reddit->save_session();
34 }
35 }
36  
37  
38 sub set
39 {
40 my ($value) = @_;
41  
42 die 'set() only accepts references' unless $value && ref $value;
43  
44 authenticate();
45  
46 my $data = serialize($value);
47 my $bleached_data = Cache::Reddit::Bleach::wash($data);
48  
49 $reddit->submit_text(
50 subreddit => $ENV{reddit_subreddit},
51 title => 'Cache::Reddit::' . (int rand 100000),
52 text => $bleached_data,
53 );
54 }
55  
56  
57 sub get
58 {
59 my $key = shift;
60  
61 die 'get() requires a key argument' unless $key;
62  
63 authenticate();
64  
65 my $data;
66  
67 for my $link ( @{$reddit->fetch_links(subreddit => $ENV{reddit_subreddit})->{items}} )
68 {
69 if ($link->{name} eq $key)
70 {
71 $data = $link->{selftext};
72 last;
73 }
74 }
75 deserialize(Cache::Reddit::Bleach::dry($data)) if $data;
76 }
77  
78  
79 sub remove
80 {
81 my $key = shift;
82 die 'remove() requires a key argument' unless $key;
83  
84 authenticate();
85  
86 $reddit->delete_item(name => $key);
87 1;
88 }
89  
90 1;
91  
92 __END__