Skip to content

Commit

Permalink
applied perltidy to make source code formatting pretty
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbargsten committed Jan 30, 2018
1 parent 70878ad commit 80920e3
Showing 1 changed file with 89 additions and 43 deletions.
132 changes: 89 additions & 43 deletions lib/WebService/MusicBrainz.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ use WebService::MusicBrainz::Request;
our $VERSION = '1.0.3';

has 'request';
has valid_resources => sub { ['area','artist','discid','label','recording','release','release_group'] };
has valid_resources =>
sub { [ 'area', 'artist', 'discid', 'label', 'recording', 'release', 'release_group' ] };
has relationships => sub {
my $rels = ['area-rels','artist-rels','event-rels','instrument-rels','label-rels','place-rels','recording-rels','release-rels','release-group-rels','series-rels','url-rels','work-rels'];
my $rels = [
'area-rels', 'artist-rels', 'event-rels', 'instrument-rels',
'label-rels', 'place-rels', 'recording-rels', 'release-rels',
'release-group-rels', 'series-rels', 'url-rels', 'work-rels'
];
return $rels;
};

# inc subqueries
our %subquery_map = (
_modifiers => [qw(discids media isrcs artist-credits various-artists)],
_misc => [qw(aliases annotation tags ratings user-tags user-ratings)],
artist => [qw(recordings releases release-groups works) ],
_modifiers => [qw(discids media isrcs artist-credits various-artists)],
_misc => [qw(aliases annotation tags ratings user-tags user-ratings)],
artist => [qw(recordings releases release-groups works)],
discid => [qw(artists artist-credits collections labels recordings release-groups)],
label => [qw(releases)],
recording => [qw(artists releases)],
Expand All @@ -28,86 +33,127 @@ our %subquery_map = (
has search_fields_by_resource => sub {
my %search_fields;

$search_fields{area} = ['aid','alias','area','begin','comment','end','ended','sortname','iso','iso1','iso2','iso3','type'];
$search_fields{artist} = ['area','beginarea','endarea','arid','artist','artistaccent','alias','begin','comment','country','end','ended','gender','ipi','sortname','tag','type'];
$search_fields{discid} = ['discid'];
$search_fields{label} = ['alias','area','begin','code','comment','country','end','ended','ipi','label','labelaccent','laid','sortname','type','tag'];
$search_fields{recording} = ['arid','artist','artistname','creditname','comment','country','date','dur','format','isrc','number','position','primarytype','puid','qdur','recording','recordingaccent','reid','release','rgid','rid','secondarytype','status','tid','trnum','tracks','tracksrelease','tag','type','video'];
$search_fields{release_group} = ['arid','artist','comment','creditname','primarytype','rgid','releasegroup','releasegroupaccent','releases','release','reid','secondarytype','status','tag','type'];
$search_fields{release} = ['arid','artist','artistname','asin','barcode','catno','comment','country','creditname','date','discids','discidsmedium','format','laid','label','lang','mediums','primarytype','puid','quality','reid','release','releaseaccent','rgid','script','secondarytype','status','tag','tracks','tracksmedium','type'];
$search_fields{work} = ['alias','arid','artist','comment','iswc','lang','tag','type','wid','work','workaccent'];
$search_fields{area} = [
'aid', 'alias', 'area', 'begin', 'comment', 'end', 'ended', 'sortname',
'iso', 'iso1', 'iso2', 'iso3', 'type'
];
$search_fields{artist} = [
'area', 'beginarea', 'endarea', 'arid', 'artist', 'artistaccent',
'alias', 'begin', 'comment', 'country', 'end', 'ended',
'gender', 'ipi', 'sortname', 'tag', 'type'
];
$search_fields{discid} = ['discid'];
$search_fields{label} = [
'alias', 'area', 'begin', 'code', 'comment', 'country',
'end', 'ended', 'ipi', 'label', 'labelaccent', 'laid',
'sortname', 'type', 'tag'
];
$search_fields{recording} = [
'arid', 'artist', 'artistname', 'creditname',
'comment', 'country', 'date', 'dur',
'format', 'isrc', 'number', 'position',
'primarytype', 'puid', 'qdur', 'recording',
'recordingaccent', 'reid', 'release', 'rgid',
'rid', 'secondarytype', 'status', 'tid',
'trnum', 'tracks', 'tracksrelease', 'tag',
'type', 'video'
];
$search_fields{release_group} = [
'arid', 'artist', 'comment', 'creditname',
'primarytype', 'rgid', 'releasegroup', 'releasegroupaccent',
'releases', 'release', 'reid', 'secondarytype',
'status', 'tag', 'type'
];
$search_fields{release} = [
'arid', 'artist', 'artistname', 'asin',
'barcode', 'catno', 'comment', 'country',
'creditname', 'date', 'discids', 'discidsmedium',
'format', 'laid', 'label', 'lang',
'mediums', 'primarytype', 'puid', 'quality',
'reid', 'release', 'releaseaccent', 'rgid',
'script', 'secondarytype', 'status', 'tag',
'tracks', 'tracksmedium', 'type'
];
$search_fields{work}
= [ 'alias', 'arid', 'artist', 'comment', 'iswc', 'lang', 'tag', 'type', 'wid', 'work',
'workaccent' ];

return \%search_fields;
};

sub is_valid_subquery {
my $self = shift;
my $resource = shift;
my $self = shift;
my $resource = shift;
my $subqueries = shift;

return unless($resource);
return unless ($resource);

my $resource_map = $subquery_map{$resource};
return if(!$resource_map);
return if ( !$resource_map );

my %valid_fields = map { $_ => 1 } (@$resource_map, @{$subquery_map{_modifiers}}, @{$subquery_map{_misc}}, @{$self->relationships});
my %valid_fields = map { $_ => 1 } (
@$resource_map,
@{ $subquery_map{_modifiers} },
@{ $subquery_map{_misc} },
@{ $self->relationships }
);

foreach my $subquery (@$subqueries) {
return if(!$valid_fields{$subquery});
return if ( !$valid_fields{$subquery} );
}

return 1;
};
}

sub search {
my $self = shift;
my $self = shift;
my $search_resource = shift;
my $search_query = shift;
my $search_query = shift;

$self->request(WebService::MusicBrainz::Request->new());
$self->request( WebService::MusicBrainz::Request->new() );

if(!grep /^$search_resource$/, @{ $self->valid_resources() }) {
if ( !grep /^$search_resource$/, @{ $self->valid_resources() } ) {
die "Not a valid resource for search ($search_resource)";
}

$self->request()->search_resource($search_resource);

if(exists $search_query->{mbid}) {
$self->request()->mbid($search_query->{mbid});
if ( exists $search_query->{mbid} ) {
$self->request()->mbid( $search_query->{mbid} );
delete $search_query->{mbid};
}

if(exists $search_query->{discid}) {
$self->request()->discid($search_query->{discid});
if ( exists $search_query->{discid} ) {
$self->request()->discid( $search_query->{discid} );
delete $search_query->{discid};
}

my $inc_subqueries = delete $search_query->{inc};
# only use "inc" parameters when a specific MBID or DISCID is given
if ( ( $self->request()->mbid() or $self->request()->discid() ) and $inc_subqueries ) {
$inc_subqueries = [$inc_subqueries] if(!ref $inc_subqueries);

if($self->is_valid_subquery($search_resource, $inc_subqueries)) {
$self->request()->inc($inc_subqueries);
} else {
my $subquery_str = join ", ", @$inc_subqueries;
die "Not a valid \"inc\" subquery ($subquery_str) for resource: $search_resource";
}
$inc_subqueries = [$inc_subqueries] if ( !ref $inc_subqueries );

if ( $self->is_valid_subquery( $search_resource, $inc_subqueries ) ) {
$self->request()->inc($inc_subqueries);
} else {
my $subquery_str = join ", ", @$inc_subqueries;
die "Not a valid \"inc\" subquery ($subquery_str) for resource: $search_resource";
}
}

if(exists $search_query->{fmt}) {
$self->request()->format($search_query->{fmt});
if ( exists $search_query->{fmt} ) {
$self->request()->format( $search_query->{fmt} );
delete $search_query->{fmt};
}

if(exists $search_query->{offset}) {
$self->request()->offset($search_query->{offset});
if ( exists $search_query->{offset} ) {
$self->request()->offset( $search_query->{offset} );
delete $search_query->{offset};
}

foreach my $search_field (keys %{ $search_query }) {
if(! grep /^$search_field$/, @{ $self->search_fields_by_resource->{$search_resource} }) {
die "Not a valid search field ($search_field) for resource \"$search_resource\"";
foreach my $search_field ( keys %{$search_query} ) {
if ( !grep /^$search_field$/, @{ $self->search_fields_by_resource->{$search_resource} } ) {
die "Not a valid search field ($search_field) for resource \"$search_resource\"";
}
}

Expand Down

0 comments on commit 80920e3

Please sign in to comment.