[mb-commits] branch, data-nes, created. First parts of Data::NES, a perl wrapper around musicbrainz-data-service.
MusicBrainz Git Server <[email protected]> Tue, 18 Dec 2012 17:31:10 +0000
| Newsgroups | gmane.comp.audio.musicbrainz.cvs |
|---|---|
| Message-ID | <E1Tl10Y-0003vs-6c@wiley> |
The branch, data-nes has been created
at 361dc49621e0e8b77fd8c20689f05cc4facb58d5 (commit)
- Log -----------------------------------------------------------------
commit 361dc49621e0e8b77fd8c20689f05cc4facb58d5
Author: warp <[email protected]>
Date: Tue Dec 18 18:30:05 2012 +0100
First parts of Data::NES, a perl wrapper around musicbrainz-data-service.
diff --git a/lib/DBDefs/Default.pm b/lib/DBDefs/Default.pm
index 87c9e48..cd8c5a7 100644
--- a/lib/DBDefs/Default.pm
+++ b/lib/DBDefs/Default.pm
@@ -73,6 +73,8 @@ sub WEB_SERVER { "www.musicbrainz.example.com" }
sub LUCENE_SERVER { "search.musicbrainz.org" }
sub WEB_SERVER_USED_IN_EMAIL { my $self = shift; $self->WEB_SERVER }
+sub DATA_ACCESS_SERVICE { "http://localhost:8000" }
+
################################################################################
# Mail Settings
################################################################################
diff --git a/lib/MusicBrainz/Server/Data/NES/Artist.pm b/lib/MusicBrainz/Server/Data/NES/Artist.pm
new file mode 100644
index 0000000..6173230
--- /dev/null
+++ b/lib/MusicBrainz/Server/Data/NES/Artist.pm
@@ -0,0 +1,87 @@
+package MusicBrainz::Server::Data::NES::Artist;
+
+use utf8;
+use Moose;
+use namespace::autoclean;
+
+use MusicBrainz::Server::Entity::Artist;
+use MusicBrainz::Server::Data::NES::Utils qw( request );
+
+extends 'MusicBrainz::Server::Data::NES::CoreEntity';
+
+sub _entity_class
+{
+ return 'MusicBrainz::Server::Entity::Artist';
+}
+
+sub _field_mapping
+{
+ my $self = shift;
+
+ my $pd = $self->c->model ('NES::PartialDate');
+
+ return {
+ 'mbid' => 'gid',
+ 'country' => 'country_id',
+ 'type' => 'type_id',
+ 'gender' => 'gender_id',
+ 'sort-name' => 'sort_name',
+ 'begin-date' => undef,
+ 'end-date' => undef,
+ 'begin-date' => sub { return $pd->new_from_service (@_); },
+ 'end-date' => sub { return $pd->new_from_service (@_); },
+ };
+}
+
+sub create
+{
+ my ($self, $editor_id, @artists) = @_;
+
+ my $response = request ('/edit/open', {});
+ my $edit_id = $response->{ref};
+
+ my @created;
+ for my $artist (@artists)
+ {
+
+ my $response = request ('/artist/create', {
+ editor => 1, edit => $edit_id, artist => $artist });
+
+ push @created, $response->{ref};
+ }
+
+ return @artists > 1 ? @created : $created[0];
+}
+
+sub get_by_revision
+{
+ my ($self, $revision_id) = @_;
+
+ my $response = request ('/artist/view-revision', { revision => $revision_id });
+
+ return $self->new_from_response ($response);
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+1;
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 MetaBrainz Foundation
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+=cut
diff --git a/lib/MusicBrainz/Server/Data/NES/CoreEntity.pm b/lib/MusicBrainz/Server/Data/NES/CoreEntity.pm
new file mode 100644
index 0000000..3833c04
--- /dev/null
+++ b/lib/MusicBrainz/Server/Data/NES/CoreEntity.pm
@@ -0,0 +1,30 @@
+package MusicBrainz::Server::Data::NES::CoreEntity;
+use Moose;
+
+extends 'MusicBrainz::Server::Data::NES::Entity';
+
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+1;
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 MetaBrainz Foundation
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+=cut
+
diff --git a/lib/MusicBrainz/Server/Data/NES/Editor.pm b/lib/MusicBrainz/Server/Data/NES/Editor.pm
new file mode 100644
index 0000000..0e448c7
--- /dev/null
+++ b/lib/MusicBrainz/Server/Data/NES/Editor.pm
@@ -0,0 +1,51 @@
+package MusicBrainz::Server::Data::NES::Editor;
+use Moose;
+use namespace::autoclean;
+
+use MusicBrainz::Server::Entity::Editor;
+use MusicBrainz::Server::Data::NES::Utils qw( request );
+
+extends 'MusicBrainz::Server::Data::NES::CoreEntity';
+
+sub _entity_class
+{
+ return 'MusicBrainz::Server::Entity::Editor';
+}
+
+sub _field_mapping
+{
+ return { 'ref' => 'id' };
+}
+
+sub register
+{
+ my ($self, $editor) = @_;
+
+ my $response = request ('/editor/register', $editor);
+
+ return $self->new_from_response ($response);
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+1;
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 MetaBrainz Foundation
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+=cut
diff --git a/lib/MusicBrainz/Server/Data/NES/Entity.pm b/lib/MusicBrainz/Server/Data/NES/Entity.pm
new file mode 100644
index 0000000..2c96b07
--- /dev/null
+++ b/lib/MusicBrainz/Server/Data/NES/Entity.pm
@@ -0,0 +1,83 @@
+package MusicBrainz::Server::Data::NES::Entity;
+use Moose;
+
+with 'MusicBrainz::Server::Data::Role::Context';
+
+sub _entity_class
+{
+ die("Not implemented");
+}
+
+sub _field_mapping
+{
+ return {};
+}
+
+
+sub new_from_service
+{
+ my ($self, $response, $field) = @_;
+
+ return unless $response;
+
+ my %data = %{ $response->{$field} };
+ my %info;
+ my %mapping = %{$self->_field_mapping};
+
+ foreach my $key (keys %data)
+ {
+ my $attrib = $mapping{$key} // $key;
+ my $val;
+ if (ref($attrib) eq 'CODE') {
+ $val = $attrib->(\%data, $key);
+ }
+ elsif (defined $data{$key}) {
+ $val = $data{$key};
+ }
+ $info{$attrib} = $val if defined $val;
+ }
+
+ my $entity_class = $self->_entity_class(\%data);
+ Class::MOP::load_class($entity_class);
+
+ return $entity_class->new(%info);
+}
+
+sub new_from_response
+{
+ my ($self, $response) = @_;
+
+ return unless $response;
+
+ for my $key (grep { $_ ne 'data' } keys %$response)
+ {
+ $response->{data}->{$key} = $response->{$key};
+ }
+
+ return $self->new_from_service ($response, 'data');
+}
+
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+1;
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 MetaBrainz Foundation
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+=cut
diff --git a/lib/MusicBrainz/Server/Data/NES/PartialDate.pm b/lib/MusicBrainz/Server/Data/NES/PartialDate.pm
new file mode 100644
index 0000000..bc3b7a0
--- /dev/null
+++ b/lib/MusicBrainz/Server/Data/NES/PartialDate.pm
@@ -0,0 +1,33 @@
+package MusicBrainz::Server::Data::NES::PartialDate;
+use Moose;
+
+extends 'MusicBrainz::Server::Data::NES::Entity';
+
+sub _entity_class
+{
+ return 'MusicBrainz::Server::Entity::PartialDate';
+}
+
+__PACKAGE__->meta->make_immutable;
+no Moose;
+1;
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 MetaBrainz Foundation
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+=cut
diff --git a/lib/MusicBrainz/Server/Data/NES/Utils.pm b/lib/MusicBrainz/Server/Data/NES/Utils.pm
new file mode 100644
index 0000000..c99cc3f
--- /dev/null
+++ b/lib/MusicBrainz/Server/Data/NES/Utils.pm
@@ -0,0 +1,55 @@
+package MusicBrainz::Server::Data::NES::Utils;
+use Moose;
+
+use DBDefs;
+use Encode;
+use JSON;
+use LWP::UserAgent;
+
+use Sub::Exporter -setup => {
+ exports => [qw( request )]
+};
+
+sub model
+{
+ my $model_class = shift;
+
+ Class::MOP::load_class("MusicBrainz::Server::Data::NES::$model_class");
+ return $model_class->new ();
+}
+
+sub request
+{
+ my ($path, $body) = @_;
+
+ my $ua = LWP::UserAgent->new;
+
+ my $uri = DBDefs->DATA_ACCESS_SERVICE.$path;
+ my $content = to_json ($body, { pretty => 1, canonical => 1 });
+
+ my $response = $ua->post ($uri, Content => encode ('utf8', $content));
+
+ return decode_json ($response->content);
+}
+
+1;
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 MetaBrainz Foundation
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+=cut
diff --git a/t/lib/t/MusicBrainz/Server/Data/NES/Artist.pm b/t/lib/t/MusicBrainz/Server/Data/NES/Artist.pm
new file mode 100644
index 0000000..3098b6b
--- /dev/null
+++ b/t/lib/t/MusicBrainz/Server/Data/NES/Artist.pm
@@ -0,0 +1,64 @@
+package t::MusicBrainz::Server::Data::NES::Artist;
+
+use utf8;
+use Test::Routine;
+use Test::More;
+use MusicBrainz::Server::Data::NES::Artist;
+
+with 't::Context';
+
+test 'Create artist' => sub {
+ my $test = shift;
+
+ my $editor = $test->c->model ('NES::Editor')->register (
+ { 'name' => 'Bulbasaur', 'password' => 'pokemons' });
+
+ my $artist_create = {
+ 'name' => '倖田 來未',
+ 'sort-name' => 'Koda Kumi',
+ # 'country' => 1,
+ # 'gender' => 1,
+ 'ended' => 0,
+ 'begin-date' => {
+ 'year' => 1982,
+ 'month' => 11,
+ 'day' => 13,
+ }
+ # 'type' => 1
+ };
+
+ my $inserted = $test->c->model ('NES::Artist')->create (
+ $editor->id, $artist_create);
+
+ ok ($inserted > 0, "Artist has a positive id");
+
+ my $artist = $test->c->model ('NES::Artist')->get_by_revision ($inserted);
+
+ is ($artist->name, "\x{5016}\x{7530} \x{4f86}\x{672a}", "Artist name consists of expected unicode code points");
+ is ($artist->sort_name, "Koda Kumi", "Artist sort-name correct");
+ is ($artist->begin_date->format, "1982-11-13");
+ is ($artist->end_date->format, "");
+ ok (!$artist->ended, "Artist is still alive");
+};
+
+1;
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 MetaBrainz Foundation
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+=cut
diff --git a/t/lib/t/MusicBrainz/Server/Data/NES/Editor.pm b/t/lib/t/MusicBrainz/Server/Data/NES/Editor.pm
new file mode 100644
index 0000000..c8e4894
--- /dev/null
+++ b/t/lib/t/MusicBrainz/Server/Data/NES/Editor.pm
@@ -0,0 +1,45 @@
+package t::MusicBrainz::Server::Data::NES::Editor;
+
+use utf8;
+use Test::Routine;
+use Test::More;
+use MusicBrainz::Server::Data::NES::Editor;
+
+with 't::Context';
+
+test 'Register editor' => sub {
+ my $test = shift;
+
+ my $editor_form = {
+ 'name' => '박 상수',
+ 'password' => 'IchGofEnckucFibrajFecepNooHyfsUv',
+ };
+
+ my $editor = $test->c->model ('NES::Editor')->register ($editor_form);
+
+ isa_ok ($editor, 'MusicBrainz::Server::Entity::Editor');
+ is ($editor->name, '박 상수', "Editor has expected name");
+ ok ($editor->id > 0, "Editor has a positive id");
+};
+
+1;
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 MetaBrainz Foundation
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+=cut
-----------------------------------------------------------------------
hooks/post-receive
--
mb_server
_______________________________________________
MusicBrainz-commits mailing list
[email protected]
http://lists.musicbrainz.org/mailman/listinfo/musicbrainz-commits