License/Helper.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« License.pm ↑ Summary
1 package Flickr::License::Helper;
2 2 ▶use Class::Singleton;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Flickr::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use Flickr::API::Utils;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Carp;
6 use strict;
7  
8 use vars qw(@ISA);
9 @ISA = qw(Class::Singleton);
10  
11 =head1 NAME
12  
13 Flickr::License::Helper - Helper class to grab details of the currently supported licenses on Flickr
14  
15 =head1 VERSION
16  
17 Version 0.02
18  
19 =cut
20  
21 our $VERSION = '0.02';
22  
23 =head1 SYNOPSIS
24  
25 my $helper = Flickr::License::Helper->get_instance($APIKEY);
26
27 my $license_name = $helper->licenses->{$license_id}{name};
28 my $license_url = $helper->licenses->{}$license_id}{url};
29  
30 =head1 DESCRIPTION
31  
32 The class is a singleton that caches the data returned by the flickr.photo.getLicenses api call in a simple hash.
33  
34 It only grabs the data when the API key is set or changed, or if the refresh function is explicitly called.
35  
36 =head1 METHODS
37  
38 =cut
39  
40 #_new_instance
41 #Returns a new instance of the Helper.
42 #It is called by the Class::Singleton method ->instance() if the class is not initialised.
43  
44 sub _new_instance {
45 my $class = shift;
46 my $api_key = shift;
47  
48 my $me = { utils => Flickr::API::Utils->new() };
49
50 my $self = bless $me, $class;
51  
52 if (defined $api_key) {
53 $self->api_key($api_key);
54 }
55  
56 return $self;
57 }
58  
59 =head2 api_key
60  
61 Returns the current api_key. If an argument is passed in that is set to be the current api_key.
62  
63 If the key has changed then the license data is refreshed from flickr
64  
65 =cut
66  
67 sub api_key {
68 my $self=shift;
69 my $key=shift;
70  
71
72 if (defined $key &&
73 (!exists $self->{api_key} || !defined $self->{api_key} || $self->{api_key} ne $key))
74 {
75 $self->{api_key}=$key;
76 $self->refresh();
77 }
78
79 return $self->{api_key};
80 }
81  
82 =head2 refresh
83  
84 Refreshes the internal cache from flickr
85  
86 =cut
87  
88 sub refresh {
89  
90 my $self=shift;
91  
92 # no license key means no hash
93 if ($self->{api_key} eq '') {
94 croak "No flickr API key defined, can't get license info";
95 }
96  
97 my $api=new Flickr::API({key => $self->{api_key}});
98 my $response=$api->execute_method('flickr.photos.licenses.getInfo');
99  
100 # License info as of 20070711
101 # my $xml=<<END
102 #<licenses>
103 # <license id="4" name="Attribution License"
104 # url="http://creativecommons.org/licenses/by/2.0/" />
105 # <license id="6" name="Attribution-NoDerivs License"
106 # url="http://creativecommons.org/licenses/by-nd/2.0/" />
107 # <license id="3" name="Attribution-NonCommercial-NoDerivs License"
108 # url="http://creativecommons.org/licenses/by-nc-nd/2.0/" />
109 # <license id="2" name="Attribution-NonCommercial License"
110 # url="http://creativecommons.org/licenses/by-nc/2.0/" />
111 # <license id="1" name="Attribution-NonCommercial-ShareAlike License"
112 # url="http://creativecommons.org/licenses/by-nc-sa/2.0/" />
113 # <license id="5" name="Attribution-ShareAlike License"
114 # url="http://creativecommons.org/licenses/by-sa/2.0/" />
115 #</licenses>
116 #END
117 # ;
118  
119 my $result={};
120 $self->{utils}->test_return($response,$result);
121 if ($result->{success}) {
122 my $xml=$response;#->{_content};
123 $self->{licenses} = $self->_parse_licenses($xml);
124 } else {
125 croak "Failed to get license data: $result->{error_message} ($result->{error_code})";
126 }
127 }
128  
129 # _parse_licenses
130 # parses the license data returned by flickr into the internal cache
131  
132 sub _parse_licenses {
133  
134 my $self=shift;
135 my $xml=shift;
136  
137 my $ret = {};
138  
139 foreach my $node (@{$xml->{tree}{children}}) {
140 if ($node->{type} eq 'tag' and $node->{name} eq 'licenses') {
141 foreach my $license (@{$node->{children}}) {
142 if ($license->{type} eq 'tag' and $license->{name} eq 'license') {
143 $ret->{$license->{attributes}{id}}{name}=$license->{attributes}{name};
144 $ret->{$license->{attributes}{id}}{url}=$license->{attributes}{url};
145 }
146 }
147 }
148 }
149  
150 return $ret;
151  
152 }
153  
154 =head2 licenses
155  
156 Returns a handle to the internal cache of licenses. The structure should look something like this:
157  
158 {
159 '6' => {
160 'url' => 'http://creativecommons.org/licenses/by-nd/2.0/',
161 'name' => 'Attribution-NoDerivs License'
162 },
163 '1' => {
164 'url' => 'http://creativecommons.org/licenses/by-nc-sa/2.0/',
165 'name' => 'Attribution-NonCommercial-ShareAlike License'
166 },
167 '4' => {
168 'url' => 'http://creativecommons.org/licenses/by/2.0/',
169 'name' => 'Attribution License'
170 },
171 '3' => {
172 'url' => 'http://creativecommons.org/licenses/by-nc-nd/2.0/',
173 'name' => 'Attribution-NonCommercial-NoDerivs License'
174 },
175 '0' => {
176 'url' => '',
177 'name' => 'All Rights Reserved'
178 },
179 '2' => {
180 'url' => 'http://creativecommons.org/licenses/by-nc/2.0/',
181 'name' => 'Attribution-NonCommercial License'
182 },
183 '5' => {
184 'url' => 'http://creativecommons.org/licenses/by-sa/2.0/',
185 'name' => 'Attribution-ShareAlike License'
186 }
187 }
188  
189 =cut
190  
191 sub licenses {
192 my $self=shift;
193 return $self->{licenses};
194 }
195  
196 =head1 AUTHOR
197  
198 Billy Abbott, C<< <billy@cowfish.org.uk> >>
199  
200 =head1 COPYRIGHT & LICENSE
201  
202 Copyright 2007 Billy Abbott, All Rights Reserved.
203  
204 This program is free software; you can redistribute it and/or modify it
205 under the same terms as Perl itself.
206  
207 =head1 SEE ALSO
208  
209 <http://www.flickr.com/>, Flickr::API, Flickr::Photo
210  
211 =cut
212  
213 1;
« License.pm ↑ Summary