File Coverage

blib/lib/WebService/GetSongBPM.pm
Criterion Covered Total %
statement 51 61 83.6
branch 8 12 66.6
condition 5 12 41.6
subroutine 12 13 92.3
pod 1 1 100.0
total 77 99 77.7


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.0400';
7              
8 1     1   1275 use Moo;
  1         12291  
  1         5  
9 1     1   2049 use strictures 2;
  1         1648  
  1         41  
10 1     1   682 use namespace::clean;
  1         9738  
  1         7  
11              
12 1     1   300 use Carp;
  1         2  
  1         56  
13 1     1   6 use Mojo::UserAgent;
  1         2  
  1         18  
14 1     1   498 use Mojo::JSON::MaybeXS;
  1         868  
  1         35  
15 1     1   7 use Mojo::JSON qw( decode_json );
  1         2  
  1         46  
16 1     1   6 use Mojo::URL;
  1         3  
  1         10  
17 1     1   45 use Try::Tiny;
  1         2  
  1         588  
18              
19              
20             has api_key => (
21             is => 'ro',
22             required => 1,
23             );
24              
25              
26             has base => (
27             is => 'rw',
28             default => sub { 'https://api.getsongbpm.com' },
29             );
30              
31              
32             has artist => (
33             is => 'ro',
34             );
35              
36              
37             has artist_id => (
38             is => 'ro',
39             );
40              
41              
42             has song => (
43             is => 'ro',
44             );
45              
46              
47             has song_id => (
48             is => 'ro',
49             );
50              
51              
52             has ua => (
53             is => 'rw',
54             default => sub { Mojo::UserAgent->new() },
55             );
56              
57              
58             sub fetch {
59 2     2 1 25993 my ($self) = @_;
60              
61 2         7 my $type;
62             my $lookup;
63 2         0 my $id;
64              
65 2 100 66     21 if ( $self->artist && $self->song ) {
    50 33        
    50 33        
66 1         3 $type = 'both';
67 1         6 $lookup = 'song:' . $self->song . '+artist:' . $self->artist;
68             }
69             elsif ( $self->artist or $self->artist_id ) {
70 0         0 $type = 'artist';
71 0         0 $lookup = $self->artist;
72 0         0 $id = $self->artist_id;
73             }
74             elsif ( $self->song or $self->song_id ) {
75 0         0 $type = 'song';
76 0         0 $lookup = $self->song;
77 0         0 $id = $self->song_id;
78             }
79 2 100       15 croak "Can't fetch: No type set"
80             unless $type;
81              
82 1         2 my $path = '';
83 1         2 my $query = {};
84              
85 1 50 33     7 if ( $self->artist_id or $self->song_id ) {
86 0         0 $path .= $type;
87 0         0 $query = {
88             api_key => $self->api_key,
89             id => $id,
90             };
91             }
92             else {
93 1         3 $path .= 'search';
94 1         5 $query = {
95             api_key => $self->api_key,
96             type => $type,
97             lookup => $lookup,
98             };
99             }
100              
101 1         9 my $url = Mojo::URL->new($self->base)->path($path)->query($query);
102              
103 1         673 my $tx = $self->ua->get($url);
104              
105 1         22263 my $data = _handle_response($tx);
106              
107 1         6 return $data;
108             }
109              
110             sub _handle_response {
111 1     1   3 my ($tx) = @_;
112              
113 1         2 my $data;
114              
115 1         8 my $res = $tx->result;
116              
117 1 50       29 if ( $res->is_success ) {
118 1         21 my $body = $res->body;
119             try {
120 1     1   141 $data = decode_json($body);
121             }
122             catch {
123 0     0   0 croak $body, "\n";
124 1         26 };
125             }
126             else {
127 0         0 croak "Connection error: ", $res->message;
128             }
129              
130 1         26 return $data;
131             }
132              
133             1;
134              
135             __END__