forked from stevan/Jackalope-REST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spec.pm
347 lines (312 loc) · 11.2 KB
/
Spec.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package Jackalope::REST::Schema::Spec;
use Moose;
our $VERSION = '0.01';
our $AUTHORITY = 'cpan:STEVAN';
extends 'Jackalope::Schema::Spec';
around '_all_spec_builder_methods' => sub {
my $next = shift;
my $self = shift;
return (
$self->$next(),
qw[
resource
resource_ref
service_discoverable
service_readonly
service_non_editable
service
]
);
};
## ------------------------------------------------------------------
## Resource Schema
## ------------------------------------------------------------------
## The Resource schema is a simple wrapper for web based resources
## it is mostly intended to be used for extension where the body
## property is overriden with the schema of your choice.
## ------------------------------------------------------------------
sub resource {
my $self = shift;
return +{
id => "jackalope/rest/resource",
title => "The 'Resource' schema",
description => q[
The is a 'wrapper' of sorts for resources
as viewed from the concept of the web and
REST. It is mostly intended to be extended
where the 'body' property is overriden with
the schema of our choice.
],
type => "object",
properties => {
id => {
type => "string",
description => q[
This is the ID of the given resource, it is
assumed to be some kind of string, which should
still just work fine even for numeric values.
This is expected to be the lookup key for
resources in a resource repository.
]
},
body => {
type => "any",
description => q[
This is the body of the resource, it is of type
'any' for now, but it as this schema is meant to
be extended and this property overridden, this is
basically whatever you need it to be.
]
},
version => {
type => "string",
'format' => "digest",
description => q[
This is a digest string (SHA-256) representing the current
version of the resource. When the resource is updated
the version should be compared first, to make sure
that it has not been updated by another.
]
}
},
additional_properties => {
links => {
type => "array",
items => { '$ref' => "jackalope/core/hyperlink" },
description => q[
This is a list of links which represent the
capabilities of given resource, the consumer of
the resource can use these links to perform
different actions.
]
}
}
}
}
## ------------------------------------------------------------------
## Resource Ref Schema
## ------------------------------------------------------------------
## The Resource Ref schema is meant to be a way to represent
## references to resources, it can be a convient way to refer
## to a resource only by it's ID and therefore save bandwidth.
## ------------------------------------------------------------------
sub resource_ref {
my $self = shift;
return +{
id => "jackalope/rest/resource/ref",
title => "The 'Resource Ref' schema",
description => q[
This is meant to be a way to represent
references to resources, it can be a convient
way to refer to a resource only by it's ID
and therefore save bandwidth.
],
type => "object",
properties => {
'$id' => {
type => "string",
description => q[
This is the lookup ID which can be
used to locate the resource.
]
},
type_of => {
type => "string",
"format" => "uri",
description => q[
This is the schema URI for the type of
resource that this refers too.
]
},
},
additional_properties => {
link => {
extends => { '$ref' => 'jackalope/core/hyperlink' },
properties => { rel => { type => 'string', literal => 'read' } },
additional_properties => { method => { type => 'string', literal => 'GET' } },
description => q[
This is an optional hyperlink to read the resource
described, it should only ever be GET since it
is only for reading.
]
},
version => {
type => "string",
'format' => "digest",
description => q[
This is a digest string (SHA-256) representing the current
version of the resource being pointed to. This can be used
to check to make sure that the resource has not changed
since it was last referred too.
]
},
}
}
}
## ------------------------------------------------------------------
## Service Schemas
## ------------------------------------------------------------------
## The Service schema is a simple template for REST based web
## service that follows a convention for the standard operations
## that would be performed on a REST resource collection.
## ------------------------------------------------------------------
sub service_discoverable {
my $self = shift;
return +{
id => 'jackalope/rest/service/discoverable',
title => 'This is a base discoverable REST enabled schema',
type => 'object',
links => {
describedby => {
rel => 'describedby',
href => '/',
method => 'OPTIONS',
target_schema => {
type => 'object',
extends => { '$ref' => 'jackalope/rest/resource' },
properties => {
body => {
type => 'object',
items => { type => 'schema' },
},
}
},
}
}
};
}
sub service_readonly {
my $self = shift;
return +{
id => 'jackalope/rest/service/read-only',
title => 'This is a simple read-only REST enabled schema',
extends => { '$ref' => 'jackalope/rest/service/discoverable' },
links => {
list => {
rel => 'list',
href => '/',
method => 'GET',
data_schema => {
type => 'object',
additional_properties => {
query => { type => 'object' }, # the query structure key=term
attrs => { type => 'object' }, # things like limit, skip, etc.
}
},
target_schema => {
type => "array",
items => {
type => 'object',
extends => { '$ref' => 'jackalope/rest/resource' },
properties => {
body => { '$ref' => '#' },
}
}
},
},
read => {
rel => 'read',
href => '/:id',
method => 'GET',
target_schema => {
type => 'object',
extends => { '$ref' => 'jackalope/rest/resource' },
properties => {
body => { '$ref' => '#' },
}
},
uri_schema => {
id => { type => 'string' }
}
}
}
};
}
sub service_non_editable {
my $self = shift;
return +{
id => 'jackalope/rest/service/non-editable',
title => 'This is a simple REST enabled schema',
extends => { '$ref' => 'jackalope/rest/service/read-only' },
links => {
create => {
rel => 'create',
href => '/',
method => 'POST',
data_schema => { '$ref' => '#' },
target_schema => {
type => 'object',
extends => { '$ref' => 'jackalope/rest/resource' },
properties => {
body => { '$ref' => '#' },
}
},
},
delete => {
rel => 'delete',
href => '/:id',
method => 'DELETE',
uri_schema => {
id => { type => 'string' }
}
}
}
};
}
sub service {
my $self = shift;
return +{
id => 'jackalope/rest/service/crud',
title => 'This is a simple REST enabled schema',
extends => { '$ref' => 'jackalope/rest/service/non-editable' },
links => {
edit => {
rel => 'edit',
href => '/:id',
method => 'PUT',
data_schema => {
type => 'object',
extends => { '$ref' => 'jackalope/rest/resource' },
properties => {
body => { '$ref' => '#' },
}
},
target_schema => {
type => 'object',
extends => { '$ref' => 'jackalope/rest/resource' },
properties => {
body => { '$ref' => '#' },
}
},
uri_schema => {
id => { type => 'string' }
}
}
}
};
}
__PACKAGE__->meta->make_immutable;
no Moose; 1;
__END__
=pod
=head1 NAME
Jackalope::REST::Schema::Spec - A Moosey solution to this problem
=head1 SYNOPSIS
use Jackalope::REST::Schema::Spec;
=head1 DESCRIPTION
=head1 METHODS
=over 4
=item B<>
=back
=head1 BUGS
All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
=head1 AUTHOR
Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2010 Infinity Interactive, Inc.
L<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut