Class: VatsimTools::DataDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/vatsim_online/data_downloader.rb

Constant Summary collapse

STATUS_URL =
"https://status.vatsim.net/status.json"
TEMP_DIR =
"#{Dir.tmpdir}/vatsim_online"
LOCAL_STATUS =
"#{Dir.tmpdir}/vatsim_online/vatsim_status.json"
LOCAL_STATUS_BAK =
"#{Dir.tmpdir}/vatsim_online/vatsim_status_bak.json"
LOCAL_DATA =
"#{Dir.tmpdir}/vatsim_online/vatsim_data.json"
LOCAL_DATA_BAK =
"#{Dir.tmpdir}/vatsim_online/vatsim_data_bak.json"

Instance Method Summary collapse

Constructor Details

#initializeDataDownloader

Returns a new instance of DataDownloader.



16
17
18
19
# File 'lib/vatsim_online/data_downloader.rb', line 16

def initialize
  FileUtils.mkdir(TEMP_DIR) unless File.exist? TEMP_DIR
  data_file
end

Instance Method Details

#create_data_backupObject



88
89
90
91
92
93
# File 'lib/vatsim_online/data_downloader.rb', line 88

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_fileObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vatsim_online/data_downloader.rb', line 68

def create_local_data_file
  uri = URI(servers.sample)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.path)
  http.use_ssl = (uri.scheme == 'https')
  req_data = http.request(request).body
  create_data_backup if File.exist?(LOCAL_DATA)
  File.open(LOCAL_DATA, "w+") {|f| f.write(req_data.force_encoding('UTF-8'))}
  File.chmod(0777, LOCAL_DATA)
  gem_data_file if req_data.include? "<html><head>"
  file = File.open(LOCAL_DATA)
  gem_data_file if file.size == 0
  file.close
rescue Exception => e
  if e.class == WebMock::NetConnectNotAllowedError
    raise e
  end
  gem_data_file
end

#create_status_backupObject



38
39
40
41
42
43
# 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_tempfileObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vatsim_online/data_downloader.rb', line 21

def create_status_tempfile
  uri = URI(STATUS_URL)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.path)
  http.use_ssl = (uri.scheme == 'https')
  data = http.request(request).body.gsub("\n", '')
  create_status_backup if File.exist?(LOCAL_STATUS)
  File.write(LOCAL_STATUS, data)
  File.chmod(0777, LOCAL_STATUS)
  dummy_status if data.include? "<html><head>"
rescue Exception => e
  if e.class == WebMock::NetConnectNotAllowedError
    raise e
  end
  dummy_status
end

#data_fileObject



107
108
109
110
# File 'lib/vatsim_online/data_downloader.rb', line 107

def data_file
  File.exist?(LOCAL_DATA) ? read_local_datafile : create_local_data_file
  LOCAL_DATA
end

#dummy_statusObject



119
120
121
122
123
124
# File 'lib/vatsim_online/data_downloader.rb', line 119

def dummy_status
  source = LOCAL_STATUS_BAK
  target = LOCAL_STATUS
  FileUtils.cp_r source, target
  File.chmod(0777, LOCAL_STATUS)
end

#gem_data_fileObject



112
113
114
115
116
117
# File 'lib/vatsim_online/data_downloader.rb', line 112

def gem_data_file
  source = LOCAL_DATA_BAK
  target = LOCAL_DATA
  FileUtils.cp_r source, target
  File.chmod(0777, LOCAL_DATA)
end

#get_data(url) ⇒ Object



125
126
127
128
# File 'lib/vatsim_online/data_downloader.rb', line 125

def get_data(url)
  uri = URI(url)
  Net::HTTP.get(uri)
end

#read_local_datafileObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vatsim_online/data_downloader.rb', line 95

def read_local_datafile
  data = File.open(LOCAL_DATA)
  difference = Time.diff(data.mtime, Time.now)[:second] + (Time.diff(data.mtime, Time.now)[:minute] * 60)
  if difference > 60
    d = create_local_data_file
  else
    d = data.read
  end
  data.close
  d
end

#read_status_tempfileObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vatsim_online/data_downloader.rb', line 45

def read_status_tempfile
  status = File.open(LOCAL_STATUS)
  difference = Time.diff(status.mtime, Time.now)[:hour]
  if difference > 3
    data = create_status_tempfile
  else
    data = status.read
  end
  status.close
  data
end

#serversObject



62
63
64
65
66
# File 'lib/vatsim_online/data_downloader.rb', line 62

def servers
  raw_data = File.read(status_file)
  data = JSON.parse(raw_data)
  data['data']['v3']
end

#status_fileObject



57
58
59
60
# File 'lib/vatsim_online/data_downloader.rb', line 57

def status_file
  File.exist?(LOCAL_STATUS) ? read_status_tempfile : create_status_tempfile
  LOCAL_STATUS
end