Skip to content

Commit

Permalink
Fix all rubocop warnings and add extensive cooments to the code
Browse files Browse the repository at this point in the history
  • Loading branch information
kupolak committed Dec 20, 2023
1 parent fb2e816 commit 2e5c14a
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 229 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
bundle install
- name: Run RSpec tests
run: bundle exec rake spec
run: bundle exec rake
18 changes: 3 additions & 15 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
AllCops:
TargetRubyVersion: 2.7

Style/StringLiterals:
Enabled: true
EnforcedStyle: double_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: double_quotes

Layout/LineLength:
Max: 120

Layout/EndOfLine:
Enabled: False
TargetRubyVersion: 3.2
NewCops: enable
SuggestExtensions: false
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

source "https://rubygems.org"
source 'https://rubygems.org'

# Specify your gem's dependencies in flight_radar.gemspec
gemspec

gem 'rake'
gem 'rspec'
gem 'rubocop'
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

require "rubocop/rake_task"
require 'rubocop/rake_task'

RuboCop::RakeTask.new

Expand Down
34 changes: 16 additions & 18 deletions flight_radar.gemspec
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
# frozen_string_literal: true

Gem::Specification.new do |spec|
spec.name = "flight_radar"
spec.version = "0.2.0"
spec.authors = ["Jakub Polak"]
spec.email = ["[email protected]"]
spec.name = 'flight_radar'
spec.version = '0.2.0'
spec.authors = ['Jakub Polak']
spec.email = ['[email protected]']

spec.summary = "Ruby gem for fetching aircraft data from Flightradar24"
spec.description = "To use this API see more information at: https://www.flightradar24.com/terms-and-conditions"
spec.homepage = "https://github.com/kupolak/flight_radar"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.7.0"
spec.summary = 'Ruby gem for fetching aircraft data from Flightradar24'
spec.description = 'To use this API see more information at: https://www.flightradar24.com/terms-and-conditions'
spec.homepage = 'https://github.com/kupolak/flight_radar'
spec.license = 'MIT'
spec.required_ruby_version = '>= 3.2.0'

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/kupolak/flight_radar"
spec.metadata["changelog_uri"] = "https://github.com/kupolak/flight_radar/blob/main/CHANGELOG.md"
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/kupolak/flight_radar'
spec.metadata['changelog_uri'] = 'https://github.com/kupolak/flight_radar/blob/main/CHANGELOG.md'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
end
spec.bindir = "exe"
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_dependency "httparty", "~> 0.21.0"
spec.add_dependency 'httparty', '~> 0.21.0'

spec.add_development_dependency "rake", "~>13.1.0"
spec.add_development_dependency "rspec", "~>3.12.0"
spec.add_development_dependency "rubocop", "~>1.59.0"
spec.metadata['rubygems_mfa_required'] = 'true'
end
59 changes: 28 additions & 31 deletions lib/flight_radar.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# frozen_string_literal: true

require_relative "flight_radar/core"
require_relative "flight_radar/request"
require_relative "flight_radar/flight"
require_relative 'flight_radar/core'
require_relative 'flight_radar/request'
require_relative 'flight_radar/flight'

# FlightRadar module for sending requests to FlightRadar24 API
module FlightRadar
VERSION = "0.2.0"
VERSION = '0.2.0'

module_function

@config = {
"faa": "1",
"satellite": "1",
"mlat": "1",
"flarm": "1",
"adsb": "1",
"gnd": "1",
"air": "1",
"vehicles": "1",
"estimated": "1",
"maxage": "14400",
"gliders": "1",
"stats": "1",
"limit": "5000"
faa: '1',
satellite: '1',
mlat: '1',
flarm: '1',
adsb: '1',
gnd: '1',
air: '1',
vehicles: '1',
estimated: '1',
maxage: '14400',
gliders: '1',
stats: '1',
limit: '5000'
}

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

def airline_logo(iata, icao)
Expand All @@ -50,40 +50,37 @@ def airport(code)

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

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

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

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

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

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

request = Request.new(Core::REAL_TIME_FLIGHT_TRACKER_DATA_URL, Core::JSON_HEADERS, request_params)
response = request.content
%w[full_count version stats].each { |k| response.delete(k) }
flights = []
response.each do |flight_id, flight_details|
flights.append(Flight.new(flight_id, flight_details))
end
flights

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

def zones
request = Request.new(Core::ZONES_DATA_URL, Core::JSON_HEADERS)
request = request.content
request.delete("version")
request.delete('version')
request
end
end
66 changes: 36 additions & 30 deletions lib/flight_radar/core.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,65 @@
# frozen_string_literal: true

