File Coverage

blib/lib/Mojo/Snoo/Link.pm
Criterion Covered Total %
statement 13 43 30.2
branch 1 14 7.1
condition 0 3 0.0
subroutine 5 13 38.4
pod 1 7 14.2
total 20 80 25.0


line stmt bran cond sub pod time code
1             package Mojo::Snoo::Link;
2 5     5   24 use Moo;
  5         9  
  5         33  
3              
4             extends 'Mojo::Snoo::Base';
5              
6 5     5   1384 use Mojo::Collection;
  5         11  
  5         205  
7 5     5   2560 use Mojo::Snoo::Comment;
  5         16  
  5         182  
8              
9 5     5   39 use constant FIELD => 'name';
  5         10  
  5         3020  
10              
11             has [
12             qw(
13             approved_by
14             archived
15             author
16             author_flair_css_class
17             author_flair_text
18             banned_by
19             clicked
20             created
21             created_utc
22             distinguished
23             domain
24             downs
25             edited
26             from
27             from_id
28             from_kind
29             gilded
30             hidden
31             id
32             is_self
33             likes
34             link_flair_css_class
35             link_flair_text
36             media
37             media_embed
38             mod_reports
39             name
40             num_comments
41             num_reports
42             over_18
43             permalink
44             removal_reason
45             report_reasons
46             saved
47             score
48             secure_media
49             secure_media_embed
50             selftext
51             selftext_html
52             stickied
53             subreddit
54             subreddit_id
55             suggested_sort
56             thumbnail
57             title
58             ups
59             url
60             user_reports
61             visited
62             )
63             ] => (is => 'ro', predicate => 1);
64              
65 3 50   3 0 10206 sub BUILDARGS { shift->SUPER::BUILDARGS(@_ == 1 ? (id => shift) : @_) }
66              
67             sub _get_comments {
68 0     0     my $cb; # callback optional
69             my ($self, $limit, $sort, $time) = map { #
70 0 0 0       ref($_) eq 'CODE' && ($cb = $_) ? () : $_;
  0            
71             } @_;
72              
73 0           my $path = '/comments/' . $self->id;
74              
75 0           my %params;
76 0 0         $params{sort} = $sort if $sort;
77 0 0         $params{t} = $time if $time;
78 0 0         $params{limit} = $limit if $limit;
79              
80 0           my $res = $self->_do_request('GET', $path, %params);
81 0 0         $res->$cb if $cb;
82              
83             my @children =
84 0 0         map { $_->{kind} eq 't1' ? $_->{data} : () }
85 0           map { @{$_->{data}{children}} } @{$res->json};
  0            
  0            
  0            
86              
87 0           my %args = map { $_ => $self->$_ } (
  0            
88             qw(
89             username
90             password
91             client_id
92             client_secret
93             )
94             );
95              
96 0           Mojo::Collection->new(map { Mojo::Snoo::Comment->new(%args, %$_) } @children);
  0            
97             }
98              
99             sub _vote {
100 0     0     my ($self, $direction) = @_;
101              
102 0           my %params = (
103             dir => $direction,
104             id => $self->name,
105             );
106              
107 0           $self->_do_request('POST', '/api/vote', %params);
108             }
109              
110             # defaults to comments_hot?
111             # TODO pass params:
112             # http://www.reddit.com/dev/api#GET_comments_{article}
113 0     0 1   sub comments { shift->_get_comments(@_) }
114              
115 0     0 0   sub upvote { shift->_vote(1) }
116 0     0 0   sub downvote { shift->_vote(-1) }
117 0     0 0   sub unvote { shift->_vote(0) }
118              
119             # TODO support category (gold accounts only)
120             sub save {
121 0     0 0   my $self = shift;
122 0           $self->_do_request('POST', '/api/save', id => $self->name);
123             }
124              
125             # TODO support category (gold accounts only)
126             sub unsave {
127 0     0 0   my $self = shift;
128 0           $self->_do_request('POST', '/api/unsave', id => $self->name);
129             }
130              
131             1;
132              
133             __END__