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.)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/flight_radar/flight.rb', line 25

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 = @number != DEFAULT_TEXT && @number.length >= 2 ? @number[0..1] : DEFAULT_TEXT
  @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

#aircraft_codeObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def aircraft_code
  @aircraft_code
end

#airline_iataObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def airline_iata
  @airline_iata
end

#airline_icaoObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def airline_icao
  @airline_icao
end

#callsignObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def callsign
  @callsign
end

#destination_airport_iataObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def destination_airport_iata
  @destination_airport_iata
end

#icao_24bitObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def icao_24bit
  @icao_24bit
end

#idObject

Accessor for the flight ID.



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

def id
  @id
end

#latitudeObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def latitude
  @latitude
end

#longitudeObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def longitude
  @longitude
end

#numberObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def number
  @number
end

#on_groundObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def on_ground
  @on_ground
end

#origin_airport_iataObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def origin_airport_iata
  @origin_airport_iata
end

#registrationObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def registration
  @registration
end

#squawkObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def squawk
  @squawk
end

#timeObject (readonly)

Read-only accessors for flight attributes.



13
14
15
# File 'lib/flight_radar/flight.rb', line 13

def time
  @time
end

Instance Method Details

#altitudeString

Returns a formatted string representing the altitude of the flight.

Returns:

  • (String)

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



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

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").



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

def flight_level
  return altitude if @altitude == DEFAULT_TEXT || @altitude.to_i <= 10_000

  "FL#{(@altitude.to_i / 100).to_s.rjust(3, '0')}"
end

#ground_speedString

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

Returns:

  • (String)

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



77
78
79
80
81
82
83
84
# File 'lib/flight_radar/flight.rb', line 77

def ground_speed
  return "#{@ground_speed} kt" if @ground_speed == DEFAULT_TEXT

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

#headingString

Returns a formatted string representing the heading of the flight.

Returns:

  • (String)

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



89
90
91
# File 'lib/flight_radar/flight.rb', line 89

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.



51
52
53
54
55
56
# File 'lib/flight_radar/flight.rb', line 51

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

#vertical_speedString

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

Returns:

  • (String)

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



96
97
98
# File 'lib/flight_radar/flight.rb', line 96

def vertical_speed
  "#{@vertical_speed} fpm"
end