blib/lib/DAPNET/API.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 140 CC 10 SCAR 28.0
CC
10
Cov
0%
send_individual_call
30.0
send_rubric_content
30.0
new
6.0
CRAP 16.4
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.3';
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 send_individual_call
123  
124 Send a call to a single callsign
125  
126 $ret = $dapnetobj->send_individual_call(<text>,<destination callsign>,<transmitter group name>,<emergency boolean>));
127  
128 Returns 0 on sucess or the HTTP error string on error.
129  
130  
131 =cut
132  
133 sub send_individual_call {
134  
135 my $self = shift;
136 my($text,$to,$txgroup,$emergency) = @_;
137
138 my($ua) = LWP::UserAgent->new;
139 my($jsonobj);
140 my($i) = 1;
141 my($json);
142
143 if (length($text) <= (80 - $self->{_CALL_LEN})) {
144 my($json) = $self->_json_individual_call($self->{CALLSIGN}.':'.$text,$to,$txgroup,$emergency);
145 my($req) = $self->_build_request($json,'calls');
146 my($res) = $ua->request($req);
147 warn($res->status_line);
148 if (!$res->is_success) {
149 return($res->status_line);
150 };
151 } else {
152 while (my $substr = substr($text,0,(80 - $self->{_CALL_LEN}),'')) {
153 if ($i == 1) {
154 $json = $self->_json_individual_call($self->{CALLSIGN}.':'.$substr.'...',$to,$txgroup,$emergency);
155 } else {
156 $json = $self->_json_individual_call($self->{CALLSIGN}.':'.'...'.$substr,$to,$txgroup,$emergency);
157 };
158 my($req) = $self->_build_request($json);
159 my($res) = $ua->request($req);
160 warn($res->status_line);
161 if (!$res->is_success) {
162 return($res->status_line);
163 };
164 $i++;
165 };
166
167 };
168
169
170 return(0);
171  
172 }
173  
174 =head2 send_rubric_content
175  
176 Sends a message to a rubric
177  
178 $ret = $dapnetobj->send_rubric_content(<text to send>,<rubric name>.<transmitter group name>,<sequence number 1 - 10>);
179  
180 Returns 0 on sucess or the HTTP error string on error.
181  
182 =cut
183  
184  
185 sub send_rubric_content {
186 my $self = shift;
187 my($text,$rubric,$number) = @_;
188
189 my($ua) = LWP::UserAgent->new;
190 my($jsonobj);
191 my($i) = 1;
192 my($json);
193
194 if (length($text) <= (80 - $self->{_CALL_LEN})) {
195 my($json) = $self->_json_rubric_content($self->{CALLSIGN}.':'.$text,$rubric,$number);
196 my($req) = $self->_build_request($json,'news');
197 my($res) = $ua->request($req);
198 warn($res->status_line);
199 if (!$res->is_success) {
200 return($res->status_line);
201 };
202 } else {
203 while (my $substr = substr($text,0,(80 - $self->{_CALL_LEN}),'')) {
204 if ($i == 1) {
205 $json = $self->_json_rubrik_content($self->{CALLSIGN}.':'.$substr.'...',$rubric,$number);
206 } else {
207 $json = $self->_json_rubrik_content($self->{CALLSIGN}.':'.'...'.$substr,$rubric,$number);
208 };
209 my($req) = $self->_build_request($json);
210 my($res) = $ua->request($req);
211 warn($res->status_line);
212 if (!$res->is_success) {
213 return($res->status_line);
214 };
215 $i++;
216 };
217
218 };
219
220
221 return(0);
222 };
223  
224 1;