blib/lib/Image/Randim/Source/Role.pm untested

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