Class: Flight

Inherits:
Object
  • Object
show all
Defined in:
lib/flight_radar/flight.rb

Overview

The Flight class represents information about a flight obtained from a data source. It provides methods to access and format various attributes related to the flight, such as altitude, ground speed, heading, and more.

Constant Summary collapse

DEFAULT_TEXT =
'N/A'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flight_id, info) ⇒ Flight

Initializes a new instance of the Flight class with the given flight ID and information.

rubocop:disable Metrics

Parameters:

  • flight_id (String)

    The unique identifier for the flight.

  • info (Array<String>)

    An array containing various information about the flight. The order of elements in the array is assumed to follow a specific pattern. (e.g., info[0] is ICAO 24-bit address, info[1] is latitude, info[2] is longitude, and so on.)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/flight_radar/flight.rb', line 19

def initialize(flight_id, info)
  @id = flight_id
  @icao_24bit = get_info(info[0])
  @latitude = get_info(info[1])
  @longitude = get_info(info[2])
  @heading = get_info(info[3])
  @altitude = get_info(info[4])
  @ground_speed = get_info(info[5])
  @squawk = get_info(info[6])
  @aircraft_code = get_info(info[8])
  @registration = get_info(info[9])
  @time = get_info(info[10])
  @origin_airport_iata = get_info(info[11])
  @destination_airport_iata = get_info(info[12])
  @number = get_info(info[13])
  @airline_iata = get_info(info[13][0..1])
  @on_ground = get_info(info[14])
  @vertical_speed = get_info(info[15])
  @callsign = get_info(info[16])
  @airline_icao = get_info(info[18])
end

Instance Attribute Details

#idObject

Accessor for the flight ID.



10
11
12
# File 'lib/flight_radar/flight.rb', line 10

def id
  @id
end

Instance Method Details

#altitudeString

Returns a formatted string representing the altitude of the flight.

Returns:

  • (String)

    Formatted altitude string (e.g., "5000 ft").



55
56
57
# File 'lib/flight_radar/flight.rb', line 55

def altitude
  "#{@altitude} ft"
end

#flight_levelString

Returns a formatted string representing the flight level based on altitude.

Returns:

  • (String)

    Formatted flight level string (e.g., "FL350").



62
63
64
# File 'lib/flight_radar/flight.rb', line 62

def flight_level
  @altitude > 10_000 ? "#{@altitude[0..2]} FL" : altitude
end

#ground_speedString

Returns a formatted string representing the ground speed of the flight.

Returns:

  • (String)

    Formatted ground speed string (e.g., "300 kts").



69
70
71
72
73
# File 'lib/flight_radar/flight.rb', line 69

def ground_speed
  speed = "#{@ground_speed} kt"
  speed += 's' if @ground_speed > 1
  speed
end

#headingString

Returns a formatted string representing the heading of the flight.

Returns:

  • (String)

    Formatted heading string (e.g., "120°").



78
79
80
# File 'lib/flight_radar/flight.rb', line 78

def heading
  "#{@heading}°"
end

#to_sString

Returns a string representation of the Flight object.

Returns:

  • (String)

    A formatted string containing relevant information about the flight.



45
46
47
48
49
50
# File 'lib/flight_radar/flight.rb', line 45

def to_s
  "<(#{@aircraft_code}) #{@registration}
  - Altitude: #{@altitude}
  - Ground Speed: #{@ground_speed}
  - Heading: #{@ground_speed}>"
end

#vertical_speedString

Returns a formatted string representing the vertical speed of the flight.

Returns:

  • (String)

    Formatted vertical speed string (e.g., "1000 fpm").



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

def vertical_speed
  "#{@vertical_speed} fpm"
end