<commit_before>package Mojolicious::Plugin::Nexmo;

use Mojo::Base 'Mojolicious::Plugin';
use Mojo::URL;
use Mojo::JSON;

use Data::Dumper;
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;

our $VERSION = '0.01';

sub register {
    my ($self, $app, $conf) = @_;
    my $base_url = Mojo::URL->new('https://rest.nexmo.com/sms/json');

    # Required params
    for my $param (qw/api_key api_secret/) {
        return $app->log->error("Param '$param' is required for Nexmo") unless $conf->{$param};
        $base_url->query([$param => $conf->{$param}]);
    }

    $app->helper(send_sms => sub {
        my $c = shift;
        my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
        my $args = {@_};
        my $url = $base_url->clone;

        for my $param (qw/from to text/) {
            my $value = $args->{$param} // $conf->{$param};
            unless(defined $value && length $value > 0) {
                return &$cb("Param '$param' is required for Nexmo", undef) if defined $cb;
                die "Param '$param' is required for Nexmo";
            }
            $url->query([$param => $value]);
        }

        # Non blocking
        return $c->ua->get($url => sub {
            my ($ua, $tx) = @_;
            $c->app->log->debug('Nexmo response: ' . Dumper $tx->res->json) if $ENV{'MOJOLICIOUS_NEXMO_DEBUG'};
            &$cb(undef, $tx->res->json);
        }) if $cb;
        my $tx = $c->ua->get($url);
        $c->app->log->debug('Nexmo response: ' . Dumper $tx->res->json) if $ENV{'MOJOLICIOUS_NEXMO_DEBUG'};
        return $tx->res->json;
    });

}


1;

__END__

=head1 NAME

Mojolicious::Plugin::Nexmo - Asynchronious send SMS from Nexmo provider.

=head1 SYNOPSIS

    use Mojolicious::Plugin::Nexmo;
    plugin Nexmo => {<commit_msg>fix bug with call callback function<commit_after>package Mojolicious::Plugin::Nexmo;

use Mojo::Base 'Mojolicious::Plugin';
use Mojo::URL;
use Mojo::JSON;

use Data::Dumper;
$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;

our $VERSION = '0.01';

sub register {
    my ($self, $app, $conf) = @_;
    my $base_url = Mojo::URL->new('https://rest.nexmo.com/sms/json');

    # Required params
    for my $param (qw/api_key api_secret/) {
        return $app->log->error("Param '$param' is required for Nexmo") unless $conf->{$param};
        $base_url->query([$param => $conf->{$param}]);
    }

    $app->helper(send_sms => sub {
        my $c = shift;
        my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
        my $args = {@_};
        my $url = $base_url->clone;

        for my $param (qw/from to text/) {
            my $value = $args->{$param} // $conf->{$param};
            unless(defined $value && length $value > 0) {
                return $c->$cb("Param '$param' is required for Nexmo", undef) if defined $cb;
                die "Param '$param' is required for Nexmo";
            }
            $url->query([$param => $value]);
        }

        # Non blocking
        return $c->ua->get($url => sub {
            my ($ua, $tx) = @_;
            $c->app->log->debug('Nexmo response: ' . Dumper $tx->res->json) if $ENV{'MOJOLICIOUS_NEXMO_DEBUG'};
            $c->$cb(undef, $tx->res->json);
        }) if $cb;
        my $tx = $c->ua->get($url);
        $c->app->log->debug('Nexmo response: ' . Dumper $tx->res->json) if $ENV{'MOJOLICIOUS_NEXMO_DEBUG'};
        return $tx->res->json;
    });

}


1;

__END__

=head1 NAME

Mojolicious::Plugin::Nexmo - Asynchronious send SMS from Nexmo provider.

=head1 SYNOPSIS

    use Mojolicious::Plugin::Nexmo;
    plugin Nexmo => {
{'fraction_non_alphanumeric': 0.18883878241262683, 'fraction_numerical': 0.005636978579481398, 'mean_word_length': 2.4190751445086707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 75, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6177973', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1259, 'n_words': 293}