Skip to content

Commit

Permalink
Merge pull request #3 from richardleach/master
Browse files Browse the repository at this point in the history
Update API Response Model
  • Loading branch information
davorg committed Oct 5, 2018
2 parents c3360dd + 64ef22b commit 1ead38b
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 37 deletions.
44 changes: 43 additions & 1 deletion lib/Net/Songkick/Artist.pm
Expand Up @@ -12,14 +12,56 @@ use warnings;
use Moose::Util::TypeConstraints;
use Moose;

use Net::Songkick::MusicBrainz;
use Net::Songkick::Types;

coerce 'Net::Songkick::Artist',
from 'HashRef',
via { Net::Songkick::Artist->new($_) };

has $_ => (
is => 'ro',
isa => 'Str',
) for qw[id displayName];
) for qw[id displayName uri];

has identifier => (
is => 'ro',
isa => 'ArrayRef[Net::Songkick::MusicBrainz]',
);

has onTourUntil => (
is => 'ro',
isa => 'Net::Songkick::DateTime',
coerce => 1,
);

around BUILDARGS => sub {
my $orig = shift;
my $class = shift;

my %args;

if (@_ == 1) {
%args = %{$_[0]};
} else {
%args = @_;
}

if (exists $args{identifier}) {
foreach (@{$args{identifier}}) {
if (ref $_ ne 'Net::Songkick::MusicBrainz') {
$_ = Net::Songkick::MusicBrainz->new($_);
}
}
}

if (exists $args{onTourUntil}) {
$args{onTourUntil} = { date => $args{onTourUntil} };
}


$class->$orig(\%args);
};

no Moose;
__PACKAGE__->meta->make_immutable;
Expand Down
51 changes: 51 additions & 0 deletions lib/Net/Songkick/City.pm
@@ -0,0 +1,51 @@
=head1 NAME
Net::Songkick::City - Models a city in the Songkick API
=cut

package Net::Songkick::City;

use strict;
use warnings;
use Moose;
use Moose::Util::TypeConstraints;

use Net::Songkick::Country;

coerce 'Net::Songkick::City',
from 'HashRef',
via { Net::Songkick::City->new($_) };

has $_ => (
is => 'ro',
isa => 'Str',
) for qw[displayName id uri];

has country => (
is => 'ro',
isa => 'Net::Songkick::Country',
coerce => 1,
);

no Moose;
__PACKAGE__->meta->make_immutable;

=head1 AUTHOR
Dave Cross <dave@mag-sol.com>
=head1 SEE ALSO
perl(1), L<http://www.songkick.com/>, L<http://developer.songkick.com/>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2018, Magnum Solutions Ltd. All Rights Reserved.
This script is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut

1;
10 changes: 8 additions & 2 deletions lib/Net/Songkick/Event.pm
Expand Up @@ -20,7 +20,7 @@ use Net::Songkick::Venue;
has $_ => (
is => 'ro',
isa => 'Str',
) for qw(type status uri displayName popularity id);
) for qw(type status uri displayName popularity id ageRestriction);

has location => (
is => 'ro',
Expand All @@ -39,6 +39,12 @@ has start => (
coerce => 1,
);

has end => (
is => 'ro',
isa => 'Net::Songkick::DateTime',
coerce => 1,
);

has venue => (
is => 'ro',
isa => 'Net::Songkick::Venue',
Expand Down Expand Up @@ -68,7 +74,7 @@ around BUILDARGS => sub {
}
}
}

$class->$orig(\%args);
};

Expand Down
43 changes: 43 additions & 0 deletions lib/Net/Songkick/MusicBrainz.pm
@@ -0,0 +1,43 @@
=head1 NAME
Net::Songkick::City - Models a MusicBrainz identifier in the Songkick API
=cut

package Net::Songkick::MusicBrainz;

use strict;
use warnings;
use Moose;
use Moose::Util::TypeConstraints;

coerce 'Net::Songkick::MusicBrainz',
from 'HashRef',
via { Net::Songkick::MusicBrainz->new($_) };

has $_ => (
is => 'ro',
isa => 'Str',
) for qw[href mbid];

no Moose;
__PACKAGE__->meta->make_immutable;

=head1 AUTHOR
Dave Cross <dave@mag-sol.com>
=head1 SEE ALSO
perl(1), L<http://www.songkick.com/>, L<http://developer.songkick.com/>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2018, Magnum Solutions Ltd. All Rights Reserved.
This script is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut

1;
14 changes: 8 additions & 6 deletions lib/Net/Songkick/Types.pm
Expand Up @@ -21,14 +21,16 @@ subtype 'Net::Songkick::DateTime',
coerce 'Net::Songkick::DateTime',
from 'HashRef',
via {
my $dt = DateTime::Format::Strptime->new(
my $dt = ( exists($_->{datetime}) ) ?

DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S%z',
)->parse_datetime($_->{datetime});
if (!$dt) {
$dt = DateTime::Format::Strptime->new(
)->parse_datetime($_->{datetime}) :

DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d',
)->parse_datetime($_->{date});
}
)->parse_datetime($_->{date}) ;

return $dt;
};

Expand Down
9 changes: 8 additions & 1 deletion lib/Net/Songkick/Venue.pm
Expand Up @@ -12,6 +12,7 @@ use warnings;
use Moose;
use Moose::Util::TypeConstraints;

use Net::Songkick::City;
use Net::Songkick::MetroArea;

coerce 'Net::Songkick::Venue',
Expand All @@ -21,7 +22,13 @@ coerce 'Net::Songkick::Venue',
has $_ => (
is => 'ro',
isa => 'Str',
) for qw[uri lat id lng displayName];
) for qw[uri lat id lng displayName street zip phone website capacity description];

has city => (
is => 'ro',
isa => 'Net::Songkick::City',
coerce => 1,
);

has metroArea => (
is => 'ro',
Expand Down
59 changes: 59 additions & 0 deletions t/05-instantiation.t
Expand Up @@ -3,8 +3,11 @@ use warnings;

use Test::More;

use Net::Songkick::Artist;
use Net::Songkick::Country;
use Net::Songkick::MetroArea;
use Net::Songkick::MusicBrainz;
use Net::Songkick::Types;
use Net::Songkick::Venue;

my $country = Net::Songkick::Country->new({ displayName => 'UK' });
Expand All @@ -27,6 +30,23 @@ isa_ok($metro->country, 'Net::Songkick::Country');
is($metro->country->displayName, 'UK',
'Metro area is in the correct country');

my $city = Net::Songkick::City->new({
id => '24426',
displayName => 'London',
uri => 'http://www.songkick.com/metro_areas/24426-uk-london',
country => $country
});

ok($city, 'Got an object');
isa_ok($city, 'Net::Songkick::City');
is($city->id, '24426', 'City has correct id');
is($city->displayName, 'London', 'City has correct name');
is($city->uri,'http://www.songkick.com/metro_areas/24426-uk-london',
'City has correct URI');
isa_ok($city->country, 'Net::Songkick::Country');
is($city->country->displayName, 'UK',
'City is in the correct country');

my $venue = Net::Songkick::Venue->new({
id => 17_522,
displayName => 'O2 Brixton Academy',
Expand Down Expand Up @@ -62,5 +82,44 @@ isa_ok($venue->metroArea, 'Net::Songkick::MetroArea');
is($venue->metroArea->displayName, 'London',
'Metro area is in the correct Metro Area');

my $identifier = Net::Songkick::MusicBrainz->new({
href => 'http://api.songkick.com/api/3.0/artists/mbid:a74b1b7f-71a5-4011-9441-d0b5e4122711.json',
mbid => 'a74b1b7f-71a5-4011-9441-d0b5e4122711'
});
ok($identifier, 'Got a MusicBrainz array');
isa_ok($identifier, 'Net::Songkick::MusicBrainz');
is($identifier->href,
'http://api.songkick.com/api/3.0/artists/mbid:a74b1b7f-71a5-4011-9441-d0b5e4122711.json',
'Identifier has the correct href');
is($identifier->mbid, 'a74b1b7f-71a5-4011-9441-d0b5e4122711',
'Identifier has the correct mbid');


my $artist = Net::Songkick::Artist->new({
id => '253846',
displayName => 'Radiohead',
uri => 'http://www.songkick.com/artists/253846-radiohead?utm_source=45852&utm_medium=partner',
identifier => [$identifier],
onTourUntil => '2018-04-25'
});
ok($artist, 'Got an artist');
isa_ok($artist, 'Net::Songkick::Artist');
is($artist->id, '253846', 'Got the right artist');
is($artist->displayName, 'Radiohead', 'Artist has the correct name');
is($artist->uri,
'http://www.songkick.com/artists/253846-radiohead?utm_source=45852&utm_medium=partner',
'Artist has the correct URI');
isa_ok($artist->onTourUntil, 'DateTime');
is( ''. $artist->onTourUntil,
''. DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d',
)->parse_datetime('2018-04-25'),
'Artist is on tour');
isa_ok($artist->identifier->[0], 'Net::Songkick::MusicBrainz');
is($artist->identifier->[0]->mbid,
'a74b1b7f-71a5-4011-9441-d0b5e4122711',
'Artist has the correct identifier'
);


done_testing;

0 comments on commit 1ead38b

Please sign in to comment.