File Coverage

lib/Image/Randim/Source/Role.pm
Criterion Covered Total %
statement 17 25 68.0
branch 0 2 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 24 35 68.5


line stmt bran cond sub pod time code
1             our $VERSION = '0.01';
2             use v5.20;
3 1     1   454 use warnings;
  1         3  
4 1     1   5 use Moose::Role;
  1         2  
  1         30  
5 1     1   378 use REST::Client;
  1         4342  
  1         4  
6 1     1   4725 use Image::Randim::Image;
  1         2  
  1         32  
7 1     1   7 use namespace::autoclean;
  1         1  
  1         20  
8 1     1   4  
  1         1  
  1         5  
9             requires 'url', 'name', 'get_image';
10              
11             has 'timeout' => ( is => 'rw',
12             isa => 'Int',
13             default => 20,
14             );
15              
16             my $self = shift;
17             my $client = REST::Client->new;
18 0     0 1   $client->setTimeout($self->timeout);
19 0           $client->GET($self->url);
20 0           my $rc = $client->responseCode;
21 0           if ($rc > 200) {
22 0           die 'Source '.$self->name
23 0 0         . " received a response code of $rc from "
24 0           . $self->url . "\n"
25             . $client->responseContent ."\n";
26             }
27             return $client->responseContent;
28             }
29 0            
30             1;
31              
32             =pod
33              
34             =head1 NAME
35              
36             Image::Randim::Source::Role - Source plugins must implement this role
37              
38             =head1 SYNOPSIS
39              
40             package Image::Randim::Source::Desktoppr
41             use Moose;
42              
43             has 'name' => ( is => 'ro',
44             isa => 'Str',
45             default => 'Desktoppr',
46             );
47             has 'url' => ( is => 'ro',
48             isa => 'Str',
49             default => 'https://api.desktoppr.co/1/wallpapers/random',
50             );
51              
52             with 'Image::Randim::Source::Role';
53              
54             sub get_image {
55             my $self = shift;
56             my $data = JSON->new->decode($self->get_response);
57             $data = $$data{'response'};
58              
59             my $image = Image::Randim::Image->new(
60             url => $$data{'image'}{'url'},
61             id => $$data{'id'},
62             width => $$data{'width'},
63             height => $$data{'height'},
64             link => $$data{'url'},
65             );
66              
67             if (exists $$data{'uploader'}) {
68             $image->owner($$data{'uploader'});
69             }
70              
71             return $image;
72             }
73              
74             =head1 DESCRIPTION
75              
76             To create a source "plugin" for this library, the plugin must be a
77             Moose class which implements this role and is named in the
78             Image::Randim::Source::* namespace.
79              
80             The class must provide "name", "url" and "get_image" methods or
81             attributes. "timeout" and "get_response are provided as a convience in
82             the role but may be overridden.
83              
84             =head1 ROLE INTERFACE
85              
86             =head2 C<name>
87              
88             Plugins must return a name, which is a string representing the source (such as "Desktoppr").
89              
90             This name must be the same as the name of module's end. For example,
91             in the case of the module "Image::Randim::Source::Desktoppr", the name
92             must be "Desktoppr".
93              
94             =head2 C<url>
95              
96             Plugins must return a URL, which is a string representing the link
97             that must be called on the provider's site to get the random image's
98             data.
99              
100             This is typically a API call that returns JSON.
101              
102             =head2 C<get_image>
103              
104             Plugins must return an Image::Randim::Image object, which is populated
105             with the information retrieved from the URL call to the provider.
106              
107             In the SYNOPSIS example, you see that JSON is returned from the
108             get_response() call to the provider's API (which uses the provided
109             url()) -- and then that JSON is parsed into a hash that is used to set
110             the image object attributes, and return it.
111              
112             =head2 C<timeout($integer)>
113              
114             How many seconds to wait for a response from the provider's site. This
115             value is up to the individual plugins to honor, but the provided
116             convenience method "get_response" honors it.
117              
118             =head1 CONVENIENCE METHODS
119              
120             =head2 C<get_response>
121              
122             Convenience method that will call the given "url" with a GET, and
123             expect a JSON response within the "timeout" time period.
124              
125             Whatever is returned can then be used to create that
126             Image::Randim::Image object. Usually what is returned in JSON -- but
127             your "get_image" method should handle this reponse and populate the
128             image object accordingly.
129              
130             =head1 AUTHOR
131              
132             Mark Rushing <mark@orbislumen.net>
133              
134             =head1 COPYRIGHT AND LICENSE
135              
136             This software is copyright (c) 2017 by Home Grown Systems, SPC.
137              
138             This is free software; you can redistribute it and/or modify it under
139             the same terms as the Perl 5 programming language system itself.
140              
141             =cut