| 1 | package Net::GNUDB::Cd; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Net::GNUDB::Cd - Base class for L<Net::GNUDBSearch> results. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Net::GNUDB::Cd; | ||
| 12 | my $config = { | ||
| 13 | 'id' => '950cc10c', | ||
| 14 | 'genre' => 'misc' | ||
| 15 | } | ||
| 16 | my $cd = Net::GNUDB::Cd->new($config); | ||
| 17 | my $id = $cd->getId(); | ||
| 18 | my $genre = $cd->getGenre(); | ||
| 19 | my $tracks = $cd->getTracks(); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Base class for L<Net::GNUDBSearch> results, normally not instantiated directly but can used to lookup a specific GNUDB entry. | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | use warnings; | ||
| 30 | use strict; | ||
| 31 | use Carp; | ||
| 32 | use Net::FreeDB2; | ||
| 33 | use Net::FreeDB2::Match; | ||
| 34 | use Net::FreeDB2::Entry; #see bug: https://rt.cpan.org/Ticket/Display.html?id=69089 | ||
| 35 | ######################################################### | ||
| 36 | |||
| 37 | =head2 new($config) | ||
| 38 | |||
| 39 | my $config = { | ||
| 40 | 'id' => '950cc10c', | ||
| 41 | 'genre' => 'misc' | ||
| 42 | } | ||
| 43 | my $cd = Net::GNUDB::Cd->new($config); | ||
| 44 | |||
| 45 | Constructor, returns a new instance of the search CD object. Requires all two of the above elements in the provided hash reference for operation. These | ||
| 46 | elements must match a GNUDB entry. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | ######################################################### | ||
| 51 | sub new{ | ||
| 52 | my($class, $config) = @_; | ||
| 53 | my $self = { | ||
| 54 | '__id' => undef, | ||
| 55 | '__genre' => undef, | ||
| 56 | '__tracks' => [] | ||
| 57 | }; | ||
| 58 | bless $self, $class; | ||
| 59 | $self->__setId($config->{'id'}); | ||
| 60 | $self->__setGenre($config->{'genre'}); | ||
| 61 | return $self; | ||
| 62 | } | ||
| 63 | ######################################################## | ||
| 64 | |||
| 65 | =head2 getTracks() | ||
| 66 | |||
| 67 | my $tracks = $cd->getTracks() | ||
| 68 | |||
| 69 | Returns an array reference of track names for the CD found from the details in the config given at object creation. | ||
| 70 | |||
| 71 | This method actually performs the lookup to the GNUDB database. | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | ######################################################## | ||
| 76 | sub getTracks{ | ||
| 77 | my $self = shift; | ||
| 78 | my @tracks = @{$self->{'__tracks'}}; | ||
| 79 | if($#tracks == -1){ | ||
| 80 | my $connectionConfig = { | ||
| 81 | client_name => ref($self), | ||
| 82 | client_version => 1.0, | ||
| 83 | protocol => "HTTP", | ||
| 84 | freedb_host => "gnudb.gnudb.org" | ||
| 85 | }; | ||
| 86 | my $conn = Net::FreeDB2->connection($connectionConfig); | ||
| 87 | my $matchConfig = { | ||
| 88 | "categ" => $self->getGenre(), | ||
| 89 | "discid" => $self->getId() | ||
| 90 | }; | ||
| 91 | my $match = Net::FreeDB2::Match->new($matchConfig); | ||
| 92 | my $res = $conn->read($match); | ||
| 93 | if($res->hasError()){ | ||
| 94 | confess('Error quering GNUDB'); | ||
| 95 | } | ||
| 96 | else{ #all ok | ||
| 97 | my $entry = $res->getEntry(); | ||
| 98 | @tracks = $entry->getTtitlen(0); #get all tracks; | ||
| 99 | $self->{'__tracks'} = \@tracks; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | return @tracks; | ||
| 103 | } | ||
| 104 | ######################################################### | ||
| 105 | |||
| 106 | =head2 getId() | ||
| 107 | |||
| 108 | my $id = $cd->getId() | ||
| 109 | |||
| 110 | Returns the same ID string as given in the config on object creation. | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | ######################################################### | ||
| 115 | sub getId{ | ||
| 116 | my $self = shift; | ||
| 117 | return $self->{'__id'}; | ||
| 118 | } | ||
| 119 | ######################################################### | ||
| 120 | |||
| 121 | =head2 getGenre() | ||
| 122 | |||
| 123 | my $genre = $cd->getGenre() | ||
| 124 | |||
| 125 | Returns the same genre string as given in the config on object creation. | ||
| 126 | |||
| 127 | =cut | ||
| 128 | |||
| 129 | ######################################################### | ||
| 130 | sub getGenre{ | ||
| 131 | my $self = shift; | ||
| 132 | return $self->{'__genre'}; | ||
| 133 | } | ||
| 134 | ######################################################### | ||
| 135 | sub __setId{ | ||
| 136 | my($self, $id) = @_; | ||
| 137 | if(defined($id)){ | ||
| 138 | if($id =~ m/^[0-9a-fA-F]+$/){ | ||
| 139 | $self->{'__id'} = $id; | ||
| 140 | return 1; | ||
| 141 | } | ||
| 142 | else{ | ||
| 143 | confess("Invalid ID"); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | else{ | ||
| 147 | confess("No ID given"); | ||
| 148 | } | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | ######################################################### | ||
| 152 | sub __setGenre{ | ||
| 153 | my($self, $genre) = @_; | ||
| 154 | if(defined($genre)){ | ||
| 155 | $self->{'__genre'} = $genre; | ||
| 156 | return 1; | ||
| 157 | } | ||
| 158 | else{ | ||
| 159 | confess("No genre given"); | ||
| 160 | } | ||
| 161 | return 0; | ||
| 162 | } | ||
| 163 | ######################################################### | ||
| 164 | |||
| 165 | =pod | ||
| 166 | |||
| 167 | =head1 Author | ||
| 168 | |||
| 169 | MacGyveR <dumb@cpan.org> | ||
| 170 | |||
| 171 | Development questions, bug reports, and patches are welcome to the above address. | ||
| 172 | |||
| 173 | =head1 Copyright | ||
| 174 | |||
| 175 | Copyright (c) 2012 MacGyveR. All rights reserved. | ||
| 176 | |||
| 177 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 178 | |||
| 179 | =head1 See Also | ||
| 180 | |||
| 181 | L<Net::GNUDBSearch> | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | ######################################################### | ||
| 186 | return 1; |