| 1 | package Flickr::License; | ||
| 2 | |||
| 3 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 0 | ▶ | use Flickr::License::Helper; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 5 | use strict; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Flickr::License - Represents the license of a photo from Flickr | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | Version 0.02 | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | our $VERSION = '0.02'; | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | my $license=Flickr::License->new({api_key=>$APIKEY, id=>$license_number}); | ||
| 22 | if ($license->valid) { | ||
| 23 | my $license_name = $license->name; | ||
| 24 | my $license_url = $license->url; | ||
| 25 | } | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | This class retrieves data about the currently available Flickr photo licenses and returns the details that Flickr does based on the license number passed in. | ||
| 30 | |||
| 31 | The license number can be obtained easily from the Flickr::Photo object using the ->license() call. | ||
| 32 | |||
| 33 | If the license number isn't recognised in the data structure that Flickr returns then the "valid" property of the class is set to 0 and the name is set to "Invalid License" | ||
| 34 | |||
| 35 | Behind the scenes it uses a singleton that holds the necessary license date, only refreshing itself if it thinks it needs to. | ||
| 36 | |||
| 37 | =head1 METHODS | ||
| 38 | |||
| 39 | =head2 new | ||
| 40 | |||
| 41 | Sets up the license object. | ||
| 42 | |||
| 43 | my $license=Flickr::License->new({api_key => $APIKEY, | ||
| 44 | id => $license_id}); | ||
| 45 | |||
| 46 | id is optional and can be set using the ->id() setter/getter -> details of the license are only filled in once id is set. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my $class = shift; | ||
| 52 | my $params = shift; | ||
| 53 | |||
| 54 | if (!exists $params->{api_key}) { | ||
| 55 | croak("Can't create a license without an Flick API key") | ||
| 56 | } | ||
| 57 | |||
| 58 | my $me = { api_key => $params->{api_key} }; | ||
| 59 | |||
| 60 | my $self = bless $me, $class; | ||
| 61 | |||
| 62 | # init the license helper | ||
| 63 | my $factory=Flickr::License::Helper->instance($self->{api_key}); | ||
| 64 | |||
| 65 | if (exists $params->{id}) { | ||
| 66 | $self->{id}=$params->{id}; | ||
| 67 | $self->_init; | ||
| 68 | } | ||
| 69 | |||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | #_init | ||
| 74 | #Function to grab the license information from the Flickr::License::Helper object for this object. | ||
| 75 | #Will croak if the license id is not set. That shouldn't happen as it is only called when the id is set. | ||
| 76 | |||
| 77 | sub _init { | ||
| 78 | my $self=shift; | ||
| 79 | |||
| 80 | if (!exists $self->{id}) { | ||
| 81 | croak "Cannot get license information without a license id"; | ||
| 82 | } | ||
| 83 | |||
| 84 | my $number=$self->{id}; | ||
| 85 | |||
| 86 | my $factory=Flickr::License::Helper->instance(); | ||
| 87 | |||
| 88 | my $licenses=$factory->licenses; | ||
| 89 | if (exists $licenses->{$number}) { | ||
| 90 | $self->{name}=$licenses->{$number}->{name}; | ||
| 91 | $self->{url}=$licenses->{$number}->{url}; | ||
| 92 | $self->{valid}=1; | ||
| 93 | } else { | ||
| 94 | $self->{name}="Invalid license"; | ||
| 95 | $self->{url}=""; | ||
| 96 | $self->{valid}=0; | ||
| 97 | } | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | =head2 id | ||
| 102 | |||
| 103 | my $id = $license->id; | ||
| 104 | my $different_id = $license->id($a_different_id); | ||
| 105 | |||
| 106 | Returns the id. Also sets the id if an argument is passed in - it will always return the new id. | ||
| 107 | |||
| 108 | Will also populate the object with license information. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub id { | ||
| 113 | my $self=shift; | ||
| 114 | my $num=shift; | ||
| 115 | |||
| 116 | if (defined $num) { | ||
| 117 | $self->{id}=$num; | ||
| 118 | $self->_init; | ||
| 119 | } | ||
| 120 | |||
| 121 | return $self->{id}; | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 name | ||
| 125 | |||
| 126 | Returns the license name | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub name { | ||
| 131 | my $self=shift; | ||
| 132 | return $self->{name}; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 url | ||
| 136 | |||
| 137 | Returns the license url | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub url { | ||
| 142 | my $self=shift; | ||
| 143 | return $self->{url}; | ||
| 144 | } | ||
| 145 | |||
| 146 | =head2 valid | ||
| 147 | |||
| 148 | Returns whether the license id was valid. 0=false, 1=true. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub valid { | ||
| 153 | my $self=shift; | ||
| 154 | return $self->{valid}; | ||
| 155 | } | ||
| 156 | |||
| 157 | =head1 AUTHOR | ||
| 158 | |||
| 159 | Billy Abbott, C<< <billy@cowfish.org.uk> >> | ||
| 160 | |||
| 161 | =head1 COPYRIGHT & LICENSE | ||
| 162 | |||
| 163 | Copyright 2007 Billy Abbott, All Rights Reserved. | ||
| 164 | |||
| 165 | This program is free software; you can redistribute it and/or modify it | ||
| 166 | under the same terms as Perl itself. | ||
| 167 | |||
| 168 | =head1 SEE ALSO | ||
| 169 | |||
| 170 | <http://www.flickr.com/>, Flickr::API, Flickr::Photo | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | 1; | ||