File Coverage

blib/lib/Flickr/API2/Photo.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 5 5 100.0
total 36 38 94.7


line stmt bran cond sub pod time code
1             package Flickr::API2::Photo;
2 4     4   3414 use Encode::Base58;
  4         2395  
  4         295  
3 4     4   24 use Mouse;
  4         9  
  4         34  
4             extends 'Flickr::API2::Base';
5              
6             =head1 NAME
7              
8             Flickr::API2::Photo
9              
10             =head1 SYNOPSIS
11              
12             Represents a Flickr photo, with helper methods. (Incomplete)
13              
14             =head1 ATTRIBUTES
15              
16             title
17              
18             date_upload
19              
20             date_taken
21              
22             owner_name
23              
24             url_s url_m url_l
25              
26             description
27              
28             path_alias
29              
30             license
31              
32             tags
33              
34             height_s
35              
36             width_s
37              
38             height_m
39              
40             width_m
41              
42             height_l
43              
44             width_l
45              
46             height_o
47              
48             width_o
49              
50             =cut
51              
52             has 'id' => (
53             is => 'ro',
54             required => 1,
55             );
56             has 'title' => ( is => 'rw' );
57             has 'date_upload' => ( is => 'rw' );
58             has 'date_taken' => ( is => 'rw' );
59             has 'count_faves' => ( is => 'rw' );
60             has 'owner_id' => ( is => 'rw' );
61             has 'owner_name' => ( is => 'rw' );
62             has 'url_sq' => (
63             is => 'rw',
64             lazy => 1,
65             default => sub { $_[0]->populate_size_urls; $_[0]->url_sq; },
66             );
67             has 'url_t' => (
68             is => 'rw',
69             lazy => 1,
70             default => sub { $_[0]->populate_size_urls; $_[0]->url_t; },
71             );
72             has 'url_s' => (
73             is => 'rw',
74             lazy => 1,
75             default => sub { $_[0]->populate_size_urls; $_[0]->url_s; },
76             );
77             has 'url_m' => (
78             is => 'rw',
79             lazy => 1,
80             default => sub { $_[0]->populate_size_urls; $_[0]->url_m; },
81             );
82             has 'url_l' => (
83             is => 'rw',
84             lazy => 1,
85             default => sub { $_[0]->populate_size_urls; $_[0]->url_l; },
86             );
87             has 'url_o' => (
88             is => 'rw',
89             lazy => 1,
90             default => sub { $_[0]->populate_size_urls; $_[0]->url_o; },
91             );
92             has 'description' => ( is => 'rw' );
93             has 'path_alias' => ( is => 'rw' );
94             has 'license' => ( is => 'rw' );
95             has 'tags' => ( is => 'rw' );
96             has 'height_s' => ( is => 'rw' );
97             has 'width_s' => ( is => 'rw' );
98             has 'height_m' => ( is => 'rw' );
99             has 'width_m' => ( is => 'rw' );
100             has 'height_l' => ( is => 'rw' );
101             has 'width_l' => ( is => 'rw' );
102             has 'height_o' => ( is => 'rw' );
103             has 'width_o' => ( is => 'rw' );
104              
105             =head1 METHODS
106              
107             =head2 info
108              
109             Returns getInfo results for this photo; contains lots of info that isn't
110             currently available by simple accessors.
111              
112             =cut
113              
114             sub info {
115 1     1 1 2 my ($self) = shift;
116 1         15 my $response = $self->api->execute_method(
117             'flickr.photos.getInfo',
118             {
119             photo_id => $self->id,
120             }
121             );
122 1         10 return $response;
123             }
124              
125             =head2 sizes
126              
127             Returns getSizes results for this photo.
128              
129             Note that you probably don't need to call this directly - instead, see the
130             various url accessors. If they're blank, try calling populate_size_urls()
131             first.
132              
133             =cut
134              
135             sub sizes {
136 1     1 1 2 my ($self) = shift;
137 1         11 my $response = $self->api->execute_method(
138             'flickr.photos.getSizes',
139             {
140             photo_id => $self->id,
141             }
142             );
143 1         11 return $response->{sizes};
144             }
145              
146             =head2 populate_size_urls
147              
148             Populates all the various URLs to the differently-sized images by querying
149             the API. This *should* have been done automatically for you, the first time
150             you queried one of the url accessors.
151              
152             =cut
153              
154             our %label_to_accessor = (
155             Square => 'url_sq',
156             Thumbnail => 'url_t',
157             Small => 'url_s',
158             Medium => 'url_m',
159             Large => 'url_l',
160             Original => 'url_o',
161             );
162              
163             sub populate_size_urls {
164 1     1 1 2 my $self = shift;
165 1         4 my $sizes = $self->sizes->{size};
166 1         5 for my $s (@$sizes) {
167 9 100       34 if (my $acc = $label_to_accessor{$s->{label}}) {
168 6         44 $self->$acc($s->{source});
169             }
170             }
171             }
172              
173             =head2 page_url
174              
175             Returns the URL for this photo's page on Flickr.
176              
177             =cut
178              
179             # See http://www.flickr.com/services/api/misc.urls.html
180             # for generation method
181             sub page_url {
182 3     3 1 5862 my $self = shift;
183 3   33     78 return sprintf('http://flickr.com/photos/%s/%d',
184             ($self->path_alias || $self->owner_name || $self->owner_id),
185             $self->id
186             );
187             }
188              
189             =head2 short_url
190              
191             Returns the shortened URL for the photo page, ie. at http://flic.kr/
192              
193             =cut
194              
195             sub short_url {
196 1     1 1 2 my $self = shift;
197 1         13 sprintf('http://flic.kr/p/%s', encode_base58($self->id));
198             }
199              
200             __PACKAGE__->meta->make_immutable;
201             1;