File: Mojolicious.pm

package info (click to toggle)
libjson-validator-perl 0.92%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 408 kB
  • ctags: 89
  • sloc: perl: 1,009; makefile: 6
file content (89 lines) | stat: -rw-r--r-- 1,878 bytes parent folder | download
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
package JSON::Validator::OpenAPI::Mojolicious;
use Mojo::Base 'JSON::Validator::OpenAPI';

sub _get_request_data {
  my ($self, $c, $in) = @_;

  return $c->req->url->query->to_hash  if $in eq 'query';
  return $c->match->stack->[-1]        if $in eq 'path';
  return $c->req->body_params->to_hash if $in eq 'formData';
  return $c->req->headers->to_hash     if $in eq 'header';
  return $c->req->json                 if $in eq 'body';
  $self->_invalid_in($in);
}

sub _get_request_uploads {
  my ($self, $c, $name) = @_;

  return @{$c->req->every_upload($name)};
}

sub _get_response_data {
  my ($self, $c, $in) = @_;

  if ($in eq 'header') {
    return $c->res->headers->to_hash(1);
  }
  else {
    $self->_invalid_in($in);
  }
}

sub _set_request_data {
  my ($self, $c, $in, $name => $value) = @_;

  if ($in eq 'query') {
    $c->req->url->query([$name => $value]);
    $c->req->params->merge($name => $value);
  }
  elsif ($in eq 'path') {
    $c->stash($name => $value);
  }
  elsif ($in eq 'formData') {
    $c->req->params->merge($name => $value);
    $c->req->body_params->merge($name => $value);
  }
  elsif ($in eq 'header') {
    $c->req->headers->header($name => $value);
  }
  elsif ($in eq 'body') { }    # no need to write body back
  else {
    $self->_invalid_in($in);
  }
}

sub _set_response_data {
  my ($self, $c, $in, $name => $value) = @_;

  if ($in eq 'header') {
    $c->res->headers->header($name => ref $value ? @$value : $value);
  }
  else {
    $self->_invalid_in($in);
  }
}

1;

=encoding utf8

=head1 NAME

JSON::Validator::OpenAPI::Mojolicious - Request/response adapter for Mojolicious

=head1 SYNOPSIS

See L<JSON::Validator::OpenAPI/SYNOPSIS>.

=head1 DESCRIPTION

This module contains private methods to get/set request/response data for
L<Mojolicious>.

=head1 SEE ALSO

L<Mojolicious::Plugin::OpenAPI>.

L<JSON::Validator>.

=cut