vatsim_online_redux 1.0.1 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a78e86307bec08f8b4dd9dbb4fefbb7178201c182fd79a636fb08779d638efc
4
- data.tar.gz: 6d4cb6b9b60b1214539bc3b34a84b19970199f9cb54721f097e4ca8e3e3924f3
3
+ metadata.gz: 9b68e83fc9b674c716b6bc39a5c49ca70a9d111eb0222bc344d0dd60df8582e9
4
+ data.tar.gz: 22751c268f529366bd591a6b1d7ce6bd020390c521aacf6d1a72417ae90c59df
5
5
  SHA512:
6
- metadata.gz: 8ecc57d54538a812f72ad152e9ef690b61ccf8be8c689997550d5c15d913c9b23e855b21ec746a0a0bc5732f92ba564b6f44d8110e55110564ee3c9a26f6717c
7
- data.tar.gz: 83652fe1c4c98c5ca26a182e41b6e79583e4b0e5db8b95a57b51cf9759ecf3518db9cb172d0667018c3fcb4af6e11efd048c33accfbe3c66f971581f65b74ce9
6
+ metadata.gz: 2f7e76e654c1f4c87c58f33c84ac13b115e5811571b9f07fedb08804867d3a6ff591b993d56ae0d2d054b8ac8d9e0e7b7ffc8b854d7b682ce9941b15e79a43e9
7
+ data.tar.gz: f8a65ee499723d787f6c25c26ab9d4dcabc7d764c3c18441bd2a78bbec92ed385f9fd6718724193ef76901e62b28643a181c978e29b2c350cfbd08eb2e0f93e4
@@ -9,7 +9,7 @@ module VatsimTools
9
9
  attributes = %w{role callsign gcmap_width gcmap_height}
10
10
  attributes.each {|attribute| attr_accessor attribute.to_sym }
11
11
 
12
- LOCAL_DATA = "#{Dir.tmpdir}/vatsim_online/vatsim_data.txt"
12
+ LOCAL_DATA = "#{Dir.tmpdir}/vatsim_online/vatsim_data.json"
13
13
 
14
14
  def initialize(callsign, args = nil)
15
15
  VatsimTools::DataDownloader.new
@@ -29,15 +29,21 @@ module VatsimTools
29
29
 
30
30
 
31
31
  def stations
32
- stations = []
33
- CSV.foreach(LOCAL_DATA, :col_sep =>':') do |row|
34
- callsign, origin, destination, client = row[0].to_s, row[11].to_s, row[13].to_s, row[3].to_s
35
- for cs in @callsign
36
- stations << row if callsign[0...cs.length] == cs # && client == "ATC") unless @role == "pilot"
32
+ matching_stations = []
33
+ raw_data = File.read(LOCAL_DATA)
34
+ data = JSON.parse(raw_data)
35
+ pilots = data['pilots'].each {|p| p['role'] = 'pilot'}
36
+ controllers = data['controllers'].each {|p| p['role'] = 'controller'}
37
+ atis = data['atis'].each {|p| p['role'] = 'atis'}
38
+ stations = pilots + controllers + atis
39
+ stations.each do |station|
40
+ callsign = station['callsign']
41
+ @callsign.each do |cs|
42
+ matching_stations << station if callsign[0...cs.length] == cs # && client == "ATC") unless @role == "pilot"
37
43
  # stations << row if (origin[0...icao.length] == icao || destination[0...icao.length] == icao) unless @role == "atc"
38
44
  end
39
45
  end
40
- stations
46
+ matching_stations
41
47
  end
42
48
 
43
49
  def station_objects
@@ -1,127 +1,131 @@
1
- module VatsimTools
2
- require 'tempfile'
3
- require 'time_diff'
4
- require 'tmpdir'
5
- require 'net/http'
6
- require 'fileutils'
7
- class DataDownloader
8
-
9
- STATUS_URL = "http://status.vatsim.net/status.txt"
10
- TEMP_DIR = "#{Dir.tmpdir}/vatsim_online"
11
- LOCAL_STATUS = "#{Dir.tmpdir}/vatsim_online/vatsim_status.txt"
12
- LOCAL_STATUS_BAK = "#{Dir.tmpdir}/vatsim_online/vatsim_status_bak.txt"
13
- LOCAL_DATA = "#{Dir.tmpdir}/vatsim_online/vatsim_data.txt"
14
- LOCAL_DATA_BAK = "#{Dir.tmpdir}/vatsim_online/vatsim_data_bak.txt"
15
-
16
- def initialize
17
- FileUtils.mkdir(TEMP_DIR) unless File.exist? TEMP_DIR
18
- data_file
19
- end
20
-
21
- def create_status_tempfile
22
- uri = URI(STATUS_URL)
23
- http = Net::HTTP.new(uri.host, uri.port)
24
- request = Net::HTTP::Get.new(uri.path)
25
- data = http.request(request).body.gsub("\n", '')
26
- create_status_backup if File.exists?(LOCAL_STATUS)
27
- File.write(LOCAL_STATUS, data)
28
- File.chmod(0777, LOCAL_STATUS)
29
- dummy_status if data.include? "<html><head>"
30
- rescue Exception => e
31
- dummy_status
32
- end
33
-
34
- def create_status_backup
35
- source = LOCAL_STATUS
36
- target = LOCAL_STATUS_BAK
37
- FileUtils.cp_r source, target
38
- File.chmod(0777, LOCAL_STATUS_BAK)
39
- end
40
-
41
- def read_status_tempfile
42
- status = File.open(LOCAL_STATUS)
43
- difference = Time.diff(status.ctime, Time.now)[:hour]
44
- if difference > 3
45
- data = create_status_tempfile
46
- else
47
- data = status.read
48
- end
49
- status.close
50
- data
51
- end
52
-
53
- def status_file
54
- File.exists?(LOCAL_STATUS) ? read_status_tempfile : create_status_tempfile
55
- LOCAL_STATUS
56
- end
57
-
58
- def servers
59
- urls = []
60
- CSV.foreach(status_file, :col_sep =>'=') do |row|
61
- urls << row[1] if row[0] == "url0"
62
- end
63
- urls
64
- end
65
-
66
- def create_local_data_file
67
- uri = URI(servers.sample)
68
- http = Net::HTTP.new(uri.host, uri.port)
69
- request = Net::HTTP::Get.new(uri.path)
70
- req_data = http.request(request).body
71
- create_data_backup if File.exists?(LOCAL_DATA)
72
- data = req_data.gsub(/["]/, '\s').encode!('UTF-16', 'UTF-8', :invalid => :replace, :replace => '').encode!('UTF-8', 'UTF-16')
73
- data = data.slice(0..(data.index('!PREFILE:')))
74
- File.open(LOCAL_DATA, "w+") {|f| f.write(data)}
75
- File.chmod(0777, LOCAL_DATA)
76
- gem_data_file if req_data.include? "<html><head>"
77
- file = File.open(LOCAL_DATA)
78
- gem_data_file if file.size == 0
79
- file.close
80
- rescue Exception => e
81
- gem_data_file
82
- end
83
-
84
- def create_data_backup
85
- source = LOCAL_DATA
86
- target = LOCAL_DATA_BAK
87
- FileUtils.cp_r source, target
88
- File.chmod(0777, LOCAL_DATA_BAK)
89
- end
90
-
91
- def read_local_datafile
92
- data = File.open(LOCAL_DATA)
93
- difference = Time.diff(data.ctime, Time.now)[:minute]
94
- if difference > 2
95
- d = create_local_data_file
96
- else
97
- d = data.read
98
- end
99
- data.close
100
- d
101
- end
102
-
103
- def data_file
104
- File.exists?(LOCAL_DATA) ? read_local_datafile : create_local_data_file
105
- LOCAL_DATA
106
- end
107
-
108
- def gem_data_file
109
- source = LOCAL_DATA_BAK
110
- target = LOCAL_DATA
111
- FileUtils.cp_r source, target
112
- File.chmod(0777, LOCAL_DATA)
113
- end
114
-
115
- def dummy_status
116
- source = LOCAL_STATUS_BAK
117
- target = LOCAL_STATUS
118
- FileUtils.cp_r source, target
119
- File.chmod(0777, LOCAL_STATUS)
120
- end
121
- def get_data(url)
122
- uri = URI(url)
123
- Net::HTTP.get(uri)
124
- end
125
- end
126
-
127
- end
1
+ module VatsimTools
2
+ require 'tempfile'
3
+ require 'time_diff'
4
+ require 'tmpdir'
5
+ require 'net/http'
6
+ require 'fileutils'
7
+ class DataDownloader
8
+
9
+ STATUS_URL = "https://status.vatsim.net/status.json"
10
+ TEMP_DIR = "#{Dir.tmpdir}/vatsim_online"
11
+ LOCAL_STATUS = "#{Dir.tmpdir}/vatsim_online/vatsim_status.json"
12
+ LOCAL_STATUS_BAK = "#{Dir.tmpdir}/vatsim_online/vatsim_status_bak.json"
13
+ LOCAL_DATA = "#{Dir.tmpdir}/vatsim_online/vatsim_data.json"
14
+ LOCAL_DATA_BAK = "#{Dir.tmpdir}/vatsim_online/vatsim_data_bak.json"
15
+
16
+ def initialize
17
+ FileUtils.mkdir(TEMP_DIR) unless File.exist? TEMP_DIR
18
+ data_file
19
+ end
20
+
21
+ def create_status_tempfile
22
+ uri = URI(STATUS_URL)
23
+ http = Net::HTTP.new(uri.host, uri.port)
24
+ request = Net::HTTP::Get.new(uri.path)
25
+ http.use_ssl = (uri.scheme == 'https')
26
+ data = http.request(request).body.gsub("\n", '')
27
+ create_status_backup if File.exists?(LOCAL_STATUS)
28
+ File.write(LOCAL_STATUS, data)
29
+ File.chmod(0777, LOCAL_STATUS)
30
+ dummy_status if data.include? "<html><head>"
31
+ rescue Exception => e
32
+ if e.class == WebMock::NetConnectNotAllowedError
33
+ raise e
34
+ end
35
+ dummy_status
36
+ end
37
+
38
+ def create_status_backup
39
+ source = LOCAL_STATUS
40
+ target = LOCAL_STATUS_BAK
41
+ FileUtils.cp_r source, target
42
+ File.chmod(0777, LOCAL_STATUS_BAK)
43
+ end
44
+
45
+ def read_status_tempfile
46
+ status = File.open(LOCAL_STATUS)
47
+ difference = Time.diff(status.ctime, Time.now)[:hour]
48
+ if difference > 3
49
+ data = create_status_tempfile
50
+ else
51
+ data = status.read
52
+ end
53
+ status.close
54
+ data
55
+ end
56
+
57
+ def status_file
58
+ File.exists?(LOCAL_STATUS) ? read_status_tempfile : create_status_tempfile
59
+ LOCAL_STATUS
60
+ end
61
+
62
+ def servers
63
+ raw_data = File.read(status_file)
64
+ data = JSON.parse(raw_data)
65
+ data['data']['v3']
66
+ end
67
+
68
+ def create_local_data_file
69
+ uri = URI(servers.sample)
70
+ http = Net::HTTP.new(uri.host, uri.port)
71
+ request = Net::HTTP::Get.new(uri.path)
72
+ http.use_ssl = (uri.scheme == 'https')
73
+ req_data = http.request(request).body
74
+ create_data_backup if File.exists?(LOCAL_DATA)
75
+ File.open(LOCAL_DATA, "w+") {|f| f.write(req_data.force_encoding('UTF-8'))}
76
+ File.chmod(0777, LOCAL_DATA)
77
+ gem_data_file if req_data.include? "<html><head>"
78
+ file = File.open(LOCAL_DATA)
79
+ gem_data_file if file.size == 0
80
+ file.close
81
+ rescue Exception => e
82
+ if e.class == WebMock::NetConnectNotAllowedError
83
+ raise e
84
+ end
85
+ gem_data_file
86
+ end
87
+
88
+ def create_data_backup
89
+ source = LOCAL_DATA
90
+ target = LOCAL_DATA_BAK
91
+ FileUtils.cp_r source, target
92
+ File.chmod(0777, LOCAL_DATA_BAK)
93
+ end
94
+
95
+ def read_local_datafile
96
+ data = File.open(LOCAL_DATA)
97
+ difference = Time.diff(data.ctime, Time.now)[:minute]
98
+ if difference > 2
99
+ d = create_local_data_file
100
+ else
101
+ d = data.read
102
+ end
103
+ data.close
104
+ d
105
+ end
106
+
107
+ def data_file
108
+ File.exists?(LOCAL_DATA) ? read_local_datafile : create_local_data_file
109
+ LOCAL_DATA
110
+ end
111
+
112
+ def gem_data_file
113
+ source = LOCAL_DATA_BAK
114
+ target = LOCAL_DATA
115
+ FileUtils.cp_r source, target
116
+ File.chmod(0777, LOCAL_DATA)
117
+ end
118
+
119
+ def dummy_status
120
+ source = LOCAL_STATUS_BAK
121
+ target = LOCAL_STATUS
122
+ FileUtils.cp_r source, target
123
+ File.chmod(0777, LOCAL_STATUS)
124
+ end
125
+ def get_data(url)
126
+ uri = URI(url)
127
+ Net::HTTP.get(uri)
128
+ end
129
+ end
130
+
131
+ end
@@ -12,27 +12,44 @@ module VatsimTools
12
12
 
13
13
  def initialize(station, args = nil)
14
14
 
15
- @callsign, @cid, @name, @role, @frequency, @latitude, @longitude, @altitude, @groundspeed, @aircraft, @origin,
16
- @planned_altitude, @destination, @transponder, @facility, @flight_type, @remarks, @route, @logon, @heading,
17
- @qnh_in, @qnh_mb = station[0], station[1], station[2], station[3], station[4], station[5], station[6], station[7],
18
- station[8], station[9], station[11], station[12], station[13], station[17], station[18], station[21], station[29],
19
- station[30], station[37], station[38], station[39], station[40]
15
+ @callsign = station['callsign']
16
+ @cid = station['cid'].to_s
17
+ @name = station['name']
18
+ @role = station['role']
19
+ @frequency = station['frequency']
20
+ @latitude = station['latitude'].to_s
21
+ @longitude = station['longitude'].to_s
22
+ @altitude = station['altitude']
23
+ @groundspeed = station['groundspeed']
24
+ @aircraft = station['flight_plan']['aircraft'] rescue ''
25
+ @origin = station['flight_plan']['departure'] rescue ''
26
+ @planned_altitude = station['flight_plan']['altitude'] rescue ''
27
+ @destination = station['flight_plan']['arrival'] rescue ''
28
+ @transponder = station['transponder']
29
+ @facility = station['facility'].to_s
30
+ @flight_type = station['flight_plan']['flight_rules'] rescue ''
31
+ @remarks = station['flight_plan']['remarks'] rescue ''
32
+ @route = station['flight_plan']['route'] rescue ''
33
+ @logon = station['logon_time']
34
+ @heading = station['heading'].to_s
35
+ @qnh_in = station['qnh_i_hg'].to_s
36
+ @qnh_mb = station['qnh_mb'].to_s
20
37
 
21
- @atis = atis_cleaner(station[35]) if station[35]
22
- @rating = humanized_rating(station[16])
23
- @latitude_humanized = latitude_parser(station[5].to_f)
24
- @longitude_humanized = longitude_parser(station[6].to_f)
38
+ @atis = atis_cleaner(station['text_atis']) if station['text_atis']
39
+ @rating = humanized_rating(station['rating'].to_s)
40
+ @latitude_humanized = latitude_parser(station['latitude'])
41
+ @longitude_humanized = longitude_parser(station['longitude'])
25
42
  @online_since = utc_logon_time if @logon
26
43
  @gcmap_width = args[:gcmap_width].to_i if args && args[:gcmap_width]
27
44
  @gcmap_height = args[:gcmap_height].to_i if args && args[:gcmap_height]
28
45
  @gcmap = gcmap_generator
29
- @atis_message = construct_atis_message(station[35]) if station[35]
46
+ @atis_message = construct_atis_message(station['text_atis']) if station['text_atis']
30
47
  end
31
48
 
32
49
  private
33
50
 
34
51
  def gcmap_generator
35
- return "No map for ATC stations" if @role != "PILOT"
52
+ return "No map for ATC stations" if @role != "pilot"
36
53
  construct_gcmap_url.gcmap(:width => @gcmap_width, :height => @gcmap_height)
37
54
  end
38
55
 
@@ -47,22 +64,26 @@ module VatsimTools
47
64
  route
48
65
  end
49
66
 
50
- def latitude_parser(lat)
67
+ def latitude_parser(lat_s)
68
+ return nil if lat_s == nil
69
+ lat = lat_s.to_f
51
70
  lat > 0 ? hemisphere = "N" : hemisphere = "S"
52
71
  hemisphere + lat.abs.to_s
53
72
  end
54
73
 
55
- def longitude_parser(lon)
74
+ def longitude_parser(lon_s)
75
+ return nil if lon_s == nil
76
+ lon = lon_s.to_f
56
77
  lon > 0 ? hemisphere = "E" : hemisphere = "W"
57
78
  hemisphere + lon.abs.to_s
58
79
  end
59
80
 
60
81
  def atis_cleaner(raw_atis)
61
- raw_atis.gsub(/[\^]/, '. ')
82
+ raw_atis.join(' ').gsub(/[\^]/, '. ')
62
83
  end
63
84
 
64
85
  def utc_logon_time
65
- Time.parse ("#{@logon[0...4]}-#{@logon[4...6]}-#{@logon[6...8]} #{@logon[8...10]}:#{@logon[10...12]}:#{@logon[12...14]} UTC")
86
+ Time.parse(@logon)
66
87
  end
67
88
 
68
89
  def humanized_rating(rating_number)
@@ -84,7 +105,7 @@ module VatsimTools
84
105
  end
85
106
 
86
107
  def construct_atis_message(raw_atis)
87
- message = raw_atis.gsub(/[\^]/, '<br />')
108
+ message = raw_atis.join(' ').gsub(/[\^]/, '<br />')
88
109
  message.index('>') ? message = message[message.index('>')+1...message.length] : message = "No published remark"
89
110
  end
90
111
 
@@ -7,9 +7,9 @@ module VatsimTools
7
7
  require_relative "station"
8
8
 
9
9
  attributes = %w{role icao excluded gcmap_width gcmap_height}
10
- attributes.each {|attribute| attr_accessor attribute.to_sym }
10
+ attributes.each { |attribute| attr_accessor attribute.to_sym }
11
11
 
12
- LOCAL_DATA = "#{Dir.tmpdir}/vatsim_online/vatsim_data.txt"
12
+ LOCAL_DATA = "#{Dir.tmpdir}/vatsim_online/vatsim_data.json"
13
13
 
14
14
  def initialize(icao, args = nil)
15
15
  VatsimTools::DataDownloader.new
@@ -17,7 +17,7 @@ module VatsimTools
17
17
  if icao == "ALL"
18
18
  @icao = nil
19
19
  else
20
- @icao = icao.upcase.split(',').each {|s| s.strip!}
20
+ @icao = icao.upcase.split(',').each { |s| s.strip! }
21
21
  end
22
22
  @excluded = args[:exclude].upcase if args && args[:exclude]
23
23
  @gcmap_width = args[:gcmap_width] if args && args[:gcmap_width]
@@ -32,44 +32,53 @@ module VatsimTools
32
32
  end
33
33
 
34
34
  def stations
35
- stations = []
36
- CSV.foreach(LOCAL_DATA, :col_sep =>':') do |row|
37
- callsign, origin, destination, client = row[0].to_s, row[11].to_s, row[13].to_s, row[3].to_s
35
+ matching_stations = []
36
+ raw_data = File.read(LOCAL_DATA)
37
+ data = JSON.parse(raw_data)
38
+ pilots = data['pilots'].each { |p| p['role'] = 'pilot' }
39
+ controllers = data['controllers'].each { |p| p['role'] = 'controller' }
40
+ atis = data['atis'].each { |p| p['role'] = 'atis' }
41
+ stations = pilots + controllers + atis
42
+ stations.each do |station|
43
+ callsign = station['callsign']
44
+ destination = station['flight_plan']['arrival'] rescue ''
45
+ origin = station['flight_plan']['departure'] rescue ''
46
+ client = station['role']
38
47
  unless @icao
39
- stations << row if (client == "ATC") unless @role == "pilot"
40
- stations << row if (client == "PILOT") unless @role == "atc"
48
+ matching_stations << station if (client == "controller") unless @role == "pilot"
49
+ matching_stations << station if (client == "pilot") unless @role == "atc"
41
50
  else
42
- for icao in @icao
43
- stations << row if (callsign[0...icao.length] == icao && client == "ATC") unless @role == "pilot"
44
- stations << row if (origin[0...icao.length] == icao || destination[0...icao.length] == icao) unless @role == "atc"
51
+ @icao.each do |icao|
52
+ matching_stations << station if (callsign[0...icao.length] == icao && client == "controller") unless @role == "pilot"
53
+ matching_stations << station if (origin[0...icao.length] == icao || destination[0...icao.length] == icao) unless @role == "atc"
45
54
  end
46
55
  end
47
56
  end
48
- stations
57
+ matching_stations
49
58
  end
50
59
 
51
60
  def station_objects
52
- station_objects= []
61
+ station_objects = []
53
62
  args = {}
54
63
  args[:gcmap_width] = @gcmap_width if @gcmap_width
55
64
  args[:gcmap_height] = @gcmap_height if @gcmap_height
56
- stations.each {|station| station_objects << VatsimTools::Station.new(station, args) }
65
+ stations.each { |station| station_objects << VatsimTools::Station.new(station, args) }
57
66
  station_objects
58
67
  end
59
68
 
60
69
  def sorted_station_objects
61
70
  atc = []; pilots = []; arrivals = []; departures = []
62
- station_objects.each {|sobj| sobj.role == "ATC" ? atc << sobj : pilots << sobj}
71
+ station_objects.each { |sobj| sobj.role == "controller" ? atc << sobj : pilots << sobj }
63
72
  if @icao
64
- for icao in @icao
65
- for pilot in pilots
73
+ @icao.each do |icao|
74
+ pilots.each do |pilot|
66
75
  departures << pilot if pilot.origin[0...icao.length] == icao
67
76
  arrivals << pilot if pilot.destination[0...icao.length] == icao
68
77
  end
69
78
  end
70
79
  end
71
- atc.delete_if {|a| @excluded && a.callsign[0...@excluded.length] == @excluded }
72
- {:atc => atc, :pilots => pilots, :arrivals => arrivals, :departures => departures}
80
+ atc.delete_if { |a| @excluded && a.callsign[0...@excluded.length] == @excluded }
81
+ { :atc => atc, :pilots => pilots, :arrivals => arrivals, :departures => departures }
73
82
  end
74
83
 
75
84
  end