File Coverage

lib/Image/Randim/Source/Unsplash.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 4 50.0
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 29 33 87.8


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