| 1 | # ABSTRACT: a {json:api} document building class | ||
| 2 | package PONAPI::Document; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use parent 'PONAPI::Document::Builder::Document'; | ||
| 8 | |||
| 9 | 1; | ||
| 10 | |||
| 11 | __END__ | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding UTF-8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | PONAPI::Document - a {json:api} document building class | ||
| 20 | |||
| 21 | =head1 VERSION | ||
| 22 | |||
| 23 | version 0.001001 | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | use PONAPI::Document; | ||
| 28 | |||
| 29 | my $document = PONAPI::Document->new( | ||
| 30 | version => $version, | ||
| 31 | req_path => $req_path, | ||
| 32 | req_base => $req_base, | ||
| 33 | ); | ||
| 34 | |||
| 35 | my $resource = $document->add_resource( type => 'foo', id => 1 ) | ||
| 36 | ->add_attributes({ ... }) | ||
| 37 | ->add_self_link; | ||
| 38 | $resource->add_relationship(%$_) for ...; | ||
| 39 | |||
| 40 | # If we want multiple resources in one response: | ||
| 41 | $document->convert_to_collection; | ||
| 42 | $document->add_resource() # go crazy! | ||
| 43 | |||
| 44 | # If we have an error at some point: | ||
| 45 | $document->raise_error( 418, 'TEA TIME' ); | ||
| 46 | |||
| 47 | # And once we are done, return a valid {json:api} document | ||
| 48 | # as a perl hash, which you can later turn into JSON. | ||
| 49 | my $result = $document->build; | ||
| 50 | |||
| 51 | =head1 DESCRIPTION | ||
| 52 | |||
| 53 | C<PONAPI::Document> lets you build {json:api} compliant documents. | ||
| 54 | |||
| 55 | You'll want to refer to the L<{json:api} spec|http://jsonapi.org/format> | ||
| 56 | when using this class. | ||
| 57 | |||
| 58 | =head1 METHODS | ||
| 59 | |||
| 60 | =over | ||
| 61 | |||
| 62 | =item * new | ||
| 63 | |||
| 64 | Creates a new document object. Requires C<version>. | ||
| 65 | |||
| 66 | You may optionally provide C<req_base> and C<req_path>. | ||
| 67 | |||
| 68 | =item * req_base | ||
| 69 | |||
| 70 | The base of the request. Used to create links. Empty string by default. | ||
| 71 | |||
| 72 | # Without req_base set: | ||
| 73 | '/authors/1' | ||
| 74 | |||
| 75 | # With req_base set to 'http://www.mygreatponapisite.com/' | ||
| 76 | 'http://www.mygreatponapisite.com/authors/1' | ||
| 77 | |||
| 78 | =item * req_path | ||
| 79 | |||
| 80 | Path to the current request. Used to create pagination links. | ||
| 81 | |||
| 82 | =item * set_status | ||
| 83 | |||
| 84 | =item * status | ||
| 85 | |||
| 86 | HTTP status for the request. Default is C<200>. | ||
| 87 | |||
| 88 | =item * version | ||
| 89 | |||
| 90 | C<{json:api}> version of the request. This B<must> be set when creating the | ||
| 91 | object. | ||
| 92 | |||
| 93 | =item * add_meta( $meta_key => $value ) | ||
| 94 | |||
| 95 | Adds an entry to the meta section of the document, under $meta_key. | ||
| 96 | |||
| 97 | =item * add_resource({ type => $type, id => $id }) | ||
| 98 | |||
| 99 | Creates a new L<PONAPI::Document::Builder::Resource> object, with | ||
| 100 | type $type and id $id, and adds it to the document. | ||
| 101 | |||
| 102 | You can then call C<add_relationship> and C<add_attributes> on | ||
| 103 | this object, amongst other things; See L<PONAPI::Document::Builder::Resource> | ||
| 104 | for all the ways to add information to this object. | ||
| 105 | |||
| 106 | =item * add_null_resource | ||
| 107 | |||
| 108 | Adds a null resource to the object. | ||
| 109 | |||
| 110 | =item * convert_to_collection | ||
| 111 | |||
| 112 | By default, all documents hold a single resource in their data section. | ||
| 113 | However, if C<convert_to_collection> is called on a resource, the data | ||
| 114 | section will instead hold an arrayref of resources. | ||
| 115 | |||
| 116 | # Originally: | ||
| 117 | { data: { type => 'foo', id => 1, attributes => ... } } | ||
| 118 | |||
| 119 | # After convert_to_collection | ||
| 120 | { data: [ { type => 'foo', id => 1, attributes => ... }, ] } | ||
| 121 | |||
| 122 | =item * is_collection | ||
| 123 | |||
| 124 | Returns true if the object holds a collection. | ||
| 125 | |||
| 126 | =item * add_included({ type => $type, id => $id }) | ||
| 127 | |||
| 128 | Similarly to C<add_resource>, returns a L<PONAPI::Document::Builder::Resource> | ||
| 129 | object of the given type and id, and adds it to the C<included> section | ||
| 130 | of the document. | ||
| 131 | |||
| 132 | =item * add_link( $link_type => $url ) | ||
| 133 | |||
| 134 | =item * add_links( $link_type => $url, ... ) | ||
| 135 | |||
| 136 | Adds links to the C<links> section of the document. | ||
| 137 | |||
| 138 | =item * add_self_link | ||
| 139 | |||
| 140 | Convenience method that adds a link to the current object into the C<links> | ||
| 141 | section. | ||
| 142 | |||
| 143 | =item * add_pagination_links(%links) | ||
| 144 | |||
| 145 | Adds the provided pagination links to the C<links> section. | ||
| 146 | |||
| 147 | $obj->add_pagination_links( | ||
| 148 | first => ..., | ||
| 149 | self => ..., | ||
| 150 | prev => ..., | ||
| 151 | next => ..., | ||
| 152 | ); | ||
| 153 | |||
| 154 | =item * build | ||
| 155 | |||
| 156 | Creates a document out of the current state of the object. | ||
| 157 | |||
| 158 | =item * parent | ||
| 159 | |||
| 160 | Returns the immediate parent of this object, or undef. See also L<is_root> | ||
| 161 | |||
| 162 | =item * is_root | ||
| 163 | |||
| 164 | Returns true if we are the root of the document tree. | ||
| 165 | |||
| 166 | =item * find_root | ||
| 167 | |||
| 168 | Returns the root document. | ||
| 169 | |||
| 170 | =item * raise_error( $http_status, $reason ) | ||
| 171 | |||
| 172 | Creates an error document. | ||
| 173 | |||
| 174 | =item * has_errors | ||
| 175 | |||
| 176 | =item * has_errors_builder | ||
| 177 | |||
| 178 | =item * has_included | ||
| 179 | |||
| 180 | =item * has_link | ||
| 181 | |||
| 182 | =item * has_links | ||
| 183 | |||
| 184 | =item * has_links_builder | ||
| 185 | |||
| 186 | =item * has_parent | ||
| 187 | |||
| 188 | =item * has_resource | ||
| 189 | |||
| 190 | =item * has_resource_builders | ||
| 191 | |||
| 192 | =item * has_resources | ||
| 193 | |||
| 194 | =item * has_status | ||
| 195 | |||
| 196 | =item * has_meta | ||
| 197 | |||
| 198 | These do what you would expect. | ||
| 199 | |||
| 200 | =back | ||
| 201 | |||
| 202 | =head1 BUGS, CONTACT AND SUPPORT | ||
| 203 | |||
| 204 | For reporting bugs or submitting patches, please use the github | ||
| 205 | bug tracker at L<https://github.com/mickeyn/PONAPI>. | ||
| 206 | |||
| 207 | =head1 AUTHORS | ||
| 208 | |||
| 209 | =over 4 | ||
| 210 | |||
| 211 | =item * | ||
| 212 | |||
| 213 | Mickey Nasriachi <mickey@cpan.org> | ||
| 214 | |||
| 215 | =item * | ||
| 216 | |||
| 217 | Stevan Little <stevan@cpan.org> | ||
| 218 | |||
| 219 | =item * | ||
| 220 | |||
| 221 | Brian Fraser <hugmeir@cpan.org> | ||
| 222 | |||
| 223 | =back | ||
| 224 | |||
| 225 | =head1 COPYRIGHT AND LICENSE | ||
| 226 | |||
| 227 | This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||
| 228 | |||
| 229 | This is free software; you can redistribute it and/or modify it under | ||
| 230 | the same terms as the Perl 5 programming language system itself. | ||
| 231 | |||
| 232 | =cut |