| 1 | package Image::Randim::Source::Unsplash; | ||||||||||
| 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 JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | has 'name' => ( is => 'ro', | |||||||||
| 9 | isa => 'Str', | ||||||||||
| 10 | default => 'Unsplash', | ||||||||||
| 11 | ); | ||||||||||
| 12 | 1 | has 'api_url' => ( is => 'ro', | |||||||||
| 13 | isa => 'Str', | ||||||||||
| 14 | default => 'https://api.unsplash.com/photos/random?client_id=', | ||||||||||
| 15 | ); | ||||||||||
| 16 | 1 | has 'api_key' => ( is => 'rw', | |||||||||
| 17 | isa => 'Str', | ||||||||||
| 18 | default => '03ad5bfbaa0acd6c96a728d425e533683ec25e5fb7fcf99f6461720b3d0d75a1', | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | 1 | with 'Image::Randim::Source::Role'; | |||||||||
| 22 | |||||||||||
| 23 | sub url { | ||||||||||
| 24 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
url: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 2 | return $self->api_url.$self->api_key; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | sub get_image { | ||||||||||
| 29 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_image: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 30 | 1 | 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 | 1 | link => $$data{'links'}{'html'}, | |||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | if (exists $$data{'user'}{'username'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 1 | $image->owner($$data{'user'}{'username'}); | |||||||||
| 42 | } | ||||||||||
| 43 | 1 | ▶ | if (exists $$data{'user'}{'name'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 1 | $image->owner_name($$data{'user'}{'name'}); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 1 | return $image; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 51 | 1 | 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 | ||||||||||