File Coverage

blib/lib/API/MikroTik/Response.pm
Criterion Covered Total %
statement 18 18 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 3 3 100.0
pod 1 1 100.0
total 29 31 93.5


line stmt bran cond sub pod time code
1             package API::MikroTik::Response;
2 4     4   75407 use Mojo::Base '-base';
  4         158466  
  4         36  
3              
4 4     4   2912 use API::MikroTik::Sentence;
  4         14  
  4         1373  
5              
6             has data => sub { [] };
7             has sentence => sub { API::MikroTik::Sentence->new() };
8              
9             sub parse {
10 48     48 1 10310 my ($self, $buff) = @_;
11              
12 48         113 my $data = [];
13              
14 48         144 my $sentence = $self->sentence;
15 48         337 while ($$buff) {
16 70         242 my $words = $sentence->fetch($buff);
17 70 100       187 last if $sentence->is_incomplete;
18              
19 66         249 my $item = {'.tag' => '', '.type' => (shift @$words)};
20 66         140 push @$data, $item;
21              
22 66 100       136 next unless @$words;
23              
24 65         165 while (my $w = shift @$words) {
25 141 50 66     1370 $item->{$1 || $2} = $3 if ($w =~ /^(?:=([^=]+)|(\.tag))=(.*)/);
26             }
27             }
28              
29 48         227 return $self->{data} = $data;
30             }
31              
32             1;
33              
34              
35             =encoding utf8
36              
37             =head1 NAME
38              
39             API::MikroTik::Response - Parse responses from a buffer
40              
41             =head1 SYNOPSIS
42              
43             use API::MikroTik::Response;
44              
45             my $response = API::MikroTik::Response->new();
46              
47             my $list = $response->parse(\$buff);
48             for my $re (@$list) {
49             my ($type, $tag) = delete @{$re}{'.type'. '.tag'};
50             say "$_ => $re->{$_}" for keys %$re;
51             }
52              
53             =head1 DESCRIPTION
54              
55             Parser for API protocol responses.
56              
57             =head1 ATTRIBUTES
58              
59             L implements the following attributes.
60              
61             =head2 data
62              
63             my $items = $response->data;
64              
65             Sentences fetched in last operation;
66              
67             =head2 sentence
68              
69             my $sentence = $response->sentence;
70             $response->sentence(API::MikroTik::Sentence->new());
71              
72             L object used to decode sentences from network buffer.
73              
74             =head1 METHODS
75              
76             =head2 parse
77              
78             my $list = $response->parse(\$buff);
79              
80             Parses data from a buffer and returns list of hashrefs with attributes for each
81             sentence. There are some special attributes:
82              
83             =over 2
84              
85             =item '.tag'
86              
87             '.tag' => 1
88              
89             Reply tag.
90              
91             =item '.type'
92              
93             '.type' => '!re'
94              
95             Reply type.
96              
97             =back
98              
99             =head1 SEE ALSO
100              
101             L
102              
103             =cut
104