File Coverage

blib/lib/WWW/Discogs.pm
Criterion Covered Total %
statement 151 163 92.6
branch 52 70 74.2
condition 37 72 51.3
subroutine 23 23 100.0
pod 5 6 83.3
total 268 334 80.2


line stmt bran cond sub pod time code
1             package WWW::Discogs;
2              
3 6     6   930863 use strict;
  6         14  
  6         214  
4 6     6   30 use warnings;
  6         10  
  6         264  
5              
6 6     6   34 use LWP::UserAgent;
  6         17  
  6         127  
7 6     6   32 use URI;
  6         9  
  6         115  
8 6     6   30 use URI::Escape;
  6         8  
  6         413  
9 6     6   29 use Carp;
  6         7  
  6         315  
10 6     6   6420 use JSON::XS;
  6         42856  
  6         446  
11 6     6   52 use Data::Dumper;
  6         12  
  6         294  
12              
13 6     6   99 use 5.008;
  6         19  
  6         4348  
14             our $VERSION = '0.13';
15              
16             our @namespaces = qw ( Artist Release Label Search Master );
17              
18             my %query_params = (
19             artist => { releases => 0, },
20             release => { },
21             label => { releases => 0, },
22             search => { type => 'all', 'q' => '', page => '1', },
23             master => { },
24             );
25              
26             for (@namespaces) {
27             my $pkg = __PACKAGE__."::$_";
28             my $name = "\L$_";
29              
30 6 100 100 6 1 3741 my $namespace = eval qq{
  6 100 66 6 1 21  
  6 50 33 6 1 3032  
  6 100 66 6 1 3165  
  6 50 33 6 1 20  
  6 50 100 6   2779  
  6 100 66 7   3643  
  6 100 33 6   13  
  6 50 66 6   2761  
  6 100 33 7   3621  
  6 50 100     16  
  6 50 66     2887  
  6 100 66     3381  
  6 100 33     18  
  6 100 33     2758  
  6 50 100     5151  
  5 50 66     20  
  5 50 66     49  
  5 100 33     67  
  2 100 33     16  
  2 100 50     4  
  2 50 33     14  
  2 50 33     51  
  1 50 33     8  
  1 100 0     12  
  1 50       12  
  1 100       1269  
  1 100       8  
  1 50       180  
  1 50       7  
  1 50       123  
  1 0       18  
  0         0  
  7         5515  
  6         22  
  6         73  
  6         84  
  3         6  
  3         12  
  3         22  
  3         51  
  2         15  
  2         20  
  1         10  
  1         844  
  1         8  
  1         171  
  1         7  
  1         117  
  1         15  
  0         0  
  6         4915  
  5         19  
  5         80  
  5         51  
  3         5  
  3         5  
  3         62  
  1         8  
  1         8  
  1         11  
  1         12  
  1         437  
  1         51  
  1         178  
  1         9  
  1         118  
  1         15  
  0         0  
  6         5115  
  5         14  
  5         40  
  5         48  
  3         5  
  3         5  
  3         60  
  1         6  
  1         7  
  1         10  
  1         11  
  1         297  
  1         49  
  1         162  
  1         8  
  1         105  
  1         23  
  0         0  
  7         5710  
  6         28  
  6         63  
  6         45  
  6         34  
  4         48  
  1         7  
  1         14  
  1         8  
  1         11  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
31             use $pkg;
32              
33             sub $name {
34             Carp::croak
35             "Params for '$name' should be key/value pairs, not hash ref"
36             if ref(\$_[1]) eq 'HASH';
37             my (\$self, \%args) = \@_;
38             my \$id = \$args{id} || \$args{name} || '';
39              
40             Carp::croak "Call to '$name' missing required arguments."
41             if !\$id && '$name' =~ /artist|release|label|master/;
42             Carp::croak "No search query specified"
43             if '$name' eq 'search' && !exists \$args{q};
44             Carp::croak "Incorrect search query"
45             if '$name' eq 'search' && \$args{q} =~ /^\\s*\$/;
46             Carp::croak "id value for '$name' not a number"
47             if '$name' =~ /master|release/ && \$id !~ /^\\d+\$/;
48             Carp::croak "name value incorrect for '$name'"
49             if '$name' =~ /artist|label/ && \$id =~ /^\\s*\$/;
50              
51             my \$query_params = \$self->_get_query_params('$name', \%args);
52              
53             my \$res = \$self->_request(
54             path => (\$id =~ /^\\s*\$/) ? '$name' : '$name'."/\$id",
55             query => \$query_params,
56             );
57              
58             my \$json = JSON::XS::decode_json( \$res->decoded_content );
59             my \$class_data = \$json->{resp}->{'$name'};
60             \$class_data->{_uri} = \$res->base;
61             \$class_data->{_params} = \$query_params;
62              
63             if (\$json->{resp}->{status} == JSON::XS::true &&
64             defined \$json->{resp}->{'$name'}) {
65             return $pkg->new(\%{\$class_data});
66             }
67              
68             return undef;
69             }
70              
71             1;
72             };
73              
74             Carp::croak "Cannot create namespace $name: $@\n" if not $namespace;
75             }
76              
77             sub _get_query_params {
78 6     6   24 my ($self, $name, %args) = @_;
79 6         15 my %params = ();
80              
81 6         24 for (keys %args) {
82 8 100       39 if (exists $query_params{$name}->{$_}) {
83 3         26 $params{$_} = $args{$_};
84             } else {
85 5         19 delete $args{$_};
86             }
87             }
88              
89 6         17 %params = ( %{$query_params{$name}}, %params );
  6         39  
90              
91 6         166 return \%params;
92             }
93              
94              
95             sub new {
96 6     6 0 4550 my ($class, @args) = @_;
97 6         19 my $self = {};
98 6         18 bless $self, $class;
99 6         30 $self->_init(@args);
100              
101 6         20 return $self;
102             }
103              
104             sub _init {
105 6     6   14 my ($self, %args) = @_;
106 6   50     71 $self->{apiurl} = $args{apiurl} || 'http://api.discogs.com';
107 6         68 $self->{ua} = LWP::UserAgent->new;
108 6         1479 $self->{ua}->agent("WWW-Discogs/$VERSION +perl");
109 6         316 $self->{ua}->default_header(
110             'Accept-Encoding' => 'gzip, deflate',
111             );
112              
113 6         240 return $self;
114             }
115              
116             sub _request {
117 6     6   25 my ($self, %args) = @_;
118 6         44 my $path = $args{path};
119 6         15 my $query = $args{query};
120              
121 6         62 my $uri = URI->new(
122             $self->{'apiurl'},
123             'http',
124             );
125 6         75210 $uri->path($path);
126 6 100       1192 $uri->query_form($query) if keys %{$query};
  6         75  
127              
128 6         454 my $url = $uri->canonical->as_string;
129 6         1164 my $res = $self->{ua}->get($url);
130              
131 6 100       25946 Carp::croak join(
132             "\n",
133             "Request to $url failed: ",
134             $res->status_line, Dumper($res)
135             ) unless $res->is_success;
136              
137 4         192 return $res;
138             }
139              
140             1;
141             __END__