API-MikroTik

 view release on metacpan or  search on metacpan

lib/API/MikroTik/Response.pm  view on Meta::CPAN

package API::MikroTik::Response;
use Mojo::Base '-base';

use API::MikroTik::Sentence;

has data     => sub { [] };
has sentence => sub { API::MikroTik::Sentence->new() };

sub parse {
    my ($self, $buff) = @_;

    my $data = [];

    my $sentence = $self->sentence;
    while ($$buff) {
        my $words = $sentence->fetch($buff);
        last if $sentence->is_incomplete;

        my $item = {'.tag' => '', '.type' => (shift @$words)};
        push @$data, $item;

        next unless @$words;

        while (my $w = shift @$words) {
            $item->{$1 || $2} = $3 if ($w =~ /^(?:=([^=]+)|(\.tag))=(.*)/);
        }
    }

    return $self->{data} = $data;
}

1;


=encoding utf8

=head1 NAME

API::MikroTik::Response - Parse responses from a buffer

=head1 SYNOPSIS

  use API::MikroTik::Response;

  my $response = API::MikroTik::Response->new();

  my $list = $response->parse(\$buff);
  for my $re (@$list) {
      my ($type, $tag) = delete @{$re}{'.type'. '.tag'};
      say "$_ => $re->{$_}" for keys %$re;
  }

=head1 DESCRIPTION

Parser for API protocol responses.

=head1 ATTRIBUTES

L<API::MikroTik::Response> implements the following attributes.

=head2 data

  my $items = $response->data;

Sentences fetched in last operation;

=head2 sentence

  my $sentence = $response->sentence;
  $response->sentence(API::MikroTik::Sentence->new());

L<API::MikroTik::Sentence> object used to decode sentences from network buffer.

=head1 METHODS

=head2 parse

  my $list = $response->parse(\$buff);

Parses data from a buffer and returns list of hashrefs with attributes for each
sentence. There are some special attributes:

=over 2

=item '.tag'

  '.tag' => 1

Reply tag.

=item '.type'

  '.type' => '!re'

Reply type.



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