Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improve tests for message

  • Loading branch information...
commit 4118f4c288b8e01a653395b3a85ba4e2fde64ba4 1 parent 1bf5883
Toru Yamaguchi authored
30 lib/Data/OpenSocial/Base.pm
View
@@ -12,32 +12,50 @@ for (
__PACKAGE__->mk_classdata($_);
}
+__PACKAGE__->element_fields([]);
+__PACKAGE__->element_to_field_map(+{});
+__PACKAGE__->field_to_element_map(+{});
__PACKAGE__->namespaces(
+{
'http://ns.opensocial.org/2008/opensocial' => 'os',
+ 'http://opensocial.org/2008/opensocialapi' => 'osapi',
'http://www.w3.org/2005/Atom' => 'atom',
'http://a9.com/-/spec/opensearch/1.1' => 'osearch',
},
);
+__PACKAGE__->field_to_namespace_map(+{});
sub setup {
my ( $class, @element_fields ) = @_;
- $class->element_fields( [ map { $_->{field} } @element_fields ] );
+ $class->element_fields( [ ( @{$class->element_fields}, map { $_->{field} } @element_fields ) ] );
$class->element_to_field_map(
+{
- map { @$_{qw/typemap field/} }
- grep { exists $_->{typemap} } @element_fields
+ %{$class->element_to_field_map},
+ (
+ map { @$_{qw/typemap field/} }
+ grep { exists $_->{typemap} } @element_fields
+ ),
}
);
$class->field_to_element_map(
+{
- map { @$_{qw/field typemap/} }
- grep { exists $_->{typemap} } @element_fields
+ %{$class->field_to_element_map},
+ (
+ map { @$_{qw/field typemap/} }
+ grep { exists $_->{typemap} } @element_fields
+ ),
}
);
$class->field_to_namespace_map(
- +{ map { ( $_->{field}, $_->{namespace} ) } @element_fields } );
+ +{
+ %{ $class->field_to_namespace_map },
+ (
+ map { ( $_->{field}, $_->{namespace} ) }
+ @element_fields
+ )
+ },
+ );
return map {
my $attr = $_;
9 lib/Data/OpenSocial/Format/JSON.pm
View
@@ -6,9 +6,8 @@ use JSON::Any qw(DWIW XS Syck JSON);
use Data::Util qw(is_array_ref is_hash_ref);
use DateTime::Format::ISO8601;
use Data::OpenSocial::Types;
-
-# use Module::Load;
-# use Module::Loaded;
+use Module::Load;
+use Module::Loaded;
sub format {
my ( $class, $object ) = @_;
@@ -96,8 +95,6 @@ sub parse {
sub parse_object {
my ( $class, $class_type, $object ) = @_;
- load $class_type unless ( is_loaded $class_type);
-
if ( UNIVERSAL::isa( $class_type, 'Data::OpenSocial::Appdata' )
&& !exists $object->{entry} )
{
@@ -111,6 +108,8 @@ sub parse_object {
}
}
+ load $class_type unless (is_loaded($class_type));
+
my %data;
for my $element ( keys %$object ) {
my $field = $class_type->element_to_field($element);
11 lib/Data/OpenSocial/MediaItem.pm
View
@@ -36,6 +36,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'thumbnail_url',
+ typemap => 'thumbnailUrl',
is => 'rw',
isa => 'Str',
required => 0,
@@ -71,6 +72,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'album_id',
+ typemap => 'albumId',
is => 'rw',
isa => 'Str',
required => 0,
@@ -78,6 +80,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'file_size',
+ typemap => 'fileSize',
is => 'rw',
isa => 'Int',
required => 0,
@@ -85,6 +88,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'start_time',
+ typemap => 'startTime',
is => 'rw',
isa => 'DateTime',
required => 0,
@@ -99,6 +103,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'num_votes',
+ typemap => 'numVotes',
is => 'rw',
isa => 'Int',
required => 0,
@@ -106,6 +111,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'num_comments',
+ typemap => 'numComments',
is => 'rw',
isa => 'Int',
required => 0,
@@ -113,6 +119,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'num_views',
+ typemap => 'numViews',
is => 'rw',
isa => 'Int',
required => 0,
@@ -127,6 +134,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'tagged_people',
+ typemap => 'taggedPeople',
is => 'rw',
isa => 'Str',
required => 0,
@@ -134,6 +142,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'mime_type',
+ typemap => 'mimeType',
is => 'rw',
isa => 'Str',
required => 0,
@@ -142,7 +151,7 @@ do {
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'type',
is => 'rw',
- isa => 'MediaItemType',
+ isa => 'OpenSocial.MediaItemType',
required => 0,
},
+{
28 lib/Data/OpenSocial/Message.pm
View
@@ -4,7 +4,11 @@ use Any::Moose;
use Any::Moose ( 'X::Types::DateTime' => [qw/DateTime/], );
use Any::Moose 'X::AttributeHelpers';
-use Data::OpenSocial::Types qw(OpenSocial.Url);
+use Data::OpenSocial::Types qw(
+ OpenSocial.Url
+ OpenSocial.Url.Collection
+ OpenSocial.MessageType
+);
extends 'Data::OpenSocial::Base';
@@ -20,6 +24,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'body_id',
+ typemap => 'bodyId',
is => 'rw',
isa => 'Str',
required => 0,
@@ -34,6 +39,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'title_id',
+ typemap => 'titleId',
is => 'rw',
isa => 'Str',
required => 0,
@@ -49,12 +55,13 @@ do {
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'recipients',
is => 'rw',
- isa => 'Str',
+ isa => 'ArrayRef[Str]',
required => 0,
},
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'sender_id',
+ typemap => 'senderId',
is => 'rw',
isa => 'Str',
required => 0,
@@ -62,6 +69,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'time_sent',
+ typemap => 'timeSent',
is => 'rw',
isa => 'DateTime',
required => 0,
@@ -70,6 +78,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'in_reply_to',
+ typemap => 'inReplyTo',
is => 'rw',
isa => 'Str',
required => 0,
@@ -78,7 +87,7 @@ do {
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'replies',
is => 'rw',
- isa => 'Str',
+ isa => 'ArrayRef[Str]',
required => 0,
},
+{
@@ -91,6 +100,7 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'app_url',
+ typemap => 'appUrl',
is => 'rw',
isa => 'Str',
required => 0,
@@ -98,8 +108,9 @@ do {
+{
namespace => 'http://ns.opensocial.org/2008/opensocial',
field => 'collection_ids',
+ typemap => 'collectionIds',
is => 'rw',
- isa => 'Str',
+ isa => 'ArrayRef[Str]',
required => 0,
},
+{
@@ -115,11 +126,18 @@ do {
field => 'urls',
metaclass => 'Collection::List',
is => 'rw',
- isa => 'ArrayRef[OpenSocial.Url]',
+ isa => 'OpenSocial.Url.Collection',
required => 0,
provides => +{ count => 'count_urls', },
coerce => 1,
},
+ +{ ### specs is wrong
+ namespace => 'http://ns.opensocial.org/2008/opensocial',
+ field => 'type',
+ is => 'rw',
+ isa => 'OpenSocial.MessageType',
+ required => 0,
+ }
);
my %attrs = __PACKAGE__->setup(@element_fields);
37 lib/Data/OpenSocial/MessageType.pm
View
@@ -1,37 +0,0 @@
-package Data::OpenSocial::MessageType;
-
-use Any::Moose;
-use Data::OpenSocial::Types qw(OpenSocial.MessageTypeType);
-
-extends 'Data::OpenSocial::Base';
-
-do {
- my @element_fields = (
- +{
- namespace => 'http://ns.opensocial.org/2008/opensocial',
- field => 'display_value',
- is => 'rw',
- isa => 'Str',
- required => 0,
- predicate => 'has_display_value',
- },
- +{
- namespace => 'http://ns.opensocial.org/2008/opensocial',
- field => 'value',
- is => 'rw',
- isa => 'MessageTypeType',
- required => 0,
- predicate => 'has_value',
- }
- );
-
- my %attrs = __PACKAGE__->setup(@element_fields);
- while ( my ( $field, $attr ) = each %attrs ) {
- has $field => %$attr;
- }
-};
-
-no Any::Moose;
-
-__PACKAGE__->meta->make_immutable;
-
209 lib/Data/OpenSocial/Types.pm
View
@@ -11,7 +11,7 @@ use Any::Moose (
'OpenSocial.PresenceType',
'OpenSocial.SmokerType',
'OpenSocial.CreateActivityPriorityType',
- 'OpenSocial.MessageTypeType',
+ 'OpenSocial.MessageType',
'OpenSocial.EnvironmentType',
'OpenSocial.LookingForType',
'OpenSocial.NetworkPresenceType',
@@ -20,21 +20,32 @@ use Any::Moose (
'OpenSocial.Activity',
'OpenSocial.ActivityTemplateParams',
'OpenSocial.Address',
+ 'OpenSocial.Album',
'OpenSocial.Appdata',
'OpenSocial.AppdataEntry',
'OpenSocial.BodyType',
+ 'OpenSocial.CreateActivityPriority',
'OpenSocial.Drinker',
+ 'OpenSocial.Entry',
+ 'OpenSocial.Environment',
+ 'OpenSocial.EscapeType',
+ 'OpenSocial.Group',
+ 'OpenSocial.LookingFor',
'OpenSocial.MediaItem',
+ 'OpenSocial.Message',
'OpenSocial.Name',
'OpenSocial.NetworkPresence',
'OpenSocial.Organization',
'OpenSocial.Person',
'OpenSocial.PluralPersonField',
'OpenSocial.Presence',
+ 'OpenSocial.Response',
'OpenSocial.Smoker',
+ 'OpenSocial.Types',
'OpenSocial.Url',
### collection
'OpenSocial.AppdataEntry.Collection',
+ 'OpenSocial.Url.Collection',
]
],
'X::Types::'
@@ -43,16 +54,15 @@ use Any::Moose (
use Any::Moose '::Util::TypeConstraints';
use Any::Moose 'X::Types::DateTime';
-use UNIVERSAL::require;
use DateTime::Format::ISO8601;
-
-use Data::OpenSocial::AppdataEntry;
-use Data::OpenSocial::Name;
+use Module::Load;
+use Module::Loaded;
our %PREMITIVE_TYPES = (
- Str => 1,
- Bool => 1,
- Int => 1,
+ Str => 1,
+ Bool => 1,
+ Int => 1,
+ 'ArrayRef[Str]' => 1,
);
our %IMPORTED_TYPES = ( DateTime => 1, );
@@ -66,7 +76,7 @@ our %SIMPLE_TYPES = (
'OpenSocial.SmokerType' =>
[qw/HEAVILY NO OCCASIONALLY QUIT QUITTING REGULARLY SOCIALLY YES/],
'OpenSocial.CreateActivityPriorityType' => [qw/HIGH LOW/],
- 'OpenSocial.MessageTypeType' =>
+ 'OpenSocial.MessageType' =>
[qw/EMAIL NOTIFICATION PRIVATE_MESSAGE PUBLIC_MESSAGE/],
'OpenSocial.EnvironmentType' => [
qw/ACTIVITY ADDRESS BODY_TYPE EMAIL FILTER_TYPE MEDIAITEM MESSAGE MESSAGE_TYPE NAME ORGANIZATION PERSON PHONE SORTORDER URL/
@@ -79,31 +89,49 @@ our %SIMPLE_TYPES = (
our %COMPLEX_TYPES = (
'OpenSocial.Account' => +{
class_type => 'Data::OpenSocial::Account',
- coerce =>
- [ from 'HashRef', via { Data::OpenSocial::Account->new(%$_); } ],
+ coerce => [
+ from 'HashRef',
+ via {
+ Data::OpenSocial::Types->create_data( 'Account', $_ );
+ }
+ ],
},
'OpenSocial.Activity' => +{
class_type => 'Data::OpenSocial::Activity',
- coerce =>
- [ from 'HashRef', via { Data::OpenSocial::Activity->new(%$_); } ],
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'Activity', $_ ); }
+ ],
},
'OpenSocial.ActivityTemplateParams' => +{
class_type => 'Data::OpenSocial::ActivityTemplateParams',
coerce => [
from 'HashRef',
- via { Data::OpenSocial::ActivityTemplateParams->new(%$_); }
+ via {
+ Data::OpenSocial::Types->create_data( 'ActivityTemplateParams',
+ $_ );
+ }
],
},
'OpenSocial.Address' => +{
class_type => 'Data::OpenSocial::Address',
- coerce =>
- [ from 'HashRef', via { Data::OpenSocial::Address->new(%$_); } ],
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'Address', $_ ); }
+ ],
+ },
+ 'OpenSocial.Album' => +{
+ class_type => 'Data::OpenSocial::Album',
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'Album', $_ ); }
+ ],
},
'OpenSocial.Appdata' => +{
class_type => 'Data::OpenSocial::Appdata',
coerce => [
from 'HashRef' => via {
- Data::OpenSocial::Appdata->new(%$_);
+ Data::OpenSocial::Types->create_data( 'Appdata', $_ );
},
]
},
@@ -114,55 +142,75 @@ our %COMPLEX_TYPES = (
my $hash = shift;
if ( exists $hash->{key} && exists $hash->{value} ) {
- return Data::OpenSocial::AppdataEntry->new(%$hash);
+ return Data::OpenSocial::Types->create_data( 'AppdataEntry',
+ $hash );
}
else {
my %args;
@args{qw/key value/} = %$hash;
- return Data::OpenSocial::AppdataEntry->new(%args);
+ return Data::OpenSocial::Types->create_data( 'AppdataEntry',
+ \%args );
}
},
]
},
'OpenSocial.BodyType' => +{
class_type => 'Data::OpenSocial::BodyType',
- coerce =>
- [ from 'HashRef', via { Data::OpenSocial::BodyType->new(%$_); } ]
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'BodyType', $_ ); }
+ ]
},
'OpenSocial.Drinker' => +{
class_type => 'Data::OpenSocial::Drinker',
- coerce =>
- [ from 'HashRef', via { Data::OpenSocial::Drinker->new(%$_); } ]
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'Drinker', $_ ); }
+ ]
},
- 'OpenSocial.Mediaitem' => +{
+ 'OpenSocial.MediaItem' => +{
class_type => 'Data::OpenSocial::MediaItem',
- coerce =>
- [ from 'HashRef', via { Data::OpenSocial::MediaItem->new(%$_); } ]
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'MediaItem', $_ ); }
+ ]
+ },
+ 'OpenSocial.Message' => +{
+ class_type => 'Data::OpenSocial::Message',
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'Message', $_ ); }
+ ]
},
'OpenSocial.Name' => +{
class_type => 'Data::OpenSocial::Name',
- coerce => [ from 'HashRef', via { Data::OpenSocial::Name->new(%$_); } ]
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'Name', $_ ); }
+ ]
},
'OpenSocial.NetworkPresence' => +{
class_type => 'Data::OpenSocial::NetworkPresence',
coerce => [
from 'HashRef',
- via { Data::OpenSocial::NetworkPresence->new(%$_); }
+ via {
+ Data::OpenSocial::Types->create_data( 'NetworkPresence', $_ );
+ }
]
},
'OpenSocial.Organization' => +{
class_type => 'Data::OpenSocial::Organization',
- coerce =>
- [ from 'HashRef', via { Data::OpenSocial::Organization->new(%$_); } ]
+ coerce => [
+ from 'HashRef',
+ via { Data::OpenSocial::Types->create_data( 'Organization', $_ ); }
+ ]
},
'OpenSocial.Person' => +{
class_type => 'Data::OpenSocial::Person',
coerce => [
from 'HashRef',
via {
-
- # Data::OpenSocial::Person->require;
- Data::OpenSocial::Person->new(%$_);
+ Data::OpenSocial::Types->create_data( 'Person', $_ );
},
]
},
@@ -170,28 +218,40 @@ our %COMPLEX_TYPES = (
class_type => 'Data::OpenSocial::PluralPersonField',
coerce => [
from 'HashRef',
- via { Data::OpenSocial::PluralPersonField->new(%$_); }
+ via { Data::OpenSocial::Types->create_data('PluralPersonField', $_); }
],
},
'OpenSocial.Presence' => +{
class_type => 'Data::OpenSocial::Presence',
coerce =>
- [ from 'HashRef', via { Data::OpenSocial::Presence->new(%$_); } ],
+ [ from 'HashRef', via { Data::OpenSocial::Types->create_data('Presence', $_); } ],
},
'OpenSocial.Smoker' => +{
class_type => 'Data::OpenSocial::Smoker',
coerce =>
- [ from 'HashRef', via { Data::OpenSocial::Smoker->new(%$_); } ],
+ [ from 'HashRef', via { Data::OpenSocial::Types->create_data('Smoker', $_); } ],
},
'OpenSocial.Url' => +{
class_type => 'Data::OpenSocial::Url',
- coerce => [ from 'HashRef', via { Data::OpenSocial::Url->new(%$_); } ],
+ coerce => [ from 'HashRef', via { Data::OpenSocial::Types->create_data('Url', $_); } ],
},
);
-our %COLLECTION_TYPES = (
- 'OpenSocial.AppdataEntry.Collection' => +{},
-);
+our %COLLECTION_TYPES = ( 'OpenSocial.AppdataEntry.Collection' => +{}, );
+
+sub create_data {
+ my ( $class, $class_type, $args ) = @_;
+
+ if ( substr( $class_type, 0, 1 ) eq '+' ) {
+ $class_type = substr( $class_type, 1 );
+ }
+ else {
+ $class_type = 'Data::OpenSocial::' . $class_type;
+ }
+
+ load $class_type unless ( is_loaded($class_type) );
+ $class_type->new($args);
+}
sub is_primitive_type {
my ( $class, $type ) = @_;
@@ -245,44 +305,55 @@ do {
};
do {
- coerce 'DateTime'
- => from 'Str'
- => via {
+ coerce 'DateTime' => from 'Str' => via {
DateTime::Format::ISO8601->parse_datetime($_);
- } if (any_moose() eq 'Moose');
+ }
+ if ( any_moose() eq 'Moose' );
subtype 'OpenSocial.AppdataEntry.Collection' => as
'ArrayRef[OpenSocial.AppdataEntry]';
- coerce 'OpenSocial.AppdataEntry.Collection' => from 'ArrayRef[HashRef]' =>
- via {
- if ( exists $_->[0]->{key} && exists $_->[0]->{value} ) {
- return [ map { Data::OpenSocial::AppdataEntry->new(%$_) } @$_, ];
+ coerce 'OpenSocial.AppdataEntry.Collection'
+ => from 'ArrayRef[HashRef]'
+ => via {
+ if ( exists $_->[0]->{key} && exists $_->[0]->{value} ) {
+ return [ map { Data::OpenSocial::Types->create_data('AppdataEntry', $_) } @$_, ];
+ }
+ else {
+ return [
+ map {
+ Data::OpenSocial::Types->create_data(
+ 'AppdataEntry', +{
+ key => $_->[0],
+ value => $_->[1],
+ },
+ )
+ }
+ map { [%$_]; } @$_,
+ ];
+ }
}
- else {
+ => from 'HashRef',
+ => via {
+ my $hash = shift;
return [
map {
- Data::OpenSocial::AppdataEntry->new(
- key => $_->[0],
- value => $_->[1]
- )
- }
- map { [%$_]; } @$_,
+ Data::OpenSocial::Types->create_data(
+ 'AppdataEntry', +{
+ key => $_,
+ value => $hash->{$_},
+ },
+ )
+ }
+ sort keys %$hash,
];
- }
- } => from 'HashRef',
- => via {
- my $hash = shift;
- return [
- map {
- Data::OpenSocial::AppdataEntry->new(
- key => $_,
- value => $hash->{$_}
- )
- }
- sort keys %$hash,
- ];
- };
+ };
+ subtype 'OpenSocial.Url.Collection' => as 'ArrayRef[OpenSocial.Url]';
+ coerce 'OpenSocial.Url.Collection'
+ => from 'ArrayRef[HashRef]'
+ => via {
+ return [ map { Data::OpenSocial::Types->create_data('Url', $_) } @$_ ];
+ };
};
no Any::Moose;
48 t/album/02_coerce.t
View
@@ -0,0 +1,48 @@
+use strict;
+use Test::More;
+
+use Data::OpenSocial::Album;
+
+plan tests => 7 * 1;
+
+{
+ package Test::Data::OpenSocial::Album::IsaType;
+
+ use Any::Moose;
+ use Data::OpenSocial::Types qw(OpenSocial.Album);
+
+ has 'data' => (
+ is => 'rw',
+ isa => 'OpenSocial.Album',
+ coerce => 1,
+ );
+
+ no Any::Moose;
+
+ __PACKAGE__->meta->make_immutable;
+}
+
+our %defaults = (
+ id => '44332211',
+ thumbnail_url => 'http://pages.example.org/albums/4433221-tn.png',
+ # caption => 'Example Album', # spec is wrong?
+ description =>
+ 'This is an example album, and this text is an example description',
+ location => +{ latitude => 0, longitude => 0 },
+ owner_id => 'example.org:55443322',
+);
+
+{
+ my $type_class = Test::Data::OpenSocial::Album::IsaType->new( data => \%defaults );
+
+ my $data = $type_class->data;
+
+ isa_ok($data, 'Data::OpenSocial::Album');
+
+ is($data->id, $defaults{id}, 'id');
+ is($data->thumbnail_url, $defaults{thumbnail_url}, 'thumbnail_url');
+ is($data->description, $defaults{description}, 'description');
+ is($data->location->latitude, $defaults{location}{latitude}, 'location->latitude');
+ is($data->location->longitude, $defaults{location}{longitude}, 'location->longitude');
+ is($data->owner_id, $defaults{owner_id}, 'owner_id');
+}
41 t/album/10_format_json.t
View
@@ -0,0 +1,41 @@
+use strict;
+use Test::More;
+
+use JSON::Any;
+use Data::OpenSocial::Album;
+use Data::OpenSocial::Format::JSON;
+
+plan tests => 8 * 1;
+
+diag(
+'refer to http://www.opensocial.org/Technical-Resources/opensocial-spec-v09/REST-API.html#rfc.section.3.6.1'
+);
+
+{
+ my $src = +{
+ id => '44332211',
+ thumbnailUrl => 'http://pages.example.org/albums/4433221-tn.png',
+
+ # caption => 'Example Album', # spec is wrong?
+ description =>
+ 'This is an example album, and this text is an example description',
+ location => +{ latitude => 0, longitude => 0 },
+ ownerId => 'example.org:55443322',
+ };
+
+ my $json = JSON::Any->new;
+ my $data = Data::OpenSocial::Format::JSON->parse( 'Album', $json->to_json($src) );
+
+ isa_ok($data, 'Data::OpenSocial::Album');
+
+ is($data->id, $src->{id}, 'id');
+ is($data->thumbnail_url, $src->{thumbnailUrl}, 'thumbnail_url');
+ is($data->description, $src->{description}, 'description');
+ is($data->location->latitude, $src->{location}{latitude}, 'location->latitude');
+ is($data->location->longitude, $src->{location}{longitude}, 'location->longitude');
+ is($data->owner_id, $src->{ownerId}, 'owner_id');
+
+ my $json_str = Data::OpenSocial::Format::JSON->format($data);
+ note($json_str);
+ is_deeply( scalar $json->from_json($json_str), $src, 'format json' );
+}
1  t/appdata/01_basic.t
View
@@ -1,6 +1,7 @@
use strict;
use Test::More;
use Data::OpenSocial::Appdata;
+use Data::OpenSocial::AppdataEntry;
diag(
'refer to http://www.opensocial.org/Technical-Resources/opensocial-spec-v09/REST-API.html#rfc.section.3.5.1'
6 t/appdata/02_coerce.t
View
@@ -25,8 +25,8 @@ plan tests => 5 * 2;
# from 'HashRef'
# provide entry as Hash, ordering sorted by key.
my $type_class =
- Test::Data::OpenSocial::Appdata::IsaType->new(
- appdata => +{ entry => +{ pokes => 3, last_poke => '2008-02-13T18:30:02Z', } } );
+ Test::Data::OpenSocial::Appdata::IsaType->new( data =>
+ +{ entry => +{ pokes => 3, last_poke => '2008-02-13T18:30:02Z', } } );
my $data = $type_class->data;
@@ -41,7 +41,7 @@ plan tests => 5 * 2;
# from 'ArrayRef[HashRef]'
my $type_class =
Test::Data::OpenSocial::Appdata::IsaType->new(
- appdata => +{ entry => [ +{ pokes => 3, }, +{ last_poke => '2008-02-13T18:30:02Z', }, ] } );
+ data => +{ entry => [ +{ pokes => 3, }, +{ last_poke => '2008-02-13T18:30:02Z', }, ] } );
my $data = $type_class->data;
31 t/media_item/01_basic.t
View
@@ -0,0 +1,31 @@
+use strict;
+use Test::More;
+
+use Data::OpenSocial::Address;
+use Data::OpenSocial::MediaItem;
+
+diag(
+ 'refer to http://www.opensocial.org/Technical-Resources/opensocial-spec-v09/REST-API.html#rfc.section.3.6.1'
+);
+
+our %defaults = (
+ id => '11223344',
+ thumbnail_url => 'http://pages.example.org/images/11223344-tn.png',
+ mime_type => 'image/png',
+ type => 'IMAGE',
+ url => 'http://pages.example.org/images/11223344.png',
+ album_id => '44332211',
+);
+
+plan tests => ( ( scalar keys %defaults ) + 1 ) * 1;
+
+{
+ my $data = Data::OpenSocial::MediaItem->new;
+
+ isa_ok( $data, 'Data::OpenSocial::MediaItem' );
+
+ for my $attr ( keys %defaults ) {
+ $data->$attr( $defaults{$attr} );
+ is_deeply( $data->$attr, $defaults{$attr}, $attr . ' attr' );
+ }
+}
47 t/media_item/02_coerce.t
View
@@ -0,0 +1,47 @@
+use strict;
+use Test::More;
+
+use Data::OpenSocial::MediaItem;
+
+plan tests => 7 * 1;
+
+{
+ package Test::Data::OpenSocial::MediaItem::IsaType;
+
+ use Any::Moose;
+ use Data::OpenSocial::Types qw(OpenSocial.MediaItem);
+
+ has 'data' => (
+ is => 'rw',
+ isa => 'OpenSocial.MediaItem',
+ coerce => 1,
+ );
+
+ no Any::Moose;
+
+ __PACKAGE__->meta->make_immutable;
+}
+
+our %defaults = (
+ id => '11223344',
+ thumbnail_url => 'http://pages.example.org/images/11223344-tn.png',
+ mime_type => 'image/png',
+ type => 'IMAGE',
+ url => 'http://pages.example.org/images/11223344.png',
+ album_id => '44332211',
+);
+
+{
+ my $type_class = Test::Data::OpenSocial::MediaItem::IsaType->new( data => \%defaults );
+
+ my $data = $type_class->data;
+
+ isa_ok($data, 'Data::OpenSocial::MediaItem');
+
+ is($data->id, $defaults{id}, 'id');
+ is($data->thumbnail_url, $defaults{thumbnail_url}, 'thumbnail_url');
+ is($data->mime_type, $defaults{mime_type}, 'mime_type');
+ is($data->type, $defaults{type}, 'type');
+ is($data->url, $defaults{url}, 'url');
+ is($data->album_id, $defaults{album_id}, 'album_id');
+}
41 t/media_item/10_format_json.t
View
@@ -0,0 +1,41 @@
+use strict;
+use Test::More;
+
+use JSON::Any;
+use Data::OpenSocial::MediaItem;
+use Data::OpenSocial::Format::JSON;
+
+plan tests => 8 * 1;
+
+diag(
+'refer to http://www.opensocial.org/Technical-Resources/opensocial-spec-v09/REST-API.html#rfc.section.3.6.2'
+);
+
+{
+ my $src = +{
+ id => '11223344',
+ thumbnailUrl => 'http://pages.example.org/images/11223344-tn.png',
+ mimeType => 'image/png',
+ type => 'IMAGE',
+ url => 'http://pages.example.org/images/11223344.png',
+ albumId => '44332211',
+ };
+
+ my $json = JSON::Any->new;
+ my $data =
+ Data::OpenSocial::Format::JSON->parse( 'MediaItem',
+ $json->to_json($src) );
+
+ isa_ok( $data, 'Data::OpenSocial::MediaItem' );
+
+ is( $data->id, $src->{id}, 'id' );
+ is( $data->thumbnail_url, $src->{thumbnailUrl}, 'thumbnailUrl' );
+ is( $data->mime_type, $src->{mimeType}, 'mimeType' );
+ is( $data->type, $src->{type}, 'type' );
+ is( $data->url, $src->{url}, 'url' );
+ is( $data->album_id, $src->{albumId}, 'albumId' );
+
+ my $json_str = Data::OpenSocial::Format::JSON->format($data);
+ note($json_str);
+ is_deeply( scalar $json->from_json($json_str), $src, 'format json' );
+}
35 t/message/01_basic.t
View
@@ -0,0 +1,35 @@
+use strict;
+use Test::More;
+
+use Data::OpenSocial::Message;
+
+diag(
+ 'refer to http://www.opensocial.org/Technical-Resources/opensocial-spec-v09/REST-API.html#rfc.section.10.2'
+);
+
+our %defaults = (
+ id => 'http://example.org/inbox/message/{msgid}',
+ recipients =>
+ [ 'example.org:AD38B3886625AAF', 'example.org:997638BAA6F25AD' ],
+ sender_id => 'example.org:34KJDCSKJN2HHF0DW20394',
+ title => 'You have a new messge from Joe',
+ title_id => '541141091700',
+ body => 'Short message from Joe to some friends',
+ body_id => '5491155811231',
+ type => 'PRIVATE_MESSAGE',
+ status => 'unread',
+ # data => '...'
+);
+
+plan tests => ( ( scalar keys %defaults ) + 1 ) * 1;
+
+{
+ my $data = Data::OpenSocial::Message->new;
+
+ isa_ok( $data, 'Data::OpenSocial::Message' );
+
+ for my $attr ( keys %defaults ) {
+ $data->$attr( $defaults{$attr} );
+ is_deeply( $data->$attr, $defaults{$attr}, $attr . ' attr' );
+ }
+}
55 t/message/02_coerce.t
View
@@ -0,0 +1,55 @@
+use strict;
+use Test::More;
+
+use Data::OpenSocial::Message;
+
+plan tests => 10 * 1;
+
+{
+ package Test::Data::OpenSocial::Message::IsaType;
+
+ use Any::Moose;
+ use Data::OpenSocial::Types qw(OpenSocial.Message);
+
+ has 'data' => (
+ is => 'rw',
+ isa => 'OpenSocial.Message',
+ coerce => 1,
+ );
+
+ no Any::Moose;
+
+ __PACKAGE__->meta->make_immutable;
+}
+
+our %defaults = (
+ id => 'http://example.org/inbox/message/{msgid}',
+ recipients =>
+ [ 'example.org:AD38B3886625AAF', 'example.org:997638BAA6F25AD' ],
+ sender_id => 'example.org:34KJDCSKJN2HHF0DW20394',
+ title => 'You have a new messge from Joe',
+ title_id => '541141091700',
+ body => 'Short message from Joe to some friends',
+ body_id => '5491155811231',
+ type => 'PRIVATE_MESSAGE',
+ status => 'unread',
+ # data => '...'
+);
+
+{
+ my $type_class = Test::Data::OpenSocial::Message::IsaType->new( data => \%defaults );
+
+ my $data = $type_class->data;
+
+ isa_ok($data, 'Data::OpenSocial::Message');
+
+ is($data->id, $defaults{id}, 'id');
+ is_deeply($data->recipients, $defaults{recipients}, 'recipients');
+ is($data->sender_id, $defaults{sender_id}, 'sender_id');
+ is($data->title, $defaults{title}, 'title');
+ is($data->title_id, $defaults{title_id}, 'title_id');
+ is($data->body, $defaults{body}, 'body');
+ is($data->body_id, $defaults{body_id}, 'body_id');
+ is($data->type, $defaults{type}, 'type');
+ is($data->status, $defaults{status}, 'status');
+}
49 t/message/10_format_json.t
View
@@ -0,0 +1,49 @@
+use strict;
+use Test::More;
+
+use JSON::Any;
+use Data::OpenSocial::Message;
+use Data::OpenSocial::Format::JSON;
+
+plan tests => 11 * 1;
+
+{
+ diag(
+ 'refer to http://www.opensocial.org/Technical-Resources/opensocial-spec-v09/REST-API.html#rfc.section.10.2'
+ );
+
+ my $src = +{
+ id => 'http://example.org/inbox/message/{msgid}',
+ recipients =>
+ [ 'example.org:AD38B3886625AAF', 'example.org:997638BAA6F25AD' ],
+ senderId => 'example.org:34KJDCSKJN2HHF0DW20394',
+ title => 'You have a new messge from Joe',
+ titleId => '541141091700',
+ body => 'Short message from Joe to some friends',
+ bodyId => '5491155811231',
+ type => 'PRIVATE_MESSAGE',
+ status => 'unread',
+ # data => '...'
+ };
+
+ my $json = JSON::Any->new;
+ my $data =
+ Data::OpenSocial::Format::JSON->parse( 'Message',
+ $json->to_json($src) );
+
+ isa_ok( $data, 'Data::OpenSocial::Message' );
+
+ is($data->id, $src->{id}, 'id');
+ is_deeply($data->recipients, $src->{recipients}, 'recipients');
+ is($data->sender_id, $src->{senderId}, 'sender_id');
+ is($data->title, $src->{title}, 'title');
+ is($data->title_id, $src->{titleId}, 'title_id');
+ is($data->body, $src->{body}, 'body');
+ is($data->body_id, $src->{bodyId}, 'body_id');
+ is($data->type, $src->{type}, 'type');
+ is($data->status, $src->{status}, 'status');
+
+ my $json_str = Data::OpenSocial::Format::JSON->format($data);
+ note($json_str);
+ is_deeply( scalar $json->from_json($json_str), $src, 'format json' );
+}
Please sign in to comment.
Something went wrong with that request. Please try again.