| 1 | package Image::Randim::Source::Desktoppr; | ||
| 2 | our $VERSION = '0.01'; | ||
| 3 | use v5.20; | ||
| 4 | use warnings; | ||
| 5 | use JSON; | ||
| 6 | use Moose; | ||
| 7 | |||
| 8 | has 'name' => ( is => 'ro', | ||
| 9 | isa => 'Str', | ||
| 10 | default => 'Desktoppr', | ||
| 11 | ); | ||
| 12 | has 'url' => ( is => 'ro', | ||
| 13 | isa => 'Str', | ||
| 14 | default => 'https://api.desktoppr.co/1/wallpapers/random', | ||
| 15 | ); | ||
| 16 | |||
| 17 | with 'Image::Randim::Source::Role'; | ||
| 18 | |||
| 19 | sub get_image { | ||
| 20 | my $self = shift; | ||
| 21 | my $data = JSON->new->decode($self->get_response); | ||
| 22 | $data = $$data{'response'}; | ||
| 23 | |||
| 24 | my $image = Image::Randim::Image->new( | ||
| 25 | url => $$data{'image'}{'url'}, | ||
| 26 | id => $$data{'id'}, | ||
| 27 | width => $$data{'width'}, | ||
| 28 | height => $$data{'height'}, | ||
| 29 | link => $$data{'url'}, | ||
| 30 | ); | ||
| 31 | |||
| 32 | if (exists $$data{'uploader'}) { | ||
| 33 | $image->owner($$data{'uploader'}); | ||
| 34 | } | ||
| 35 | |||
| 36 | return $image; | ||
| 37 | } | ||
| 38 | |||
| 39 | __PACKAGE__->meta->make_immutable; | ||
| 40 | 1; | ||
| 41 | |||
| 42 | =pod | ||
| 43 | |||
| 44 | =head1 NAME | ||
| 45 | |||
| 46 | Image::Randim::Source::Desktoppr - Desktoppr source plugin | ||
| 47 | |||
| 48 | =head1 SYNOPSIS | ||
| 49 | |||
| 50 | use Image::Randim::Source; | ||
| 51 | |||
| 52 | $source = Image::Randim::Source->new; | ||
| 53 | $source->set_provider('Desktoppr'); | ||
| 54 | $image = $source->get_image; | ||
| 55 | |||
| 56 | say $image->url; | ||
| 57 | |||
| 58 | =head1 DESCRIPTION | ||
| 59 | |||
| 60 | You will probably not want to call this directly, and instead ose | ||
| 61 | Image::Randim::Source as described in that class' documentation. | ||
| 62 | |||
| 63 | This "plugin" conforms to the Image::Randim::Source::Role and provides | ||
| 64 | access to a random image information from Desktoppr. | ||
| 65 | |||
| 66 | =head1 AUTHOR | ||
| 67 | |||
| 68 | Mark Rushing <mark@orbislumen.net> | ||
| 69 | |||
| 70 | =head1 COPYRIGHT AND LICENSE | ||
| 71 | |||
| 72 | This software is copyright (c) 2017 by Home Grown Systems, SPC. | ||
| 73 | |||
| 74 | This is free software; you can redistribute it and/or modify it under | ||
| 75 | the same terms as the Perl 5 programming language system itself. | ||
| 76 | |||
| 77 | =cut |