| 1 | # | ||
| 2 | # Web::DataService::Format | ||
| 3 | # | ||
| 4 | # This module provides a role that is used by 'Web::DataService'. It implements | ||
| 5 | # routines for defining and documenting output formats. | ||
| 6 | # | ||
| 7 | # Author: Michael McClennen | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | |||
| 11 | package Web::DataService::Format; | ||
| 12 | |||
| 13 | use Carp 'croak'; | ||
| 14 | use Data::Dumper; | ||
| 15 | |||
| 16 | use Moo::Role; | ||
| 17 | |||
| 18 | |||
| 19 | our (%FORMAT_DEF) = (name => 'ignore', | ||
| 20 | suffix => 'single', | ||
| 21 | title => 'single', | ||
| 22 | content_type => 'single', | ||
| 23 | disposition => 'single', | ||
| 24 | uses_header => 'single', | ||
| 25 | is_text => 'single', | ||
| 26 | encode_as_text => 'single', | ||
| 27 | default_vocab => 'single', | ||
| 28 | doc_node => 'single', | ||
| 29 | module => 'single', | ||
| 30 | package => 'single', | ||
| 31 | doc_string => 'single', | ||
| 32 | undocumented => 'single', | ||
| 33 | disabled => 'single'); | ||
| 34 | |||
| 35 | our (%FORMAT_CT) = (json => 'application/json', | ||
| 36 | txt => 'text/plain', | ||
| 37 | tsv => 'text/tab-separated-values', | ||
| 38 | csv => 'text/csv', | ||
| 39 | xml => 'text/xml'); | ||
| 40 | |||
| 41 | our (%FORMAT_CLASS) = (json => 'Web::DataService::Plugin::JSON', | ||
| 42 | txt => 'Web::DataService::Plugin::Text', | ||
| 43 | tsv => 'Web::DataService::Plugin::Text', | ||
| 44 | csv => 'Web::DataService::Plugin::Text', | ||
| 45 | xml => 'Web::DataService::Plugin::XML'); | ||
| 46 | |||
| 47 | |||
| 48 | # define_format ( attrs... ) | ||
| 49 | # | ||
| 50 | # Define one or more formats for data service responses. | ||
| 51 | |||
| 52 | sub define_format { | ||
| 53 | |||
| 54 | my $ds = shift; | ||
| 55 | |||
| 56 | my ($last_node); | ||
| 57 | |||
| 58 | # Now we go through the rest of the arguments. Hashrefs define new | ||
| 59 | # vocabularies, while strings add to the documentation of the vocabulary | ||
| 60 | # whose definition they follow. | ||
| 61 | |||
| 62 | foreach my $item (@_) | ||
| 63 | { | ||
| 64 | # A hashref defines a new vocabulary. | ||
| 65 | |||
| 66 | if ( ref $item eq 'HASH' ) | ||
| 67 | { | ||
| 68 | # Make sure the attributes include 'name'. | ||
| 69 | |||
| 70 | my $name = $item->{name}; | ||
| 71 | |||
| 72 | croak "define_format: the attributes must include 'name'" unless defined $name; | ||
| 73 | |||
| 74 | # Make sure this format was not already defined by a previous call. | ||
| 75 | |||
| 76 | croak "define_format: '$name' was already defined" if defined $ds->{format}{$name}; | ||
| 77 | |||
| 78 | # Create a new record to represent this format and check the attributes. | ||
| 79 | |||
| 80 | my $record = bless { name => $name }, 'Web::DataService::Format'; | ||
| 81 | |||
| 82 | foreach my $k ( keys %$item ) | ||
| 83 | { | ||
| 84 | croak "define_format: invalid attribute '$k'" unless $FORMAT_DEF{$k}; | ||
| 85 | |||
| 86 | my $v = $item->{$k}; | ||
| 87 | |||
| 88 | if ( $k eq 'default_vocab' && defined $v && $v ne '' ) | ||
| 89 | { | ||
| 90 | croak "define_format: unknown vocabulary '$v'" | ||
| 91 | unless ref $ds->{vocab}{$v}; | ||
| 92 | |||
| 93 | croak "define_format: cannot default to disabled vocabulary '$v'" | ||
| 94 | if $ds->{vocab}{$v}{disabled} and not $item->{disabled}; | ||
| 95 | } | ||
| 96 | |||
| 97 | $record->{$k} = $item->{$k}; | ||
| 98 | } | ||
| 99 | |||
| 100 | # Set defaults and check values. | ||
| 101 | |||
| 102 | $record->{content_type} ||= $FORMAT_CT{$name}; | ||
| 103 | $record->{uses_header} //= 1 if $name eq 'txt' || $name eq 'tsv' || $name eq 'csv'; | ||
| 104 | $record->{is_text} //= 1 if $record->{content_type} =~ /(x(?:ht)?ml|text|json|javascript)/ | ||
| 105 | || $record->{encode_as_text}; | ||
| 106 | |||
| 107 | croak "define_format: you must specify an HTTP content type for format '$name' using the attribute 'content_type'" | ||
| 108 | unless $record->{content_type}; | ||
| 109 | |||
| 110 | $record->{package} //= $record->{module}; | ||
| 111 | $record->{package} //= $FORMAT_CLASS{$name}; | ||
| 112 | |||
| 113 | croak "define_format: you must specify a package to implement format '$name' using the attribute 'module'" | ||
| 114 | unless defined $record->{package}; | ||
| 115 | |||
| 116 | $record->{module} //= $record->{package}; | ||
| 117 | |||
| 118 | # Make sure that the module is loaded, unless the format is disabled. | ||
| 119 | |||
| 120 | if ( $record->{module} && ! $record->{disabled} ) | ||
| 121 | { | ||
| 122 | my $filename = $record->{module}; | ||
| 123 | $filename =~ s{::}{/}g; | ||
| 124 | $filename .= '.pm' unless $filename =~ /\.pm$/; | ||
| 125 | |||
| 126 | require $filename; | ||
| 127 | } | ||
| 128 | |||
| 129 | # Now store the record as a response format for this data service. | ||
| 130 | |||
| 131 | $ds->{format}{$name} = $record; | ||
| 132 | push @{$ds->{format_list}}, $name unless $record->{disabled}; | ||
| 133 | $last_node = $record; | ||
| 134 | } | ||
| 135 | |||
| 136 | # A scalar is taken to be a documentation string. | ||
| 137 | |||
| 138 | elsif ( not ref $item ) | ||
| 139 | { | ||
| 140 | $ds->add_node_doc($last_node, $item); | ||
| 141 | } | ||
| 142 | |||
| 143 | else | ||
| 144 | { | ||
| 145 | croak "define_format: the arguments to this routine must be hashrefs and strings"; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | croak "define_format: you must include at least one hashref of attributes" | ||
| 150 | unless $last_node; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | # list_formats ( ) | ||
| 155 | # | ||
| 156 | # Return the list of names of all the formats that have been defined for this | ||
| 157 | # data service. | ||
| 158 | |||
| 159 | sub list_formats { | ||
| 160 | |||
| 161 | my ($ds) = @_; | ||
| 162 | return @{$ds->{format_list}}; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | # valid_format ( ) | ||
| 167 | # | ||
| 168 | # Return a code reference (actually a reference to a closure) that can be used | ||
| 169 | # in a parameter rule to validate a format-selecting parameter. All | ||
| 170 | # non-disabled formats are included. | ||
| 171 | |||
| 172 | sub format_validator { | ||
| 173 | |||
| 174 | my ($self) = @_; | ||
| 175 | |||
| 176 | # The ENUM_VALUE subroutine is defined by HTTP::Validate.pm. | ||
| 177 | |||
| 178 | return ENUM_VALUE(@{$self->{format_list}}); | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | # document_formats ( path, options ) | ||
| 183 | # | ||
| 184 | # Return a string containing POD documentation of the response formats that | ||
| 185 | # are allowed for the request path. If the root path '/' is specified, then | ||
| 186 | # document all of the formats enabled for this data service regardless of | ||
| 187 | # whether they are actually allowed for that path. But formats marked as | ||
| 188 | # undocumented are never shown. If the option 'extended' is specified, then | ||
| 189 | # include the text description of each format. | ||
| 190 | |||
| 191 | sub document_formats { | ||
| 192 | |||
| 193 | my ($ds, $path, $options) = @_; | ||
| 194 | |||
| 195 | $options ||= {}; | ||
| 196 | $path ||= '/'; | ||
| 197 | |||
| 198 | # If no formats have been defined, return a note to that effect. | ||
| 199 | |||
| 200 | return "MSG_FORMAT_NONE_DEFINED" | ||
| 201 | unless ref $ds->{format_list} eq 'ARRAY'; | ||
| 202 | |||
| 203 | # Now figure out which formats to document. If the path is '/', then | ||
| 204 | # document all of them. Otherwise, go thorugh the list of defined formats | ||
| 205 | # in order, filtering out those which are not allowed for this path. The | ||
| 206 | # reason for doing it this way is so that the formats will always be | ||
| 207 | # listed in the order defined, instead of the arbitrary hash order. | ||
| 208 | |||
| 209 | my @formats; | ||
| 210 | |||
| 211 | if ( $path eq '/' ) | ||
| 212 | { | ||
| 213 | @formats = grep { ! $ds->{format}{$_}{undocumented} } @{$ds->{format_list}}; | ||
| 214 | return "MSG_FORMAT_NONE_DEFINED" unless @formats; | ||
| 215 | } | ||
| 216 | |||
| 217 | else | ||
| 218 | { | ||
| 219 | my $allowed = $ds->node_attr($path, 'allow_format'); | ||
| 220 | |||
| 221 | return "MSG_FORMAT_NONE_ALLOWED" | ||
| 222 | unless ref $allowed eq 'HASH'; | ||
| 223 | |||
| 224 | @formats = grep { $allowed->{$_} && ! $ds->{format}{$_}{undocumented} } @{$ds->{format_list}}; | ||
| 225 | return "MSG_FORMAT_NONE_ALLOWED" unless @formats; | ||
| 226 | } | ||
| 227 | |||
| 228 | # Go through the list of defined formats in order, | ||
| 229 | |||
| 230 | my @paths = grep { $ds->{format}{$_}{doc_node} } @formats; | ||
| 231 | |||
| 232 | my $name_header = $ds->has_feature('format_suffix') ? 'Suffix' : 'Name'; | ||
| 233 | my $ext_header = $options->{extended} || ! @paths ? "| Description" : ''; | ||
| 234 | my $doc_header = @paths ? "| Documentation" : ''; | ||
| 235 | |||
| 236 | my $doc = "=for wds_table_header Format* | $name_header $doc_header $ext_header\n\n"; | ||
| 237 | $doc .= "=over 4\n\n"; | ||
| 238 | |||
| 239 | FORMAT: | ||
| 240 | foreach my $name (@formats) | ||
| 241 | { | ||
| 242 | my $frec = $ds->{format}{$name}; | ||
| 243 | my $title = $frec->{title} || $frec->{name}; | ||
| 244 | my $doc_link = $ds->node_link($frec->{doc_node}) if $frec->{doc_node}; | ||
| 245 | my $name_or_suffix = $ds->has_feature('format_suffix') ? ".$frec->{name}" : $frec->{name}; | ||
| 246 | |||
| 247 | next FORMAT if $frec->{undocumented}; | ||
| 248 | |||
| 249 | $doc .= "=item $title | C<$name_or_suffix>"; | ||
| 250 | $doc .= " | $doc_link" if $doc_link && @paths && $options->{extended}; | ||
| 251 | $doc .= "\n\n"; | ||
| 252 | |||
| 253 | if ( $options->{extended} || ! @paths ) | ||
| 254 | { | ||
| 255 | $doc .= "$frec->{doc_string}\n\n" if $frec->{doc_string}; | ||
| 256 | } | ||
| 257 | |||
| 258 | elsif ( $doc_link ) | ||
| 259 | { | ||
| 260 | $doc .= "$doc_link\n\n"; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | $doc .= "=back"; | ||
| 265 | |||
| 266 | return $doc; | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | 1; |