File Coverage

blib/lib/WebService/GetSongBPM.pm
Criterion Covered Total %
statement 48 58 82.7
branch 8 12 66.6
condition 5 12 41.6
subroutine 11 12 91.6
pod 1 1 100.0
total 73 95 76.8


line stmt bran cond sub pod time code
1             package WebService::GetSongBPM;
2             our $AUTHORITY = 'cpan:GENE';
3              
4             # ABSTRACT: Access to the getsongbpm.com API
5              
6             our $VERSION = '0.0401';
7              
8 1     1   1023 use strictures 2;
  1         1406  
  1         37  
9 1     1   188 use Carp qw(croak);
  1         1  
  1         41  
10 1     1   5 use Mojo::UserAgent ();
  1         2  
  1         16  
11 1     1   4 use Mojo::JSON qw(decode_json);
  1         2  
  1         37  
12 1     1   5 use Mojo::URL ();
  1         3  
  1         12  
13 1     1   450 use Moo;
  1         10703  
  1         5  
14 1     1   1652 use Try::Tiny;
  1         2  
  1         51  
15 1     1   443 use namespace::clean;
  1         8361  
  1         8  
16              
17              
18             has api_key => (
19             is => 'ro',
20             required => 1,
21             );
22              
23              
24             has base => (
25             is => 'rw',
26             default => sub { 'https://api.getsongbpm.com' },
27             );
28              
29              
30             has artist => (
31             is => 'ro',
32             );
33              
34              
35             has artist_id => (
36             is => 'ro',
37             );
38              
39              
40             has song => (
41             is => 'ro',
42             );
43              
44              
45             has song_id => (
46             is => 'ro',
47             );
48              
49              
50             has ua => (
51             is => 'rw',
52             default => sub { Mojo::UserAgent->new() },
53             );
54              
55              
56             sub fetch {
57 2     2 1 26313 my ($self) = @_;
58              
59 2         21 my $type;
60             my $lookup;
61 2         0 my $id;
62              
63 2 100 66     30 if ( $self->artist && $self->song ) {
    50 33        
    50 33        
64 1         3 $type = 'both';
65 1         7 $lookup = 'song:' . $self->song . '+artist:' . $self->artist;
66             }
67             elsif ( $self->artist or $self->artist_id ) {
68 0         0 $type = 'artist';
69 0         0 $lookup = $self->artist;
70 0         0 $id = $self->artist_id;
71             }
72             elsif ( $self->song or $self->song_id ) {
73 0         0 $type = 'song';
74 0         0 $lookup = $self->song;
75 0         0 $id = $self->song_id;
76             }
77 2 100       14 croak "Can't fetch: No type set"
78             unless $type;
79              
80 1         4 my $path = '';
81 1         2 my $query = {};
82              
83 1 50 33     9 if ( $self->artist_id or $self->song_id ) {
84 0         0 $path .= $type;
85 0         0 $query = {
86             api_key => $self->api_key,
87             id => $id,
88             };
89             }
90             else {
91 1         2 $path .= 'search';
92 1         8 $query = {
93             api_key => $self->api_key,
94             type => $type,
95             lookup => $lookup,
96             };
97             }
98              
99 1         9 my $url = Mojo::URL->new($self->base)->path($path)->query($query);
100              
101 1         554 my $tx = $self->ua->get($url);
102              
103 1         20512 my $data = _handle_response($tx);
104              
105 1         10 return $data;
106             }
107              
108             sub _handle_response {
109 1     1   3 my ($tx) = @_;
110              
111 1         1 my $data;
112              
113 1         8 my $res = $tx->result;
114              
115 1 50       26 if ( $res->is_success ) {
116 1         16 my $body = $res->body;
117             try {
118 1     1   121 $data = decode_json($body);
119             }
120             catch {
121 0     0   0 croak $body, "\n";
122 1         27 };
123             }
124             else {
125 0         0 croak "Connection error: ", $res->message;
126             }
127              
128 1         127 return $data;
129             }
130              
131             1;
132              
133             __END__