| 1 | package WebService::ADSBExchange; | ||
| 2 | |||
| 3 | # ABSTRACT: Interface with the ADSBExchange API | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | WebService::ADSBExchange | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use WebService::ADSBExchange; | ||
| 12 | |||
| 13 | my $adsb = WebService::ADSBExchange->new( key => $key ); | ||
| 14 | |||
| 15 | my $registration = 'N161UW'; | ||
| 16 | my $position = $adsb->single_aircraft_position_by_registration($registration); | ||
| 17 | my $latitude = $position->{ac}[0]->{lat}; | ||
| 18 | my $longitude = $position->{ac}[0]->{lon}; | ||
| 19 | my $flight = $position->{ac}[0]->{flight}; | ||
| 20 | say "$registration is flight $flight and its current position is $latitude by $longitude"; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This interface helps the user to communicate with the API at adsbexchange.com to track aircraft | ||
| 25 | information. The API could be used, for example, to alert you when an aircraft is within four | ||
| 26 | miles and flying under 5000 feet, or when an aircraft squawks 7700. To use the API you need to | ||
| 27 | register at https://rapidapi.com/adsbx/api/adsbexchange-com1 and buy a subscription. | ||
| 28 | |||
| 29 | To use the module, you first create a WebService::ADSBExchange object. The new() function takes | ||
| 30 | one parameter: your API Key. | ||
| 31 | |||
| 32 | my $adsb = WebService::ADSBExchange->new( key => $key ); | ||
| 33 | |||
| 34 | ... and then you are ready to use the methods on your $adsb object. If you send no key you will | ||
| 35 | get an error message from the module. If you send an invalid key, the module will use it and | ||
| 36 | you'll get an error from the API. | ||
| 37 | |||
| 38 | The API URL is fixed in the module, but if it changes you can also pass the new one to the new() | ||
| 39 | function: | ||
| 40 | |||
| 41 | my $adsb = WebService::ADSBExchange->new( key => $key, api_url => 'new_api_url.com' ); | ||
| 42 | |||
| 43 | =head1 METHODS | ||
| 44 | |||
| 45 | Each method returns the full API response in a hash. The API responds with JSON, and this interface | ||
| 46 | parses it into a hash for you to read. A complete example of accessing the information is in the | ||
| 47 | synopsis. If you want to mess with the "raw" JSON response, just call get_json_response(). This | ||
| 48 | could be useful if you want to inspect the result to determine which pieces of data you want. You | ||
| 49 | could also use Data::Dumper on the hash to see everything formatted nice and pretty, but that's | ||
| 50 | no fun. | ||
| 51 | |||
| 52 | =head2 single_aircraft_position_by_registration | ||
| 53 | |||
| 54 | $adsb->single_aircraft_position_by_registration('N161UW'); | ||
| 55 | |||
| 56 | =head2 single_aircraft_position_by_hex_id | ||
| 57 | |||
| 58 | $adsb->single_aircraft_position_by_hex_id('A0F73C'); | ||
| 59 | |||
| 60 | =head2 aircraft_live_position_by_hex_id | ||
| 61 | |||
| 62 | $adsb->aircraft_live_position_by_hex_id('A0F73C'); | ||
| 63 | |||
| 64 | This call appears to be the same as single_aircraft_position_by_hex_id | ||
| 65 | |||
| 66 | =head2 aircraft_last_position_by_hex_id | ||
| 67 | |||
| 68 | $adsb->aircraft_last_position_by_hex_id("A0F73C"); | ||
| 69 | |||
| 70 | =head2 aircraft_by_callsign | ||
| 71 | |||
| 72 | $adsb->aircraft_by_callsign('AAL2630'); | ||
| 73 | |||
| 74 | =head2 aircraft_by_squawk | ||
| 75 | |||
| 76 | $adsb->aircraft_by_squawk('2025'); | ||
| 77 | |||
| 78 | =head2 tagged_military_aircraft | ||
| 79 | |||
| 80 | $adsb->tagged_military_aircraft(); | ||
| 81 | |||
| 82 | Note this method takes no parameters. | ||
| 83 | |||
| 84 | =head2 aircraft_within_n_mile_radius | ||
| 85 | |||
| 86 | $adsb->aircraft_within_n_mile_radius( '45.09634', '-94.41019', '30' ); | ||
| 87 | |||
| 88 | Latitude, Longitude, and miles | ||
| 89 | |||
| 90 | =head2 do_call | ||
| 91 | |||
| 92 | do_call is not meant to be accessed directly, but if additional API calls are implemented before this | ||
| 93 | interface is updated to use them, you could still use this interface and make the API call like this: | ||
| 94 | |||
| 95 | $adsb->do_call('new_api_call_example/new_api_call_parameters'); | ||
| 96 | |||
| 97 | For example, if there was a new API call that returned aircraft by country of registration, it might | ||
| 98 | look something like this: | ||
| 99 | |||
| 100 | $adsb->do_call('country/Austria'); | ||
| 101 | |||
| 102 | =head2 get_json_response | ||
| 103 | |||
| 104 | This is the only method that doesn't make an API call; it returns the full JSON response | ||
| 105 | from the previous call. This is useful if you wish to see what information is available from the | ||
| 106 | call or wish to parse it in a different way. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | use LWP::UserAgent; | ||
| 111 | use HTTP::Request; | ||
| 112 | use HTTP::Headers; | ||
| 113 | use strict; | ||
| 114 | use warnings; | ||
| 115 | use Moose; | ||
| 116 | use Types::Standard qw(Str); | ||
| 117 | use Carp qw(croak); | ||
| 118 | use JSON::Parse qw(parse_json); | ||
| 119 | |||
| 120 | sub BUILD { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | croak | ||
| 124 | 'You did not specify your API key. <WebService::ADSBExchange->new( key => $key );> To get a key, visit https://rapidapi.com/adsbx/api/adsbexchange-com1' | ||
| 125 | if ( !defined $self->key ); | ||
| 126 | |||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | has 'api_url' => ( | ||
| 131 | is => 'rw', | ||
| 132 | isa => Str, | ||
| 133 | default => 'adsbexchange-com1.p.rapidapi.com', | ||
| 134 | ); | ||
| 135 | |||
| 136 | has 'key' => ( | ||
| 137 | is => 'rw', | ||
| 138 | isa => Str | ||
| 139 | ); | ||
| 140 | |||
| 141 | has 'json_response' => ( | ||
| 142 | is => 'rw', | ||
| 143 | isa => Str | ||
| 144 | ); | ||
| 145 | |||
| 146 | sub do_call { | ||
| 147 | my ( $self, $callrequest ) = @_; | ||
| 148 | |||
| 149 | my $full_api_url = 'https://' . $self->api_url; | ||
| 150 | my $obj_user_agent = LWP::UserAgent->new( timeout => 15 ); | ||
| 151 | my $header = HTTP::Headers->new; | ||
| 152 | |||
| 153 | $header->push_header( 'X-RapidAPI-Key' => $self->key ); | ||
| 154 | $header->push_header( 'X-RapidAPI-Host' => $self->api_url ); | ||
| 155 | my $obj_request = | ||
| 156 | HTTP::Request->new( 'GET', $full_api_url . "/v2/$callrequest/", $header ); | ||
| 157 | my $obj_response = $obj_user_agent->request($obj_request); | ||
| 158 | |||
| 159 | $self->json_response( $obj_response->content ); | ||
| 160 | return parse_json( $obj_response->content ); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub get_json_response { | ||
| 164 | my $self = shift; | ||
| 165 | return $self->json_response; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub single_aircraft_position_by_registration { | ||
| 169 | my $self = shift; | ||
| 170 | my $registration = shift; | ||
| 171 | return $self->do_call("registration/$registration"); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub single_aircraft_position_by_hex_id { | ||
| 175 | my $self = shift; | ||
| 176 | my $hex_id = shift; | ||
| 177 | return $self->do_call("icao/$hex_id"); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub aircraft_live_position_by_hex_id { | ||
| 181 | my $self = shift; | ||
| 182 | my $hex_id = shift; | ||
| 183 | return $self->do_call("icao/$hex_id"); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub aircraft_last_position_by_hex_id { | ||
| 187 | my $self = shift; | ||
| 188 | my $hex_id = shift; | ||
| 189 | return $self->do_call("hex/$hex_id"); | ||
| 190 | } | ||
| 191 | |||
| 192 | sub aircraft_by_callsign { | ||
| 193 | my $self = shift; | ||
| 194 | my $callsign = shift; | ||
| 195 | return $self->do_call("callsign/$callsign"); | ||
| 196 | } | ||
| 197 | sub aircraft_by_squawk { | ||
| 198 | my $self = shift; | ||
| 199 | my $squawk = shift; | ||
| 200 | return $self->do_call("sqk/$squawk"); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub tagged_military_aircraft { | ||
| 204 | my $self = shift; | ||
| 205 | return $self->do_call('mil'); | ||
| 206 | } | ||
| 207 | |||
| 208 | sub aircraft_within_n_mile_radius { | ||
| 209 | my ( $self, $lat, $lon, $dist ) = @_; | ||
| 210 | return $self->do_call("lat/$lat/lon/$lon/dist/$dist"); | ||
| 211 | } | ||
| 212 | |||
| 213 | 1; |