Mojolicious-Plugin-Vparam

 view release on metacpan or  search on metacpan

lib/Mojolicious/Plugin/Vparam/Address.pm  view on Meta::CPAN

package Mojolicious::Plugin::Vparam::Address;
use Mojo::Base -strict;
use Mojolicious::Plugin::Vparam::Common qw(load_class decode_json);
use Mojolicious::Plugin::Vparam::Numbers;

use Mojo::JSON;
use Digest::MD5                     qw(md5_hex);
use Encode                          qw(encode is_utf8);


sub new {
    my ($class, $opts) = @_;
    return bless $opts => $class;
}

=head2 parse $str

Parse address from string

=cut

sub parse {
    my ($class, $str) = @_;

    return undef unless defined $str;
    my ($full, $address, $lon, $lat, $md5, $id, $type, $lang, $opt);

    if( $str =~ m{^\s*\[} and $str =~ m{\]\s*$} ) {
        # JSON format
        my $json = decode_json $str;
        if( $json and 'ARRAY' eq ref($json)) {
            $full       = sprintf '%s : %s , %s',
                            $json->[2]//'', $json->[3]//'', $json->[4]//'';
            $address    = $json->[2];
            $lon        =
                Mojolicious::Plugin::Vparam::Numbers::parse_number $json->[3];
            $lat        =
                Mojolicious::Plugin::Vparam::Numbers::parse_number $json->[4];
            $id         = $json->[0];
            $type       = $json->[1];
            $lang       = $json->[5];
            $opt        = 'ARRAY' eq ref($json->[6])
                            ? $class->new($json->[6])
                            : $json->[6];
        }
    } else {
        # Text format
        ($full, $address, $lon, $lat, $md5) = $str =~ m{^
            (
                \s*
                # address
                (\S.*?)
                \s*:\s*
                # longitude
                (-?\d{1,3}(?:\.\d+)?)
                \s*,\s*
                # latitude
                (-?\d{1,3}(?:\.\d+)?)
                \s*
            )
            # md5
            (?:\[\s*(\w*)\s*\])?
            \s*
        $}x;
    }

    return $class->new([
        $address, $lon, $lat, $md5, $full, $id, $type, $lang, $opt
    ]);
}

=head2 check_secret $secret

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.424 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )