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