blib/lib/FR24/Bot.pm

filter: Statementstmt 94.7% 91 / 96 Branchbran 63.6% 14 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 66.6% 2 / 3 totaltotal 89.6% 121 / 135 CC 13 SCAR 14.5
CC
13
Cov
89%
new
9.3
update
3.0
_curl
2.0
CRAP 4.3
« bin/config-fr24-bot ↑ Summary blib/lib/FR24/Tutorial.pm »
1 #ABSTRACT: Subroutines for FR24-Bot
2 package FR24::Bot;
3 3 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use FR24::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 our $VERSION = "0.0.2";
13 3 my $UPDATE_MS = 10 * 1000;
14 # Export version
15 3 our @EXPORT = qw($VERSION);
16 3 our @EXPORT_OK = qw(new get);
17
18  
19 sub new {
20 2 ▶ my $class = shift @_;
Subroutine new: called CC=9 SCAR=22.3
Pod documented
21  
22 2 my $name = undef;
23 2 my $config = undef;
24 2 my $refresh = $UPDATE_MS;
25 2 my $test_mode = 0;
26 # Descriptive instantiation with parameters -param => value
27 2 ▶ if (substr($_[0], 0, 1) eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq "-") 2 0 2
28 2 my %data = @_;
29 # Try parsing
30 2 for my $i (keys %data) {
31 5 ▶ if ($i =~ /^-conf/i) {
Branch: 4 branches
BranchTrueFalseTotal
if ($i =~ /^-conf/iu) { } 2 3 5
elsif ($i =~ /^-(name|id)$/iu) { } 2 1 3
elsif ($i =~ /^-refresh$/iu) { } 0 1 1
elsif ($i =~ /^-test$/iu) { } 1 0 1
32 #Can be -conf, -config and other crazy stuff, hope not -confused
33 2 $config = $data{$i};
34 2 ▶ if (ref($config) ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config ne "HASH") 2 0 2
35 2 $config = FR24::Utils::loadconfig($config);
36 }
37 } elsif ($i =~ /^-(name|id)$/i) {
38 2 $name = $data{$i};
39 } elsif ($i =~ /^-refresh$/i) {
40 # Receive seconds, convert to milliseconds
41 0 $refresh = 1000 * $data{$i};
42 } elsif ($i =~ /^-test$/i) {
43 # Receive seconds, convert to milliseconds
44 1 $test_mode = 1;
45 } else {
46 0 confess "ERROR FR24::Bot: Unknown parameter $i\n";
47 }
48 }
49 }
50
51
52
53
54 2 my $self = bless {}, $class;
55
56 2 ▶ if (not defined $config->{telegram}->{apikey}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"telegram"}{"apikey"}) 0 2 2
57 0 confess "ERROR FR24::Bot: No config provided or no apikey found\n";
58 }
59 2 $self->{apikey} = $config->{telegram}->{apikey};
60 2 $self->{name} = $name;
61 2 $self->{config} = $config;
62 2 $self->{ip} = $config->{server}->{ip};
63 2 $self->{port} = $config->{server}->{port};
64 2 $self->{localip} = undef;
65 2 $self->{refresh} = $refresh;
66 2 $self->{total} = 0;
67 2 $self->{uploaded} = 0;
68 2 $self->{flights} = {};
69 2 $self->{callsigns} = {};
70 2 $self->{flights_url} = "http://" . $self->{ip} . ":" . $self->{port} . "/flights.json";
71  
72 2 $self->{users} = {};
73 2 $self->{last_updated} = 0;
74 2 $self->{last_url} = undef;
75 2 $self->{test_mode} = $test_mode;
76 2 $self->update();
77 2 return $self;
78
79 }
80  
81 sub _timestamp_milliseconds {
82 4 ▶ return int(time * 1000);
Subroutine _timestamp_milliseconds: called CC=1 SCAR=0.0
83 }
84 sub update {
85 2 ▶ my $self = shift;
Subroutine update: called CC=3 SCAR=11.1
Pod documented
86
87 2 my $timestamp = _timestamp_milliseconds();
88 2 ▶ if ($timestamp - $self->{last_updated} < $self->{refresh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timestamp - $self->{"last_updated"} < $self->{"refresh"}) 0 2 2
89 # Update only once per second
90 0 return;
91 }
92 2 $self->{last_updated} = $timestamp;
93 2 my $url = $self->{flights_url} . "?time=" . _timestamp_milliseconds();
94 2 $self->{last_url} = $url;
95 2 ▶ confess "No URL specified for update\n" unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 0 2 2
96  
97 2 my $content = _curl($url);
98 2 $self->{content} = $content;
99 2 my $data = FR24::Utils::parse_flights($content, $self->{test_mode});
100 # Parse the content here
101 # Example: extracting flight information
102 #my @flights = extract_flights($content);
103  
104 # Update the object properties
105 2 $self->{flights} = $data->{data};
106 2 $self->{callsigns} = $data->{callsigns};
107 2 $self->{total} = $data->{total};
108 2 $self->{uploaded} = $data->{uploaded};
109  
110 2 return;
111 }
112  
113  
114  
115 # Write a $self->update() method to update the object
116 sub getflight {
117 2 ▶ my ($self, $callsign) = @_;
Subroutine getflight: called CC=2 SCAR=6.9
Pod undocumented
118 2 ▶ if (not defined $self->{'callsigns'}->{$callsign}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"callsigns"}{$callsign}) 1 1 2
119 1 return 0;
120 }
121
122 1 return $self->{'flights'}->{ $self->{'callsigns'}->{$callsign} };
123 }
124  
125 sub _curl {
126 2 ▶ my $url = shift;
Subroutine _curl: called CC=2 SCAR=7.1
127 2 my $response = "";
128 2 eval {
129 2 my $http = HTTP::Tiny->new();
130 2 my $response = $http->get($url);
131  
132 2 ▶ if ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{'success'}) { } 0 2 2
133 0 return $response->{content};
134 } else {
135 2 return "Failed to retrieve URL: $response->{status} $response->{reason}\n";
136 }
137 };
138 }
139  
140 __END__
« bin/config-fr24-bot ↑ Summary blib/lib/FR24/Tutorial.pm »