| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::LastFM::TrackInfo; | 
| 2 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:GENE'; | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | # ABSTRACT: Access to the track.getInfo slice of the LastFM API | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = '0.0204'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 1918 | use Moo; | 
|  | 1 |  |  |  |  | 8583 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 9 | 1 |  |  | 1 |  | 1572 | use strictures 2; | 
|  | 1 |  |  |  |  | 1354 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 10 | 1 |  |  | 1 |  | 580 | use namespace::clean; | 
|  | 1 |  |  |  |  | 9114 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  | 1 |  | 231 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 52 |  | 
| 13 | 1 |  |  | 1 |  | 7 | use Mojo::UserAgent; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 12 |  | 
| 14 | 1 |  |  | 1 |  | 45 | use Mojo::JSON qw(decode_json); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 15 | 1 |  |  | 1 |  | 4 | use Mojo::URL; | 
|  | 1 |  |  |  |  | 6 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 16 | 1 |  |  | 1 |  | 37 | use Try::Tiny; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 450 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | has api_key => ( | 
| 20 |  |  |  |  |  |  | is       => 'ro', | 
| 21 |  |  |  |  |  |  | required => 1, | 
| 22 |  |  |  |  |  |  | ); | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | has method => ( | 
| 26 |  |  |  |  |  |  | is      => 'ro', | 
| 27 |  |  |  |  |  |  | default => sub { 'track.getInfo' }, | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | has format => ( | 
| 32 |  |  |  |  |  |  | is      => 'ro', | 
| 33 |  |  |  |  |  |  | default => sub { 'json' }, | 
| 34 |  |  |  |  |  |  | ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | has base => ( | 
| 38 |  |  |  |  |  |  | is      => 'rw', | 
| 39 |  |  |  |  |  |  | default => sub { 'http://ws.audioscrobbler.com' }, | 
| 40 |  |  |  |  |  |  | ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | has version => ( | 
| 44 |  |  |  |  |  |  | is      => 'ro', | 
| 45 |  |  |  |  |  |  | default => sub { '2.0' }, | 
| 46 |  |  |  |  |  |  | ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | has ua => ( | 
| 50 |  |  |  |  |  |  | is      => 'rw', | 
| 51 |  |  |  |  |  |  | default => sub { Mojo::UserAgent->new }, | 
| 52 |  |  |  |  |  |  | ); | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | sub fetch { | 
| 56 | 4 |  |  | 4 | 1 | 22102 | my ( $self, %args ) = @_; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 4 | 100 |  |  |  | 19 | croak 'No artist provided' unless $args{artist}; | 
| 59 | 3 | 100 | 100 |  |  | 22 | croak 'No track provided' if $self->method eq 'track.getInfo' && !$args{track}; | 
| 60 | 2 | 100 | 66 |  |  | 26 | croak 'No album provided' if $self->method eq 'album.getInfo' && !$args{album}; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 1 |  |  |  |  | 9 | my $url = Mojo::URL->new($self->base) | 
| 63 |  |  |  |  |  |  | ->path($self->version) | 
| 64 |  |  |  |  |  |  | ->query( | 
| 65 |  |  |  |  |  |  | %args, | 
| 66 |  |  |  |  |  |  | api_key => $self->api_key, | 
| 67 |  |  |  |  |  |  | method  => $self->method, | 
| 68 |  |  |  |  |  |  | format  => $self->format, | 
| 69 |  |  |  |  |  |  | ); | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 1 |  |  |  |  | 445 | my $tx = $self->ua->get($url); | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 1 |  |  |  |  | 19995 | my $data = $self->_handle_response($tx); | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 1 |  |  |  |  | 5 | return $data; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub _handle_response { | 
| 79 | 1 |  |  | 1 |  | 2 | my ($self, $tx) = @_; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 1 |  |  |  |  | 2 | my $data; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 1 |  |  |  |  | 6 | my $res = $tx->result; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 1 | 50 |  |  |  | 23 | if ( $res->is_success ) { | 
| 86 | 1 |  |  |  |  | 17 | my $body = $res->body; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 1 | 50 |  |  |  | 21 | if ($self->format eq 'json') { | 
| 89 |  |  |  |  |  |  | try { | 
| 90 | 1 |  |  | 1 |  | 95 | $data = decode_json($body); | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  | catch { | 
| 93 | 0 |  |  | 0 |  | 0 | croak $body, "\n"; | 
| 94 | 1 |  |  |  |  | 8 | }; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  | else { | 
| 97 | 0 |  |  |  |  | 0 | $data = $body; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  | else { | 
| 101 | 0 |  |  |  |  | 0 | croak 'Connection error: ', $res->message; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  |  | 
| 104 | 1 |  |  |  |  | 126 | return $data; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | 1; | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | __END__ |