| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package API::MikroTik::Response; | 
| 2 | 4 |  |  | 4 |  | 67662 | use Mojo::Base '-base'; | 
|  | 4 |  |  |  |  | 187419 |  | 
|  | 4 |  |  |  |  | 28 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 4 |  |  | 4 |  | 2769 | use API::MikroTik::Sentence; | 
|  | 4 |  |  |  |  | 13 |  | 
|  | 4 |  |  |  |  | 1230 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | has data     => sub { [] }; | 
| 7 |  |  |  |  |  |  | has sentence => sub { API::MikroTik::Sentence->new() }; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub parse { | 
| 10 | 48 |  |  | 48 | 1 | 9754 | my ($self, $buff) = @_; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 48 |  |  |  |  | 126 | my $data = []; | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 48 |  |  |  |  | 147 | my $sentence = $self->sentence; | 
| 15 | 48 |  |  |  |  | 299 | while ($$buff) { | 
| 16 | 70 |  |  |  |  | 215 | my $words = $sentence->fetch($buff); | 
| 17 | 70 | 100 |  |  |  | 174 | last if $sentence->is_incomplete; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 66 |  |  |  |  | 236 | my $item = {'.tag' => '', '.type' => (shift @$words)}; | 
| 20 | 66 |  |  |  |  | 134 | push @$data, $item; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 66 | 100 |  |  |  | 136 | next unless @$words; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 65 |  |  |  |  | 147 | while (my $w = shift @$words) { | 
| 25 | 141 | 50 | 66 |  |  | 1192 | $item->{$1 || $2} = $3 if ($w =~ /^(?:=([^=]+)|(\.tag))=(.*)/); | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 48 |  |  |  |  | 204 | 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 |  |  |  |  |  |  |  |