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.0302';
7              
8 1     1   1082 use Moo;
  1         8697  
  1         5  
9 1     1   1665 use strictures 2;
  1         1341  
  1         32  
10 1     1   573 use namespace::clean;
  1         7886  
  1         6  
11              
12 1     1   231 use Carp;
  1         2  
  1         59  
13 1     1   6 use Mojo::UserAgent;
  1         2  
  1         14  
14 1     1   403 use Mojo::JSON::MaybeXS;
  1         677  
  1         28  
15 1     1   5 use Mojo::JSON qw( decode_json );
  1         2  
  1         49  
16 1     1   7 use Mojo::URL;
  1         2  
  1         9  
17 1     1   42 use Try::Tiny;
  1         1  
  1         479  
18              
19              
20             has api_key => (
21             is => 'ro',
22             required => 1,
23             );
24              
25              
26             has base => (
27             is => 'rw',
28             default => sub { Mojo::URL->new('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 25848 my ($self) = @_;
60              
61 2         6 my $type;
62             my $lookup;
63 2         0 my $id;
64              
65 2 100 66     25 if ( $self->artist && $self->song ) {
    50 33        
    50 33        
66 1         2 $type = 'both';
67 1         5 $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       14 croak "Can't fetch: No type set"
80             unless $type;
81              
82 1         3 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 .= 'api_key=' . $self->api_key . "&id=$id";
88             }
89             else {
90 1         2 $path .= '/search/';
91 1         6 $query .= 'api_key=' . $self->api_key
92             . "&type=$type"
93             . "&lookup=$lookup";
94             }
95              
96 1         5 my $url = Mojo::URL->new($self->base)->path($path)->query($query);
97              
98 1         390 my $tx = $self->ua->get($url);
99              
100 1         13417 my $data = _handle_response($tx);
101              
102 1         4 return $data;
103             }
104              
105             sub _handle_response {
106 1     1   3 my ($tx) = @_;
107              
108 1         2 my $data;
109              
110 1         8 my $res = $tx->result;
111              
112 1 50       23 if ( $res->is_success ) {
113 1         17 my $body = $res->body;
114             try {
115 1     1   86 $data = decode_json($body);
116             }
117             catch {
118 0     0   0 croak $body, "\n";
119 1         39 };
120             }
121             else {
122 0         0 croak "Connection error: ", $res->message;
123             }
124              
125 1         23 return $data;
126             }
127              
128             1;
129              
130             __END__