Module: FlightRadar

Defined in:
lib/flight_radar.rb

Overview

FlightRadar module for sending requests to FlightRadar24 API

Constant Summary collapse

VERSION =
'0.2.1'

Class Method Summary collapse

Class Method Details

.airline_logo(iata, icao) ⇒ Array

Retrieves airline logos based on IATA and ICAO codes.

Parameters:

  • iata (String)

    The IATA code of the airline.

  • icao (String)

    The ICAO code of the airline.

Returns:

  • (Array)

    An array containing URLs of airline logos.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flight_radar.rb', line 43

def (iata, icao)
  first_logo_url = "#{Core::AIRLINE_LOGO_URL}#{iata}_#{icao}.png"
  second_logo_url = "#{Core::ALTERNATIVE_AIRLINE_LOGO_URL}#{icao}_logo0.png"

  # Check the availability of logos and return URLs.
  result = []
  first_request = Request.new(first_logo_url, Core::IMAGE_HEADERS)
  second_request = Request.new(second_logo_url, Core::IMAGE_HEADERS)
  result << first_logo_url if first_request.success?
  result << second_logo_url if second_request.success?
  result
end

.airlinesArray

Retrieves a list of airlines.

Returns:

  • (Array)

    An array containing information about airlines.



33
34
35
36
# File 'lib/flight_radar.rb', line 33

def airlines
  request = Request.new(Core::AIRLINES_DATA_URL, Core::JSON_HEADERS)
  request.content['rows']
end

.airport(code) ⇒ Hash

Retrieves traffic statistics for a specific airport.

Parameters:

  • code (String)

    The code of the airport.

Returns:

  • (Hash)

    A hash containing traffic statistics for the specified airport.



60
61
62
# File 'lib/flight_radar.rb', line 60

def airport(code)
  HTTParty.get("https://data-live.flightradar24.com/airports/traffic-stats/?airport=#{code}").parsed_response
end

.airportsArray

Retrieves a list of airports.

Returns:

  • (Array)

    An array containing information about airports.



67
68
69
70
# File 'lib/flight_radar.rb', line 67

def airports
  request = Request.new(Core::AIRPORTS_DATA_URL, Core::JSON_HEADERS)
  request.content['rows']
end

.bounds(zone) ⇒ String

Converts zone information into bounds string.

Parameters:

  • zone (Hash)

    A hash containing zone information.

Returns:

  • (String)

    A string containing bounds information.



76
77
78
# File 'lib/flight_radar.rb', line 76

def bounds(zone)
  "#{zone['tl_y']},#{zone['br_y']},#{zone['tl_x']},#{zone['br_x']}"
end

.country_flag(country) ⇒ String

Retrieves the URL of a country flag based on the country name.

Parameters:

  • country (String)

    The name of the country.

Returns:

  • (String)

    The URL of the country flag.



84
85
86
# File 'lib/flight_radar.rb', line 84

def country_flag(country)
  "#{Core::COUNTRY_FLAG_URL}#{country.downcase.gsub(' ', '-')}.gif"
end

.flight_details(flight_id) ⇒ Hash

Retrieves detailed information about a specific flight.

Parameters:

  • flight_id (String)

    The ID of the flight.

Returns:

  • (Hash)

    A hash containing detailed information about the specified flight.



92
93
94
# File 'lib/flight_radar.rb', line 92

def flight_details(flight_id)
  HTTParty.get("https://data-live.flightradar24.com/clickhandler/?flight=#{flight_id}").parsed_response
end

.flights(params = {}) ⇒ Array

Retrieves a list of flights based on specified parameters.

Parameters:

  • params (Hash) (defaults to: {})

    (optional) Parameters for filtering flights.

Returns:

  • (Array)

    An array containing Flight objects.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/flight_radar.rb', line 100

def flights(params = {})
  request_params = @config.dup
  request_params[:airline] = params[:airline] if params[:airline]
  request_params[:bounds] = params[:bounds]&.gsub(',', '%2C')

  response = Request.new(Core::REAL_TIME_FLIGHT_TRACKER_DATA_URL, Core::JSON_HEADERS, request_params).content

  %w[full_count version stats].each { |k| response.delete(k) }

  response.map { |flight_id, flight_details| Flight.new(flight_id, flight_details) }
end

.zonesHash

Retrieves information about flight tracking zones.

Returns:

  • (Hash)

    A hash containing information about flight tracking zones.



115
116
117
118
119
120
# File 'lib/flight_radar.rb', line 115

def zones
  request = Request.new(Core::ZONES_DATA_URL, Core::JSON_HEADERS)
  request = request.content
  request.delete('version')
  request
end