class VatsimTools::DataDownloader

Constants

LOCAL_DATA
LOCAL_DATA_BAK
LOCAL_STATUS
LOCAL_STATUS_BAK
STATUS_URL

Public Class Methods

new() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 13
def initialize
  data_file
end

Public Instance Methods

create_data_backup() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 86
def create_data_backup
  source = LOCAL_DATA
  target = LOCAL_DATA_BAK
  FileUtils.cp_r source, target
  File.chmod(0777, LOCAL_DATA_BAK)
end
create_local_data_file() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 62
def create_local_data_file
  curl = Curl::Easy.new(servers.sample)
  curl.timeout = 20
  curl.perform
  curl = curl.body_str
  create_data_backup if File.exists?(LOCAL_DATA)
  data = Tempfile.new('vatsim_data', :encoding => 'utf-8')
  File.rename data.path, LOCAL_DATA
  data = curl.gsub(/["]/, '\s').encode!('UTF-16', 'UTF-8', :invalid => :replace, :replace => '').encode!('UTF-8', 'UTF-16')
  data = data.slice(0..(data.index('!PREFILE:')))
  File.open(LOCAL_DATA, "w+") {|f| f.write(data)}
  File.chmod(0777, LOCAL_DATA)
  gem_data_file if curl.include? "<html><head>"
  gem_data_file if File.open(LOCAL_DATA).size == 0
rescue Curl::Err::HostResolutionError
  gem_data_file
rescue Curl::Err::TimeoutError
  gem_data_file
rescue
  gem_data_file
rescue Exception
  gem_data_file
end
create_status_backup() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 38
def create_status_backup
  source = LOCAL_STATUS
  target = LOCAL_STATUS_BAK
  FileUtils.cp_r source, target
  File.chmod(0777, LOCAL_STATUS_BAK)
end
create_status_tempfile() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 17
def create_status_tempfile
  curl = Curl::Easy.new(STATUS_URL)
  curl.timeout = 20
  curl.perform
  curl = curl.body_str
  create_status_backup if File.exists?(LOCAL_STATUS)
  status = Tempfile.new('vatsim_status')
  File.rename status.path, LOCAL_STATUS
  File.open(LOCAL_STATUS, "w+") {|f| f.write(curl) }
  File.chmod(0777, LOCAL_STATUS)
  dummy_status if curl.include? "<html><head>"
rescue Curl::Err::HostResolutionError
  dummy_status
rescue Curl::Err::TimeoutError
  dummy_status
rescue
  dummy_status
rescue Exception
  dummy_status
end
data_file() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 99
def data_file
  File.exists?(LOCAL_DATA) ? read_local_datafile : create_local_data_file
  LOCAL_DATA
end
dummy_status() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 111
def dummy_status
  source = LOCAL_STATUS_BAK
  target = LOCAL_STATUS
  FileUtils.cp_r source, target
  File.chmod(0777, LOCAL_STATUS)
end
gem_data_file() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 104
def gem_data_file
  source = LOCAL_DATA_BAK
  target = LOCAL_DATA
  FileUtils.cp_r source, target
  File.chmod(0777, LOCAL_DATA)
end
read_local_datafile() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 93
def read_local_datafile
  data = File.open(LOCAL_DATA)
  difference = Time.diff(data.ctime, Time.now)[:minute]
  difference > 2 ? create_local_data_file : data.read
end
read_status_tempfile() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 45
def read_status_tempfile
  status = File.open(LOCAL_STATUS)
  difference = Time.diff(status.ctime, Time.now)[:hour]
  difference > 3 ? create_status_tempfile : status.read
end
servers() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 56
def servers
  urls = []
  CSV.foreach(status_file, :col_sep =>'=') {|row| urls << row[1] if row[0] == "url0"}
  urls
end
status_file() click to toggle source
# File lib/vatsim_online/data_downloader.rb, line 51
def status_file
  File.exists?(LOCAL_STATUS) ? read_status_tempfile : create_status_tempfile
  LOCAL_STATUS
end