[Bps-public-commit] rt-extension-rest2 branch, master, updated. 41d9f3d6926a77a65531933c67c3dcfed3db2c5e
Shawn Moore
shawn at bestpractical.com
Tue Dec 13 15:36:32 EST 2016
The branch, master has been updated
via 41d9f3d6926a77a65531933c67c3dcfed3db2c5e (commit)
from b0d5e708c16a88f4b9e5c08d0d401905a334f4ff (commit)
Summary of changes:
lib/RT/Extension/REST2/Resource/Record/Readable.pm | 26 ++++++++++++++++++++--
t/tickets.t | 8 ++++++-
2 files changed, 31 insertions(+), 3 deletions(-)
- Log -----------------------------------------------------------------
commit 41d9f3d6926a77a65531933c67c3dcfed3db2c5e
Author: Shawn M Moore <shawn at bestpractical.com>
Date: Tue Dec 13 20:36:21 2016 +0000
Generalize self-link into arbitrary hypermedia links
diff --git a/lib/RT/Extension/REST2/Resource/Record/Readable.pm b/lib/RT/Extension/REST2/Resource/Record/Readable.pm
index c5be583..e0e9bdf 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Readable.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Readable.pm
@@ -12,18 +12,40 @@ requires 'base_uri';
use JSON ();
use RT::Extension::REST2::Util qw( serialize_record record_type );
+use Scalar::Util qw( blessed );
sub serialize {
my $self = shift;
my $record = $self->record;
my $data = serialize_record($record);
- # Add the resource url for this record
- $data->{_url} = join "/", $self->base_uri, $record->id;
+ $data->{_hyperlinks} = $self->hypermedia_links;
return $data;
}
+sub _self_link {
+ my $self = shift;
+ my $record = $self->record;
+
+ my $class = blessed($record);
+ $class =~ s/^RT:://;
+ $class = lc $class;
+ my $id = $record->id;
+
+ return {
+ ref => 'self',
+ type => $class,
+ id => $id,
+ _url => RT::Extension::REST2->base_uri . "/$class/$id",
+ };
+}
+
+sub hypermedia_links {
+ my $self = shift;
+ return [ $self->_self_link ];
+}
+
sub charsets_provided { [ 'utf-8' ] }
sub default_charset { 'utf-8' }
diff --git a/t/tickets.t b/t/tickets.t
index 0d946de..805b8ee 100644
--- a/t/tickets.t
+++ b/t/tickets.t
@@ -86,11 +86,17 @@ my ($ticket_url, $ticket_id);
is($content->{Type}, 'ticket');
is($content->{Status}, 'new');
is($content->{Subject}, 'Ticket creation using REST');
- like($content->{_url}, qr[$rest_base_path/ticket/$ticket_id$]);
+
ok(exists $content->{$_}) for qw(AdminCc TimeEstimated Started Cc
LastUpdated TimeWorked Resolved
Created Due Priority EffectiveId);
+ my $links = $content->{_hyperlinks};
+ is($links->[0]{ref}, 'self');
+ is($links->[0]{id}, 1);
+ is($links->[0]{type}, 'ticket');
+ like($links->[0]{_url}, qr[$rest_base_path/ticket/$ticket_id$]);
+
my $queue = $content->{Queue};
is($queue->{id}, 1);
is($queue->{type}, 'queue');
-----------------------------------------------------------------------
More information about the Bps-public-commit
mailing list