« blib/lib/Web/DataService/Render.pm
↑ Summary
/home/cpancover/.cpan/build/Web-DataService-0.41-0/lib/Web/DataService/Plugin/TemplateToolkit.pm »
| 1 | # | ||
| 2 | # Web::DataService::IDocument | ||
| 3 | # | ||
| 4 | # This is a role that provides access to dataservice documentation. It is | ||
| 5 | # designed to be composed into the classes used for documenting dataservice | ||
| 6 | # nodes. | ||
| 7 | |||
| 8 | |||
| 9 | use strict; | ||
| 10 | |||
| 11 | package Web::DataService::IDocument; | ||
| 12 | |||
| 13 | use Moo::Role; | ||
| 14 | |||
| 15 | |||
| 16 | # document_node ( ) | ||
| 17 | # | ||
| 18 | # Return the documentation string for this node, if one was defined. If an | ||
| 19 | # "extended_doc" string was also defined for this node, return it as well. | ||
| 20 | |||
| 21 | sub document_node { | ||
| 22 | |||
| 23 | my ($request) = @_; | ||
| 24 | |||
| 25 | my $ds = $request->ds; | ||
| 26 | my $path = $request->node_path; | ||
| 27 | my $extended = $ds->{extdoc_node}{$path}; | ||
| 28 | |||
| 29 | if ( ref $extended eq 'HASH' ) | ||
| 30 | { | ||
| 31 | my $disp = $extended->{disp}; | ||
| 32 | |||
| 33 | if ( $disp eq 'replace' ) | ||
| 34 | { | ||
| 35 | return $extended->{doc_string}; | ||
| 36 | } | ||
| 37 | |||
| 38 | my $doc_string = $ds->node_attr($path, 'doc_string') // ''; | ||
| 39 | |||
| 40 | $doc_string .= "\n\n" if $disp eq 'para' && $doc_string ne ''; | ||
| 41 | |||
| 42 | return $doc_string . $extended->{doc_string}; | ||
| 43 | } | ||
| 44 | |||
| 45 | else | ||
| 46 | { | ||
| 47 | return $ds->node_attr($path, 'doc_string'); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | # list_navtrail ( ) | ||
| 53 | # | ||
| 54 | # Return a list of navigation trail components for the current request, in POD | ||
| 55 | # format. This is derived component-by-component from the request path. | ||
| 56 | |||
| 57 | sub list_navtrail { | ||
| 58 | |||
| 59 | my ($request, $base_label) = @_; | ||
| 60 | |||
| 61 | my $path = $request->node_path || '/'; | ||
| 62 | my $ds = $request->ds; | ||
| 63 | |||
| 64 | $base_label ||= $ds->node_attr($path, 'title') || ''; | ||
| 65 | |||
| 66 | # If there are no path components, return just the base label. | ||
| 67 | |||
| 68 | return $base_label if $path eq '/'; | ||
| 69 | |||
| 70 | # Otherwise, split the path into components and go through them one by one. | ||
| 71 | |||
| 72 | my @trail = $ds->node_link('/', $base_label); | ||
| 73 | my @path = split qr{/}, $path; | ||
| 74 | my $node = ""; | ||
| 75 | my $count = $#path; | ||
| 76 | |||
| 77 | foreach my $component (@path) | ||
| 78 | { | ||
| 79 | $node .= '/' if $node; | ||
| 80 | $node .= $component; | ||
| 81 | |||
| 82 | if ( $count-- == 0 ) | ||
| 83 | { | ||
| 84 | push @trail, $ds->node_attr($node, 'title') || $component; | ||
| 85 | } | ||
| 86 | |||
| 87 | else | ||
| 88 | { | ||
| 89 | push @trail, $ds->node_link($node); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | return @trail; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | # list_http_methods ( ) | ||
| 98 | # | ||
| 99 | # Return a list of the HTTP methods that are allowed for this request path. | ||
| 100 | |||
| 101 | sub list_http_methods { | ||
| 102 | |||
| 103 | my ($request) = @_; | ||
| 104 | |||
| 105 | my $methods = $request->{ds}->node_attr($request, 'allow_method'); | ||
| 106 | return @Web::DataService::DEFAULT_METHODS unless ref $methods eq 'HASH'; | ||
| 107 | return grep { $methods->{$_} } @Web::DataService::HTTP_METHOD_LIST; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | # document_http_methods ( ) | ||
| 112 | # | ||
| 113 | # Return a string documenting the HTTP methods that are allowed for this | ||
| 114 | # request path. | ||
| 115 | |||
| 116 | sub document_http_methods { | ||
| 117 | |||
| 118 | my ($request) = @_; | ||
| 119 | |||
| 120 | my $doc = join(', ', map { "C<$_>" } $request->list_http_methods); | ||
| 121 | return $doc || ''; | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | # list_subnodes | ||
| 126 | # | ||
| 127 | # Return a list of sub-nodes of the current one. This will include all | ||
| 128 | # sub-nodes with a value for the node attribute 'place', in order by the value | ||
| 129 | # of that attribute. | ||
| 130 | |||
| 131 | sub list_subnodes { | ||
| 132 | |||
| 133 | my ($request) = @_; | ||
| 134 | |||
| 135 | my $ds = $request->ds; | ||
| 136 | my $path = $request->node_path; | ||
| 137 | |||
| 138 | return $ds->list_subnodes($path); | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | # document_subnodes ( options ) | ||
| 143 | # | ||
| 144 | # Return a documentation string in Pod format listing the subnodes (if any) | ||
| 145 | # given for this node. See &list_subnodes above. | ||
| 146 | |||
| 147 | sub document_nodelist { | ||
| 148 | |||
| 149 | my ($request, $options) = @_; | ||
| 150 | |||
| 151 | $options ||= {}; | ||
| 152 | |||
| 153 | my $ds = $request->ds; | ||
| 154 | my $path = $options->{list} || $request->node_path; | ||
| 155 | $options->{base} = $request->base_url; | ||
| 156 | |||
| 157 | return $ds->document_nodelist($path, $options); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | # document_usage | ||
| 162 | # | ||
| 163 | # Return a documentation string in Pod format describing the usage examples of | ||
| 164 | # the node corresponding to this request. | ||
| 165 | |||
| 166 | sub document_usage { | ||
| 167 | |||
| 168 | my ($request, $options) = @_; | ||
| 169 | |||
| 170 | my $ds = $request->ds; | ||
| 171 | my $path = $request->node_path; | ||
| 172 | |||
| 173 | return $ds->document_usage($path, $options); | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | # document_params ( ) | ||
| 178 | # | ||
| 179 | # Return a documentation string in POD format describing the parameters | ||
| 180 | # available for this request. | ||
| 181 | |||
| 182 | sub document_params { | ||
| 183 | |||
| 184 | my ($request, $ruleset_name) = @_; | ||
| 185 | |||
| 186 | my $ds = $request->{ds}; | ||
| 187 | |||
| 188 | $ruleset_name ||= $ds->determine_ruleset($request->node_path); | ||
| 189 | |||
| 190 | # Generate documentation about the parameters, using the appropriate | ||
| 191 | # method from the validator class (HTTP::Validate). If no ruleset is | ||
| 192 | # selected for this request, then state that no parameters are accepted. | ||
| 193 | |||
| 194 | if ( $ruleset_name ) | ||
| 195 | { | ||
| 196 | return $ds->document_ruleset($ruleset_name); | ||
| 197 | } | ||
| 198 | |||
| 199 | else | ||
| 200 | { | ||
| 201 | return ''; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | # output_label ( ) | ||
| 207 | # | ||
| 208 | # Return the output label for the node corresponding to this request. | ||
| 209 | |||
| 210 | sub output_label { | ||
| 211 | |||
| 212 | my ($request) = @_; | ||
| 213 | |||
| 214 | return $request->{ds}->node_attr($request, 'output_label') || 'basic'; | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | # optional_output ( ) | ||
| 219 | # | ||
| 220 | # Return the name of the optional output map, if any. | ||
| 221 | |||
| 222 | sub optional_output { | ||
| 223 | |||
| 224 | my ($request) = @_; | ||
| 225 | |||
| 226 | return $request->{ds}->node_attr($request, 'optional_output'); | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | # document_response ( ) | ||
| 231 | # | ||
| 232 | # Return a documentation string in POD format describing the fields that can | ||
| 233 | # be included in the result. | ||
| 234 | |||
| 235 | sub document_response { | ||
| 236 | |||
| 237 | my ($request, $options) = @_; | ||
| 238 | |||
| 239 | my $ds = $request->{ds}; | ||
| 240 | |||
| 241 | return $ds->document_response($request->node_path, $options); | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | # document_summary ( ) | ||
| 246 | # | ||
| 247 | # Return a documentation string in POD format describing the fields that can | ||
| 248 | # be included in the summary block. If no summary block was specified for | ||
| 249 | # this operation, return the empty string. | ||
| 250 | |||
| 251 | sub document_summary { | ||
| 252 | |||
| 253 | my ($request, $options) = @_; | ||
| 254 | |||
| 255 | my $ds = $request->{ds}; | ||
| 256 | |||
| 257 | return $ds->document_summary($request->node_path, $options); | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | # document_formats ( extended ) | ||
| 262 | # | ||
| 263 | # Return a string in POD format documenting the formats allowed for the path | ||
| 264 | # associated with this request. If $extended is true, then include a text | ||
| 265 | # description of each format. | ||
| 266 | |||
| 267 | sub document_formats { | ||
| 268 | |||
| 269 | my ($request, $options) = @_; | ||
| 270 | |||
| 271 | $options ||= {}; | ||
| 272 | my ($path) = $options->{all} ? ('/') : ($options->{path} || $request->node_path); | ||
| 273 | |||
| 274 | return $request->ds->document_formats($path, $options); | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | # defualt_format ( ) | ||
| 279 | # | ||
| 280 | # Return the name of the default format (if any) for this request's path. | ||
| 281 | |||
| 282 | sub default_format { | ||
| 283 | |||
| 284 | $_[0]->ds->node_attr($_[0], 'default_format'); | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | # document_vocabs ( extended ) | ||
| 289 | # | ||
| 290 | # Return a string in POD format documenting the vocabularies allowed for the | ||
| 291 | # path associated with this request. If $extended is true, then include a | ||
| 292 | # text description of each vocabulary. | ||
| 293 | |||
| 294 | sub document_vocabs { | ||
| 295 | |||
| 296 | my ($request, $options) = @_; | ||
| 297 | |||
| 298 | $options = {} unless ref $options; | ||
| 299 | my $path = $options->{all} ? '/' : $request->node_path; | ||
| 300 | |||
| 301 | return $request->ds->document_vocabs($path, $options); | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | # pod_B, pod_I, pod_C | ||
| 306 | # | ||
| 307 | # These functions are intended for use as filters. They take a list of | ||
| 308 | # arguments and return a string consisting of the list items joined by commas | ||
| 309 | # and surrounded by the appropriate formatting code. | ||
| 310 | |||
| 311 | sub pod_B { | ||
| 312 | return "B<" . join(', ', @_) . ">"; | ||
| 313 | } | ||
| 314 | |||
| 315 | |||
| 316 | sub pod_C { | ||
| 317 | return "C<" . join(', ', @_) . ">"; | ||
| 318 | } | ||
| 319 | |||
| 320 | |||
| 321 | sub pod_I { | ||
| 322 | return "I<" . join(', ', @_) . ">"; | ||
| 323 | } | ||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | 1; |