| 1 | package Image::Randim::Source::Unsplash; | ||
| 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 => 'Unsplash', | ||
| 11 | ); | ||
| 12 | has 'api_url' => ( is => 'ro', | ||
| 13 | isa => 'Str', | ||
| 14 | default => 'https://api.unsplash.com/photos/random?client_id=', | ||
| 15 | ); | ||
| 16 | has 'api_key' => ( is => 'rw', | ||
| 17 | isa => 'Str', | ||
| 18 | default => '03ad5bfbaa0acd6c96a728d425e533683ec25e5fb7fcf99f6461720b3d0d75a1', | ||
| 19 | ); | ||
| 20 | |||
| 21 | with 'Image::Randim::Source::Role'; | ||
| 22 | |||
| 23 | sub url { | ||
| 24 | my $self = shift; | ||
| 25 | return $self->api_url.$self->api_key; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub get_image { | ||
| 29 | my $self = shift; | ||
| 30 | my $data = JSON->new->decode($self->get_response); | ||
| 31 | |||
| 32 | my $image = Image::Randim::Image->new( | ||
| 33 | url => $$data{'urls'}{'full'}, | ||
| 34 | id => $$data{'id'}, | ||
| 35 | width => $$data{'width'}, | ||
| 36 | height => $$data{'height'}, | ||
| 37 | link => $$data{'links'}{'html'}, | ||
| 38 | ); | ||
| 39 | |||
| 40 | if (exists $$data{'user'}{'username'}) { | ||
| 41 | $image->owner($$data{'user'}{'username'}); | ||
| 42 | } | ||
| 43 | if (exists $$data{'user'}{'name'}) { | ||
| 44 | $image->owner_name($$data{'user'}{'name'}); | ||
| 45 | } | ||
| 46 | |||
| 47 | return $image; | ||
| 48 | } | ||
| 49 | |||
| 50 | __PACKAGE__->meta->make_immutable; | ||
| 51 | 1; | ||
| 52 | |||
| 53 | =pod | ||
| 54 | |||
| 55 | =head1 NAME | ||
| 56 | |||
| 57 | Image::Randim::Source::Unsplash - Unsplash source plugin | ||
| 58 | |||
| 59 | =head1 SYNOPSIS | ||
| 60 | |||
| 61 | use Image::Randim::Source; | ||
| 62 | |||
| 63 | $source = Image::Randim::Source->new; | ||
| 64 | $source->set_provider('Unsplash'); | ||
| 65 | $image = $source->get_image; | ||
| 66 | |||
| 67 | say $image->url; | ||
| 68 | |||
| 69 | # OR if you want to use your own Unsplash API client key | ||
| 70 | # access the src_obj and set it there before calling get_image | ||
| 71 | |||
| 72 | $source->src_obj->api_key('980ef8da882...'); | ||
| 73 | |||
| 74 | =head1 DESCRIPTION | ||
| 75 | |||
| 76 | You will probably not want to call this directly, and instead ose | ||
| 77 | Image::Randim::Source as described in that class' documentation. | ||
| 78 | |||
| 79 | Unsplash requires that you use a so-called developer API client key to | ||
| 80 | get a random image information. However, these client keys can be | ||
| 81 | rate-limited. | ||
| 82 | |||
| 83 | If you experience this, you can register for your own developer client | ||
| 84 | API key and use it instead as outlined in the SYNOPSIS. | ||
| 85 | |||
| 86 | Otherwise, this "plugin" conforms to the Image::Randim::Source::Role | ||
| 87 | just like the others. | ||
| 88 | |||
| 89 | =head1 AUTHOR | ||
| 90 | |||
| 91 | Mark Rushing <mark@orbislumen.net> | ||
| 92 | |||
| 93 | =head1 COPYRIGHT AND LICENSE | ||
| 94 | |||
| 95 | This software is copyright (c) 2017 by Home Grown Systems, SPC. | ||
| 96 | |||
| 97 | This is free software; you can redistribute it and/or modify it under | ||
| 98 | the same terms as the Perl 5 programming language system itself. | ||
| 99 | |||
| 100 | =cut |