Shutterstock.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Shutterstock/SearchResult/Image.pm ↑ Summary Shutterstock/HasClient.pm »
1 package WebService::Shutterstock;
2 {
3 $WebService::Shutterstock::VERSION = '0.006';
4 }
5  
6 # ABSTRACT: Easy access to Shutterstock's public API
7  
8 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use Moo 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use WebService::Shutterstock::Lightbox;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use WebService::Shutterstock::Client;
17 use WebService::Shutterstock::Customer;
18 use WebService::Shutterstock::SearchResults;
19 use WebService::Shutterstock::Exception;
20 use WebService::Shutterstock::Video;
21  
22 has api_username => (
23 is => 'ro',
24 required => 1,
25 );
26 has api_key => (
27 is => 'ro',
28 required => 1,
29 );
30 has client => (
31 is => 'lazy',
32 clearer => 1,
33 );
34  
35 sub _build_client {
36 my $self = shift;
37 my $client = WebService::Shutterstock::Client->new( host => $ENV{SS_API_HOST} || 'https://api.shutterstock.com' );
38 $client->addHeader(
39 Authorization => sprintf(
40 'Basic %s',
41 MIME::Base64::encode(
42 join( ':', $self->api_username, $self->api_key )
43 )
44 )
45 );
46 return $client;
47 }
48  
49  
50  
51 sub auth {
52 my $self = shift;
53 my %args = @_;
54 $args{username} ||= $self->api_username;
55 if(!$args{password}){
56 die WebService::Shutterstock::Exception->new( error => "missing 'password' param for auth call");
57 }
58 $self->client->POST(
59 '/auth/customer.json',
60 {
61 username => $args{username},
62 password => $args{password}
63 }
64 );
65 my $auth_info = $self->client->process_response;
66 if(ref($auth_info) eq 'HASH'){
67 return WebService::Shutterstock::Customer->new( auth_info => $auth_info, client => $self->client );;
68 } else {
69 die WebService::Shutterstock::Exception->new(
70 response => $self->client->response,
71 error => "Error authenticating $args{username}: $auth_info"
72 );
73 }
74 }
75  
76  
77 sub categories {
78 my $self = shift;
79 $self->client->GET('/categories.json');
80 return $self->client->process_response;
81 }
82  
83  
84 sub search {
85 my $self = shift;
86 my %args = @_;
87 my $type = delete $args{type} || 'image';
88 return WebService::Shutterstock::SearchResults->new( client => $self->client, query => \%args, type => $type );
89 }
90  
91  
92 sub search_images {
93 return shift->search(@_, type => 'image');
94 }
95  
96  
97 sub search_videos {
98 return shift->search(@_, type => 'video');
99 }
100  
101  
102 sub image {
103 my $self = shift;
104 my $image_id = shift;
105 my $image = WebService::Shutterstock::Image->new( image_id => $image_id, client => $self->client );
106 return $image->is_available ? $image : undef;
107 }
108  
109  
110 sub video {
111 my $self = shift;
112 my $video_id = shift;
113 my $video = WebService::Shutterstock::Video->new( video_id => $video_id, client => $self->client );
114 return $video->is_available ? $video : undef;
115 }
116  
117 1;
118  
119 __END__
« Shutterstock/SearchResult/Image.pm ↑ Summary Shutterstock/HasClient.pm »