lib/Net/GNUDB/Cd.pm

filter: Statementstmt 89.6% 52 / 58 Branchbran 60.0% 6 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 88.1% 74 / 84 SCAR 18.1
CC
6
Cov
85%
__setId
3.7
getTracks
3.0
__setGenre
2.2
CRAP 6.1
« Build ↑ Summary blib/lib/Net/GNUDBSearch/Cd.pm »
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 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use Net::FreeDB2;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use Net::FreeDB2::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 4 ▶use Net::FreeDB2::Entry; #see bug: https://rt.cpan.org/Ticket/Display.html?id=69089
Subroutine BEGIN: called CC=1 SCAR=0.0
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 2 ▶ my($class, $config) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
53 2 my $self = {
54 '__id' => undef,
55 '__genre' => undef,
56 '__tracks' => []
57 };
58 2 bless $self, $class;
59 2 $self->__setId($config->{'id'});
60 2 $self->__setGenre($config->{'genre'});
61 2 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 4 ▶ my $self = shift;
Subroutine getTracks: called CC=3 SCAR=11.0
Pod documented
78 4 my @tracks = @{$self->{'__tracks'}};
79 4 ▶ if($#tracks == -1){
Branch: 1 branch
BranchTrueFalseTotal
if ($#tracks == -1) 2 2 4
80 2 my $connectionConfig = {
81 client_name => ref($self),
82 client_version => 1.0,
83 protocol => "HTTP",
84 freedb_host => "gnudb.gnudb.org"
85 };
86 2 my $conn = Net::FreeDB2->connection($connectionConfig);
87 2 my $matchConfig = {
88 "categ" => $self->getGenre(),
89 "discid" => $self->getId()
90 };
91 2 my $match = Net::FreeDB2::Match->new($matchConfig);
92 2 my $res = $conn->read($match);
93 2 ▶ if($res->hasError()){
Branch: 1 branch
BranchTrueFalseTotal
if ($res->hasError) { } 0 2 2
94 0 confess('Error quering GNUDB');
95 }
96 else{ #all ok
97 2 my $entry = $res->getEntry();
98 2 @tracks = $entry->getTtitlen(0); #get all tracks;
99 2 $self->{'__tracks'} = \@tracks;
100 }
101 }
102 4 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 4 ▶ my $self = shift;
Subroutine getId: called CC=1 SCAR=0.0
Pod documented
117 4 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 4 ▶ my $self = shift;
Subroutine getGenre: called CC=1 SCAR=0.0
Pod documented
132 4 return $self->{'__genre'};
133 }
134 #########################################################
135 sub __setId{
136 2 ▶ my($self, $id) = @_;
Subroutine __setId: called CC=3 SCAR=13.0
137 2 ▶ if(defined($id)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) { } 2 0 2
138 2 ▶ if($id =~ m/^[0-9a-fA-F]+$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($id =~ /^[0-9a-fA-F]+$/) { } 2 0 2
139 2 $self->{'__id'} = $id;
140 2 return 1;
141 }
142 else{
143 0 confess("Invalid ID");
144 }
145 }
146 else{
147 0 confess("No ID given");
148 }
149 0 return 0;
150 }
151 #########################################################
152 sub __setGenre{
153 2 ▶ my($self, $genre) = @_;
Subroutine __setGenre: called CC=2 SCAR=7.9
154 2 ▶ if(defined($genre)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $genre) { } 2 0 2
155 2 $self->{'__genre'} = $genre;
156 2 return 1;
157 }
158 else{
159 0 confess("No genre given");
160 }
161 0 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 4 return 1;
« Build ↑ Summary blib/lib/Net/GNUDBSearch/Cd.pm »