Mojolicious-Plugin-MoreHelpers

 view release on metacpan or  search on metacpan

lib/Mojolicious/Plugin/MoreHelpers.pm  view on Meta::CPAN

    $c->render(json => { }, status => 422);
  });

  $app->helper('reply_json.locked' => sub {
    my ($c, %headers) = @_;

    $reply_headers->($c, \%headers, "error.temporary_locked");
    $c->render(json => { }, status => 423);
  });

  $app->helper('reply_json.rate_limit' => sub {
    my ($c, %headers) = @_;

    $reply_headers->($c, \%headers, "error.too_many_requests");
    $c->render(json => { }, status => 429);
  });

  $app->helper('reply_json.unavailable' => sub {
    my ($c, %headers) = @_;

    $reply_headers->($c, \%headers, "error.service_unavailable");
    $c->render(json => { }, status => 503);
  });

  $app->helper('reply_json.dispatch' => sub {
    my ($c, $status, $message, %headers) = @_;

    die "Wrong reply_json dispatch status\n"
      unless defined $status and not ref $status;

    die "Wrong reply_json dispatch message\n"
      unless defined $message and not ref $message;

    my %hash = (
      success       => sub { $c->reply_json->success(@_)        },
      bad_request   => sub { $c->reply_json->bad_request(@_)    },
      unauthorized  => sub { $c->reply_json->unauthorized(@_)   },
      forbidden     => sub { $c->reply_json->forbidden(@_)      },
      not_found     => sub { $c->reply_json->not_found(@_)      },
      unprocessable => sub { $c->reply_json->unprocessable(@_)  },
      locked        => sub { $c->reply_json->locked(@_)         },
      rate_limit    => sub { $c->reply_json->rate_limit(@_)     },
      unavailable   => sub { $c->reply_json->unavailable(@_)    },
    );

    my $sub = $hash{$status};

    die "Wrong reply_json dispatch status '$status'\n"
      unless defined $sub;

    $sub->(%headers, $conf->{header_message} => $message);
  });

  $app->validator->add_check(inet_address => sub {
    my ($v, $name, $value) = @_;

    return is_ip $value ? undef : 1;
  });

  $app->validator->add_check(email_address => sub {
    my ($validate, $name, $value) = @_;

    my ($email) = Email::Address->parse($value);
    return defined $email && $email->address ? undef : 1;
  });
}

1;

=encoding utf8

=head1 NAME

Mojolicious::Plugin::MoreHelpers - More helpers lacking in Mojolicious

=head1 SYNOPSIS

  # Mojolicious
  $app->plugin('MoreHelpers');

  # Mojolicious::Lite
  plugin 'MoreHelpers';

=head1 DESCRIPTION

L<Mojolicious::Plugin::MoreHelpers> is a mingle of helpers lacking in
L<Mojolicious> Web framework for REST-like APIs.

=head1 HELPERS

L<Mojolicious::Plugin::MoreHelpers> implements the following helpers.

=head2 route_params

  my $params = $c->route_params(@names);

Recursive collect current route params and his parents.

=head2 validation_json

  my $v = $c->validation_json;

Merge flat request JSON object with validation.

=head2 headers_response

  my $h = $c->headers_response(%headers);

Set multiple reponse headers in one time.

=head2 reply_json->success

  $c->reply_json->success($data, %headers);

Render the success JSON object with status code, depend on POST or GET request.

=head2 reply_json->bad_request

  $c->reply_json->bad_request(%headers);

Render empty JSON object with 400 Bad Request HTTP status.



( run in 0.461 second using v1.01-cache-2.11-cpan-39bf76dae61 )