Skip to content

Commit

Permalink
Add method. Add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kupolak committed Dec 20, 2023
1 parent 4e6ee5c commit e192e04
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
43 changes: 39 additions & 4 deletions lib/flight_radar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module FlightRadar

module_function

# Default configuration parameters for FlightRadar requests.
@config = {
faa: '1',
satellite: '1',
Expand All @@ -26,45 +27,76 @@ module FlightRadar
limit: '5000'
}

# Retrieves a list of airlines.
#
# @return [Array] An array containing information about airlines.
def airlines
request = Request.new(Core::AIRLINES_DATA_URL, Core::JSON_HEADERS)
request.content['rows']
end

# Retrieves airline logos based on IATA and ICAO codes.
#
# @param iata [String] The IATA code of the airline.
# @param icao [String] The ICAO code of the airline.
# @return [Array] An array containing URLs of airline logos.
def airline_logo(iata, icao)
first_logo_url = "#{Core::AIRLINE_LOGO_URL}#{iata}_#{icao}.png"
second_logo_url = "#{Core::ALTERNATIVE_AIRLINE_LOGO_URL}#{icao}_logo0.png"

# check status codes
# 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.status_code[0] != 4
result << second_logo_url if second_request.status_code[0] != 4
[first_logo_url, second_logo_url]
result << first_logo_url if first_request.success?
result << second_logo_url if second_request.success?
result
end

# Retrieves traffic statistics for a specific airport.
#
# @param code [String] The code of the airport.
# @return [Hash] A hash containing traffic statistics for the specified airport.
def airport(code)
HTTParty.get("https://data-live.flightradar24.com/airports/traffic-stats/?airport=#{code}").parsed_response
end

# Retrieves a list of airports.
#
# @return [Array] An array containing information about airports.
def airports
request = Request.new(Core::AIRPORTS_DATA_URL, Core::JSON_HEADERS)
request.content['rows']
end

# Converts zone information into bounds string.
#
# @param zone [Hash] A hash containing zone information.
# @return [String] A string containing bounds information.
def bounds(zone)
"#{zone['tl_y']},#{zone['br_y']},#{zone['tl_x']},#{zone['br_x']}"
end

# Retrieves the URL of a country flag based on the country name.
#
# @param country [String] The name of the country.
# @return [String] The URL of the country flag.
def country_flag(country)
"#{Core::COUNTRY_FLAG_URL}#{country.downcase.gsub(' ', '-')}.gif"
end

# Retrieves detailed information about a specific flight.
#
# @param flight_id [String] The ID of the flight.
# @return [Hash] A hash containing detailed information about the specified flight.
def flight_details(flight_id)
HTTParty.get("https://data-live.flightradar24.com/clickhandler/?flight=#{flight_id}").parsed_response
end

# Retrieves a list of flights based on specified parameters.
#
# @param params [Hash] (optional) Parameters for filtering flights.
# @return [Array] An array containing Flight objects.
def flights(params = {})
request_params = @config.dup
request_params[:airline] = params[:airline] if params[:airline]
Expand All @@ -77,6 +109,9 @@ def flights(params = {})
response.map { |flight_id, flight_details| Flight.new(flight_id, flight_details) }
end

# Retrieves information about flight tracking zones.
#
# @return [Hash] A hash containing information about flight tracking zones.
def zones
request = Request.new(Core::ZONES_DATA_URL, Core::JSON_HEADERS)
request = request.content
Expand Down
7 changes: 7 additions & 0 deletions lib/flight_radar/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def status_code
@response.code
end

# Checks if the HTTP response code indicates success.
#
# @return [Boolean] Returns true if the response code is within the range 200 to 299 (inclusive), indicating success.
def success?
(200..299).include?(@response.code)
end

private

# Sends an HTTP GET request using the specified URL, headers, and parameters.
Expand Down

0 comments on commit e192e04

Please sign in to comment.