| 1 | package Image::Randim::Source; | ||||||||||
| 2 | 1 | our $VERSION = '0.02'; | |||||||||
| 3 | 1 | ▶ | use v5.20; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Module::Find; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | usesub Image::Randim::Source; | |||||||||
| 9 | |||||||||||
| 10 | 1 | has 'src_obj' => ( | |||||||||
| 11 | is => 'ro', | ||||||||||
| 12 | isa => 'Image::Randim::Source::Role', | ||||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | 1 | has 'timeout' => ( | |||||||||
| 16 | is => 'rw', | ||||||||||
| 17 | isa => 'Int', | ||||||||||
| 18 | default => 20, | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | # This property will be removed - it's only here to fix the badness | ||||||||||
| 22 | # of people who don't keep their module libraries clean between | ||||||||||
| 23 | # released versions of this module. | ||||||||||
| 24 | has 'autoload_blacklist' => ( | ||||||||||
| 25 | is => 'ro', | ||||||||||
| 26 | isa => 'HashRef', | ||||||||||
| 27 | 2 | ▶ | default => sub { {'Image::Randim::Source::Role' => 1, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 'Image::Randim::Source::Desktoppr' => 1, | ||||||||||
| 29 | } | ||||||||||
| 30 | }, | ||||||||||
| 31 | 1 | ); | |||||||||
| 32 | |||||||||||
| 33 | 28 | ▶ | sub list { | ||||||||
|
Subroutine
list: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 34 | 28 | my $self = shift; | |||||||||
| 35 | #my @class = map { s/.+::(.+)$/$1/r } grep {!/::Role$/ && !/::Desktoppr$/} findsubmod Image::Randim::Source; | ||||||||||
| 36 | 28 | my @class; | |||||||||
| 37 | 28 | foreach (findsubmod Image::Randim::Source) { | |||||||||
| 38 | 56 | ▶ | next if defined ${$self->autoload_blacklist}{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 28 | push @class, s/.+::(.+)$/$1/r; | |||||||||
| 40 | } | ||||||||||
| 41 | 28 | return \@class; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 28 | ▶ | sub set_provider { | ||||||||
|
Subroutine
set_provider: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | 28 | my ($self, $source_name) = @_; | |||||||||
| 46 | 28 | $self->{src_obj} = "Image::Randim::Source::$source_name"->new; | |||||||||
| 47 | 27 | $self->{src_obj}->timeout($self->timeout); | |||||||||
| 48 | 27 | return $self->src_obj; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 26 | ▶ | sub set_random_provider { | ||||||||
|
Subroutine
set_random_provider: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 52 | 26 | my ($self, $provider_names) = @_; | |||||||||
| 53 | 26 | ▶ | my $source = $provider_names ? $provider_names : $self->list; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 26 | return $self->set_provider($$source[int(rand(scalar @$source))]); | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub url { | ||||||||
|
Subroutine
url: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 58 | 1 | my $self = shift; | |||||||||
| 59 | 1 | ▶ | die 'No valid provider' unless $self->src_obj; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 1 | return $self->src_obj->url; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 27 | ▶ | sub name { | ||||||||
|
Subroutine
name: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 64 | 27 | my $self = shift; | |||||||||
| 65 | 27 | ▶ | die 'No valid provider' unless $self->src_obj; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 27 | return $self->src_obj->name; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub get_image { | ||||||||
|
Subroutine
get_image: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 70 | 0 | my $self = shift; | |||||||||
| 71 | 0 | ▶ | die 'No valid provider' unless $self->src_obj; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 0 | return $self->src_obj->get_image; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 76 | 1 | 1; | |||||||||
| 77 | |||||||||||
| 78 | =pod | ||||||||||
| 79 | |||||||||||
| 80 | =head1 NAME | ||||||||||
| 81 | |||||||||||
| 82 | Image::Randim::Source - Pull a random image from a source | ||||||||||
| 83 | |||||||||||
| 84 | =head1 SYNOPSIS | ||||||||||
| 85 | |||||||||||
| 86 | use Image::Randim::Source; | ||||||||||
| 87 | |||||||||||
| 88 | $source = Image::Randim::Source->new; | ||||||||||
| 89 | $source->set_random_provider; | ||||||||||
| 90 | $image = $source->get_image; | ||||||||||
| 91 | |||||||||||
| 92 | say $image->url; | ||||||||||
| 93 | |||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||
| 95 | |||||||||||
| 96 | This is the main class to instantiate when wanting to pull random | ||||||||||
| 97 | image information from the defined sources. | ||||||||||
| 98 | |||||||||||
| 99 | You can specifically state which source to use to pull a random image | ||||||||||
| 100 | from by using the set_provider() method, or you can also let the | ||||||||||
| 101 | provider be a random choice too by calling the method | ||||||||||
| 102 | set_random_provider() instead. | ||||||||||
| 103 | |||||||||||
| 104 | Nothing much will work until you call one of those two methods, except | ||||||||||
| 105 | for list(), which returns a list of defined sources (providers of | ||||||||||
| 106 | images). | ||||||||||
| 107 | |||||||||||
| 108 | =head1 METHODS | ||||||||||
| 109 | |||||||||||
| 110 | =head2 C<list> | ||||||||||
| 111 | |||||||||||
| 112 | Return a list of supported providers (ones with "plugins"). | ||||||||||
| 113 | |||||||||||
| 114 | =head2 C<set_provider($provider_name)> | ||||||||||
| 115 | |||||||||||
| 116 | Sets the provider name to use, to grab a random image. Valid ones can | ||||||||||
| 117 | be found with the list() method. | ||||||||||
| 118 | |||||||||||
| 119 | "Plugins" can be created to support more providers by implementing the | ||||||||||
| 120 | Image::Randim::Source::Role | ||||||||||
| 121 | |||||||||||
| 122 | =head2 C<set_random_provider(['name1', 'name2'])> | ||||||||||
| 123 | |||||||||||
| 124 | Without any parameters, chooses a random provider from the list() of | ||||||||||
| 125 | possible providers and set_provider()'s to it. | ||||||||||
| 126 | |||||||||||
| 127 | An array ref - a list of source providers names - can be provided if | ||||||||||
| 128 | you want to limit the random choice to just those providers. | ||||||||||
| 129 | |||||||||||
| 130 | =head2 C<url> | ||||||||||
| 131 | |||||||||||
| 132 | Returns the URL from which the provider information is gathered. You | ||||||||||
| 133 | probably don't need to care about this. | ||||||||||
| 134 | |||||||||||
| 135 | =head2 C<name> | ||||||||||
| 136 | |||||||||||
| 137 | Returns the provider name (as it was set by set_provider()) | ||||||||||
| 138 | |||||||||||
| 139 | =head2 C<get_image> | ||||||||||
| 140 | |||||||||||
| 141 | Returns an Image::Randim::Image object, populated with the necessary | ||||||||||
| 142 | information to download the image, and image credits for the owner of | ||||||||||
| 143 | the image, as well as reported dimensions of the image. | ||||||||||
| 144 | |||||||||||
| 145 | =head1 ATTRIBUTES | ||||||||||
| 146 | |||||||||||
| 147 | =head2 C<timeout> | ||||||||||
| 148 | |||||||||||
| 149 | Gets or sets the timeout in seconds we will wait for a response from | ||||||||||
| 150 | the provider's server for the information. If you want something | ||||||||||
| 151 | different than the default (25 seconds) you must set this attribute | ||||||||||
| 152 | before calling the set_* methods. | ||||||||||
| 153 | |||||||||||
| 154 | =head2 C<src_obj> | ||||||||||
| 155 | |||||||||||
| 156 | Gives access to the provider "plugin" object directly. Not typically | ||||||||||
| 157 | needed. | ||||||||||
| 158 | |||||||||||
| 159 | =head1 AUTHOR | ||||||||||
| 160 | |||||||||||
| 161 | Mark Rushing <mark@orbislumen.net> | ||||||||||
| 162 | |||||||||||
| 163 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 164 | |||||||||||
| 165 | This software is copyright (c) 2017 by Home Grown Systems, SPC. | ||||||||||
| 166 | |||||||||||
| 167 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 168 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 169 | |||||||||||
| 170 | =cut | ||||||||||