Apache2-API

 view release on metacpan or  search on metacpan

lib/Apache2/API/Status.pm  view on Meta::CPAN

    ( my $type = $STATUS_TO_TYPE->{ $code } ) =~ s/_/$sep/g;
    return( $type );
}

# Returns a status line for a given code
# e.g. status_message( 404 ) would yield "Not found"
sub status_message
{
    my $self = shift( @_ );
    my( $code, $lang );
    if( scalar( @_ ) == 2 )
    {
        ( $code, $lang ) = @_;
    }
    else
    {
        $code = shift( @_ );
        $lang = 'en_GB';
    }
    $lang = 'en_GB' if( !exists( $HTTP_CODES->{ $lang } ) );
    my $ref = $HTTP_CODES->{ $lang };
    return( $ref->{ $code } );
}

sub supported_languages
{
    my $self = shift( @_ );
    return( [sort( keys( %$HTTP_CODES ) )] );
}

sub _min_max
{
    my $this = shift( @_ );
    my( $min, $max, $code ) = @_;
    return( $this->error( "A 3 digit code is required." ) ) if( !defined( $code ) || $code !~ /^\d{3}$/ );
    return( $code >= $min && $code < $max );
}

# NOTE: sub FREEZE is inherited

sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }

sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }

# NOTE: sub THAW is inherited

1;
# NOTE: POD
__END__

=encoding utf8

=head1 NAME

Apache2::API::Status - Apache2 Status Codes

=head1 SYNOPSIS

    use Apache2::API::Status ':common';
    use Apache2::API::Status ':all';
    say Apache2::API::Status::HTTP_TOO_MANY_REQUESTS;
    # returns code 429


    use Apache2::API::Status;
    # in German: Zu viele Anfragen
    say $Apache2::API::Status::HTTP_CODES->{de_DE}->{429};
    # same
    say $Apache2::API::Status::HTTP_CODES->{de}->{429};
    # In English: Too Many Requests
    say $Apache2::API::Status::HTTP_CODES->{en_GB}->{429};
    # same
    say $Apache2::API::Status::HTTP_CODES->{en}->{429};
    # in French: Trop de requête
    say $Apache2::API::Status::HTTP_CODES->{fr_FR}->{429};
    # same
    say $Apache2::API::Status::HTTP_CODES->{fr}->{429};
    # In Japanese: リクエスト過大で拒否した
    say $Apache2::API::Status::HTTP_CODES->{ja_JP}->{429};
    # same
    say $Apache2::API::Status::HTTP_CODES->{ja}->{429};
    # In Korean: 너무 많은 요청
    say $Apache2::API::Status::HTTP_CODES->{ko_KR}->{429};
    # same
    say $Apache2::API::Status::HTTP_CODES->{ko}->{429};
    # In Russian: слишком много запросов
    say $Apache2::API::Status::HTTP_CODES->{ru_RU}->{429};
    # same
    say $Apache2::API::Status::HTTP_CODES->{ru}->{429};
    # In simplified Chinese: 太多请求
    say $Apache2::API::Status::HTTP_CODES->{zh_CN}->{429};
    # In Taiwanese (traditional) Chinese: 太多請求
    say $Apache2::API::Status::HTTP_CODES->{zh_TW}->{429};

But maybe more simply:

    my $status = Apache2::API::Status->new;
    say $status->status_message( 429 => 'ja_JP' );
    # Or without the language code parameter, it will default to en_GB
    say $status->status_message( 429 );

    # Is success
    say $status->is_info( 102 ); # true
    say $status->is_success( 200 ); # true
    say $status->is_redirect( 302 ); # true
    say $status->is_error( 404 ); # true
    say $status->is_client_error( 403 ); # true
    say $status->is_server_error( 501 ); # true

=head1 VERSION

    v0.2.1

=head1 DESCRIPTION

This module allows to get the localised version of the HTTP status for a given code for currently supported languages: fr_FR (French), en_GB (British English) and ja_JP (Japanese), de_DE (German), ko_KR (Korean), ru_RU (Russian), zh_CN (simplified Ch...

It also provides some functions to check if a given code is an information, success, redirect, error, client error or server error code.

It provides a full set of constants to use and import.

Finally, it adds a few more C<Apache2::Const>. See L</CONSTANTS> below.

=head1 METHODS

=head2 init

Creates an instance of L<Apache2::API::Status> and returns the object.

=head2 convert_short_lang_to_long

Given a 2 characters language code (not case sensitive) and this will return its iso 639 5 characters equivalent for supported languages.

For example:

    Apache2::API::Status->convert_short_lang_to_long( 'zh' );
    # returns: zh_TW

=head2 is_cacheable_by_default

Return true if the 3-digits code provided indicates that a response is cacheable by default, and it can be reused by a cache with heuristic expiration. All other status codes are not cacheable by default. See L<RFC 7231 - HTTP/1.1 Semantics and Conte...

=head2 is_client_error

Returns true if the 3-digits code provided is between 400 and 500

=head2 is_error

Returns true if the 3-digits code provided is between 400 and 600

=head2 is_info

Returns true if the 3-digits code provided is between 100 and 200

=head2 is_redirect

Returns true if the 3-digits code provided is between 300 and 400

=head2 is_server_error

Returns true if the 3-digits code provided is between 500 and 600

=head2 is_success

Returns true if the 3-digits code provided is between 200 and 300

=head2 status_message



( run in 0.300 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )