blib/lib/DAPNET/API.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 144 CC 10 SCAR 27.5
CC
10
Cov
0%
send_individual_call
30.0
send_rubric_content
30.0
new
6.0
CRAP 15.7
1 use strict;
2  
3 package DAPNET::API;
4  
5 use LWP::UserAgent;
6 use JSON;
7 use MIME::Base64;
8 #use LWP::ConsoleLogger::Everywhere ();
9  
10 =head1 NAME
11  
12 DAPNET::API - Use the DAPNET API from Perl
13  
14 =head1 SYNOPSIS
15  
16 my($dapnetobj) = DAPNET::API->new({
17 DAPNET_USERNAME => '<username>',
18 DAPNET_PW => '<password>',
19 CALLSIGN => '<your callsign>'
20 });
21  
22 $ret = $dapnetobj->send_rubric_content(<text to send>,<rubric name>.<transmitter group name>,<sequence number 1 - 10>);
23  
24 $ret = $dapnetobj->send_individual_call(<text>,<destination callsign>,<transmitter group name>,<emergency boolean>));
25  
26 if ($ret) {
27 print('HTTP Error response: '.$ret."\n");
28 } else {
29 print("Message sent\n");
30 }
31  
32 =head1 DESCRIPTION
33  
34 Implementation of the DAPNET REST API in Perl.
35  
36 =head1 AUTHOR
37  
38 Simon (G7RZU) <simon@gb7fr.org.uk>
39  
40 =cut
41  
42 use vars qw($VERSION);
43 #Define version
44 $VERSION = '0.5';
45  
46 =head1 METHODS
47  
48 This section describes the methods that are implemented and their use.
49  
50 =head2 new
51  
52 Instantiates new object. Pass a hash reference with options as follows
53  
54 my($dapnetobj) = DAPNET::API->new({
55 DAPNET_USERNAME => '<username>',
56 DAPNET_PW => '<password>',
57 CALLSIGN => '<your callsign>'
58 });
59  
60 returns an object if sucessful or false if not sucessful
61  
62 =cut
63  
64  
65 sub new {
66 my($class) = shift;
67 my($self) = shift;
68 if (!exists($self->{DAPNET_USERNAME}) || !exists($self->{DAPNET_PW}) || !exists($self->{CALLSIGN})) {
69 return(0);
70 }
71 bless($self,$class);
72 $self->{_CALL_LEN} = length($self->{CALLSIGN});
73 return($self);
74 };
75  
76 sub _build_request {
77 my($self) = shift;
78 my($json) = shift;
79 my($type) = shift;
80 my($username) = $self->{DAPNET_USERNAME};
81 my($pw) = $self->{DAPNET_PW};
82 my($uri) = 'http://www.hampager.de:8080/'.$type;
83 my($req) = HTTP::Request->new(
84 POST => $uri
85 );
86 $req->header( 'Content-Type' => 'application/json',
87 'Authorization'=>'Basic ' . encode_base64($username.':'.$pw)
88 );
89
90 $req->content( $json );
91 return($req);
92
93 };
94  
95 sub _json_individual_call {
96 my($self) = shift;
97 my($text,$to,$txgroup,$emergency) = @_;
98 my($jsonobj) = JSON->new;
99  
100 my $json = $jsonobj->encode ({
101 'text' => $text,
102 'callSignNames' => [$to],
103 'transmitterGroupNames' => [$txgroup],
104 'emergency' => $emergency
105 });
106 return($json);
107 };
108  
109 sub _json_rubric_content {
110 my($self) = shift;
111 my($text,$rubric,$number) = @_;
112 my($jsonobj) = JSON->new;
113  
114 my $json = $jsonobj->encode ({
115 'text' => $text,
116 'rubricName'=> $rubric,
117 'number' => $number
118 });
119 return($json);
120 };
121  
122 =head2 json_response
123  
124 Returns a hash ref to the last JSON response or undef if there is none
125
126 $jsonhashref = $jsonobj->json_response;
127  
128 =cut
129  
130 sub json_response {
131 my($self) = shift;
132 return($self->{_JSONRESPONSEREF});
133 };
134  
135 =head2 send_individual_call
136  
137 Send a call to a single callsign
138  
139 $ret = $dapnetobj->send_individual_call(<text>,<destination callsign>,<transmitter group name>,<emergency boolean>));
140  
141 Returns 0 on sucess or the HTTP error string on error.
142  
143 =cut
144  
145 sub send_individual_call {
146  
147 my $self = shift;
148 my($text,$to,$txgroup,$emergency) = @_;
149
150 my($ua) = LWP::UserAgent->new;
151 my($jsonresobj) = JSON->new;
152 my($i) = 1;
153 my($json);
154
155 if (length($text) <= (80 - $self->{_CALL_LEN})) {
156 my($json) = $self->_json_individual_call($self->{CALLSIGN}.':'.$text,$to,$txgroup,$emergency);
157 my($req) = $self->_build_request($json,'calls');
158 my($res) = $ua->request($req);
159 $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content);
160 if (!$res->is_success) {
161 return($res->status_line);
162 };
163 } else {
164 while (my $substr = substr($text,0,(80 - $self->{_CALL_LEN}),'')) {
165 if ($i == 1) {
166 $json = $self->_json_individual_call($self->{CALLSIGN}.':'.$substr.'...',$to,$txgroup,$emergency);
167 } else {
168 $json = $self->_json_individual_call($self->{CALLSIGN}.':'.'...'.$substr,$to,$txgroup,$emergency);
169 };
170 my($req) = $self->_build_request($json);
171 my($res) = $ua->request($req);
172 $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content);
173 if (!$res->is_success) {
174 return($res->status_line);
175 };
176 $i++;
177 };
178
179 };
180
181
182 return(0);
183  
184 }
185  
186 =head2 send_rubric_content
187  
188 Sends a message to a rubric
189  
190 $ret = $dapnetobj->send_rubric_content(<text to send>,<rubric name>.<transmitter group name>,<sequence number 1 - 10>);
191  
192 Returns 0 on sucess or the HTTP error string on error.
193  
194 =cut
195  
196  
197 sub send_rubric_content {
198 my $self = shift;
199 my($text,$rubric,$number) = @_;
200 my($jsonresobj) = JSON->new;
201 my($ua) = LWP::UserAgent->new;
202 my($i) = 1;
203 my($json);
204
205 if (length($text) <= (80 - $self->{_CALL_LEN})) {
206 my($json) = $self->_json_rubric_content($self->{CALLSIGN}.':'.$text,$rubric,$number);
207 my($req) = $self->_build_request($json,'news');
208 my($res) = $ua->request($req);
209 $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content);
210 if (!$res->is_success) {
211 return($res->status_line);
212 };
213 } else {
214 while (my $substr = substr($text,0,(80 - $self->{_CALL_LEN}),'')) {
215 if ($i == 1) {
216 $json = $self->_json_rubrik_content($self->{CALLSIGN}.':'.$substr.'...',$rubric,$number);
217 } else {
218 $json = $self->_json_rubrik_content($self->{CALLSIGN}.':'.'...'.$substr,$rubric,$number);
219 };
220 my($req) = $self->_build_request($json);
221 my($res) = $ua->request($req);
222 $self->{_JSONRESPONSEREF} = $jsonresobj->decode($res->decoded_content);
223 if (!$res->is_success) {
224 return($res->status_line);
225 };
226 $i++;
227 };
228
229 };
230
231
232 return(0);
233 };
234  
235 1;