# The `Core` module contains constants related to FlightRadar API endpoints,
# URLs for various data sources, and headers used in HTTP requests.
module Core
API_FLIGHT_RADAR_BASE_URL = "https://api.flightradar24.com/common/v1"
CDN_FLIGHT_RADAR_BASE_URL = "https://cdn.flightradar24.com"
FLIGHT_RADAR_BASE_URL = "https://www.flightradar24.com"
DATA_LIVE_BASE_URL = "https://data-live.flightradar24.com"
DATA_CLOUD_BASE_URL = "https://data-cloud.flightradar24.com"
API_FLIGHT_RADAR_BASE_URL = 'https://api.flightradar24.com/common/v1'
CDN_FLIGHT_RADAR_BASE_URL = 'https://cdn.flightradar24.com'
FLIGHT_RADAR_BASE_URL = 'https://www.flightradar24.com'
DATA_LIVE_BASE_URL = 'https://data-live.flightradar24.com'
DATA_CLOUD_BASE_URL = 'https://data-cloud.flightradar24.com'

# User login URL.
USER_LOGIN_URL = "#{FLIGHT_RADAR_BASE_URL}/user/login"
USER_LOGOUT_URL = "#{FLIGHT_RADAR_BASE_URL}/user/logout"
USER_LOGIN_URL = "#{FLIGHT_RADAR_BASE_URL}/user/login".freeze
USER_LOGOUT_URL = "#{FLIGHT_RADAR_BASE_URL}/user/logout".freeze

# Most tracked data URL
MOST_TRACKED_URL = "#{FLIGHT_RADAR_BASE_URL}/flights/most-tracked"
MOST_TRACKED_URL = "#{FLIGHT_RADAR_BASE_URL}/flights/most-tracked".freeze

# Search data URL
SEARCH_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/v1/search/web/find?query={}&limit=50"
SEARCH_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/v1/search/web/find?query={}&limit=50".freeze

# Flights data URLs.
REAL_TIME_FLIGHT_TRACKER_DATA_URL = "#{DATA_CLOUD_BASE_URL}/zones/fcgi/feed.js"
FLIGHT_DATA_URL = "#{DATA_LIVE_BASE_URL}/clickhandler/?flight={}"
REAL_TIME_FLIGHT_TRACKER_DATA_URL = "#{DATA_CLOUD_BASE_URL}/zones/fcgi/feed.js".freeze
FLIGHT_DATA_URL = "#{DATA_LIVE_BASE_URL}/clickhandler/?flight={}".freeze

# Airports data URLs.
API_AIRPORT_DATA_URL = "#{API_FLIGHT_RADAR_BASE_URL}/airport.json"
AIRPORT_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/airports/traffic-stats/?airport="
AIRPORTS_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/_json/airports.php"
API_AIRPORT_DATA_URL = "#{API_FLIGHT_RADAR_BASE_URL}/airport.json".freeze
AIRPORT_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/airports/traffic-stats/?airport=".freeze
AIRPORTS_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/_json/airports.php".freeze

# Airlines data URL.
AIRLINES_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/_json/airlines.php"
AIRLINES_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/_json/airlines.php".freeze

# Zones data URL.
ZONES_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/js/zones.js.php"
ZONES_DATA_URL = "#{FLIGHT_RADAR_BASE_URL}/js/zones.js.php".freeze

# Country flag image URL.
COUNTRY_FLAG_URL = "#{FLIGHT_RADAR_BASE_URL}/static/images/data/flags-small/"
COUNTRY_FLAG_URL = "#{FLIGHT_RADAR_BASE_URL}/static/images/data/flags-small/".freeze

# Airline logo image URL.
AIRLINE_LOGO_URL = "#{CDN_FLIGHT_RADAR_BASE_URL}/assets/airlines/logotypes/"
ALTERNATIVE_AIRLINE_LOGO_URL = "#{FLIGHT_RADAR_BASE_URL}/static/images/data/operators/"
AIRLINE_LOGO_URL = "#{CDN_FLIGHT_RADAR_BASE_URL}/assets/airlines/logotypes/".freeze
ALTERNATIVE_AIRLINE_LOGO_URL = "#{FLIGHT_RADAR_BASE_URL}/static/images/data/operators/".freeze

# rubocop:disable Style/MutableConstant
HEADERS = {
"accept-encoding": "gzip, br",
"accept-language": "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "max-age=0",
"origin": "#{FLIGHT_RADAR_BASE_URL}",
"referer": "#{FLIGHT_RADAR_BASE_URL}/",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
'accept-encoding': 'gzip, br',
'accept-language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
'cache-control': 'max-age=0',
origin: FLIGHT_RADAR_BASE_URL.to_s,
referer: "#{FLIGHT_RADAR_BASE_URL}/",
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/87.0.4280.88 Safari/537.36'
}
# rubocop:enable Style/MutableConstant

JSON_HEADERS = HEADERS
JSON_HEADERS["accept"] = "application/json"
JSON_HEADERS['accept'] = 'application/json'

IMAGE_HEADERS = HEADERS
IMAGE_HEADERS["accept"] = "image/gif, image/jpg, image/jpeg, image/png"
IMAGE_HEADERS['accept'] = 'image/gif, image/jpg, image/jpeg, image/png'
end
Loading

0 comments on commit 2e5c14a

Please sign in to comment.