vatsim_online_redux 1.0.1 → 2.1.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: c66083b4e7cce58e2f54c7582452157ff5ef8ea741b77a07b18cccc5b21a0e21
4
+ data.tar.gz: 1513cda20bf54348b56821c50d18de0b9f389f58fe31f7875a3572f0a7bd9124
5
5
  SHA512:
6
- metadata.gz: 8ecc57d54538a812f72ad152e9ef690b61ccf8be8c689997550d5c15d913c9b23e855b21ec746a0a0bc5732f92ba564b6f44d8110e55110564ee3c9a26f6717c
7
- data.tar.gz: 83652fe1c4c98c5ca26a182e41b6e79583e4b0e5db8b95a57b51cf9759ecf3518db9cb172d0667018c3fcb4af6e11efd048c33accfbe3c66f971581f65b74ce9
6
+ metadata.gz: 6597071f1292ef8a6e2f0ae6b05e285fa7fde6df224f6d88868798d24014ee2e7b1e964a95bdb8c3eadae11215473882e6b4eb0af8af35e4f14b921368f09614
7
+ data.tar.gz: 15fb29033a50d6a5f71e48588551454c7d4fdd0f2a76c18dfae6e232f654f8cd048154038bf89628a516e89c7a2ceae39b687b8f86be75eabe9e6b8ad8a9be8e
data/CHANGELOG.md CHANGED
@@ -2,6 +2,27 @@
2
2
 
3
3
  ## Changelog
4
4
 
5
+ ### v. 2.1.1 - 16 Mar 2022
6
+
7
+ * Fixing a bug with the time diff on the status and data files
8
+
9
+ ### v. 2.1.0 - 20 Apr 2021
10
+
11
+ * Added the new vatsim_regex_callsign method
12
+ * Updated refresh time for data from 3 minutes to 60 seconds
13
+
14
+ ### v. 2.0.1 - 30 Jan 2021
15
+
16
+ * Updated the code to use the new VATSIM Json data feed
17
+
18
+ ### v. 1.0.1 - 30 Mar 2020
19
+
20
+ * Small bugfix to the 1.0.0 release
21
+
22
+ ### v. 1.0.0 - 30 Mar 2020
23
+
24
+ * Updating code to work better with Windows environments
25
+
5
26
  ### v. 0.9.1 - 31 Dec 2016
6
27
 
7
28
  * Maintenance release, updated dependencies
data/README.md CHANGED
@@ -11,10 +11,7 @@ online ATC and/or pilots for given airports, areas or globally and to filter by
11
11
  returned as objects, exposing a rich set of attributes. Vatsim data is pulled
12
12
  on preset intervals and cached locally to avoid flooding the servers.
13
13
 
14
- [![Build Status](https://secure.travis-ci.org/tarakanbg/vatsim_online.png?branch=master)](http://travis-ci.org/tarakanbg/vatsim_online)
15
- [![Gemnasium](https://gemnasium.com/tarakanbg/vatsim_online.png?travis)](https://gemnasium.com/tarakanbg/vatsim_online)
16
- [![Gem Version](https://badge.fury.io/rb/vatsim_online.png)](http://badge.fury.io/rb/vatsim_online)
17
- [![Code Climate](https://codeclimate.com/github/tarakanbg/vatsim_online.png)](https://codeclimate.com/github/tarakanbg/vatsim_online)
14
+ [![Gem Version](https://badge.fury.io/rb/vatsim_online_redux.png)](http://badge.fury.io/rb/vatsim_online_redux)
18
15
 
19
16
  ## Requirements
20
17
 
@@ -48,6 +45,11 @@ any string (or variable containing a string) representing a full or partial pilo
48
45
  callsign or a comma-separated list of callsigns. The provided callsign or fragment
49
46
  will be used as a search criteria and matched against the current vatsim data.
50
47
 
48
+ * `vatsim_regex_callsign`, which can be applied to
49
+ any string (or variable containing a string) representing a regular expression or
50
+ a comma-separated list of regular expressions.
51
+ The provided regex will be used as a search criteria and matched against the current vatsim data.
52
+
51
53
  ### Filter by ICAO code examples
52
54
 
53
55
  For example if you want to retrieve all active stations (ATC positions and pilots)
@@ -103,6 +105,13 @@ callsign = "BAW, RYR"
103
105
  callsign.vatsim_callsign # => returns an array of all Speedbird and Ryanair flights as station objects
104
106
  ```
105
107
 
108
+ ### Filter by regex examples
109
+
110
+ ```ruby
111
+ # Attaching the method directly to a string:
112
+ 'BAW\d{3}'.vatsim_regex_callsign # => returns an array of all Speedbird flights with a 3 digit flight number as station objects
113
+ ```
114
+
106
115
 
107
116
  ### Anatomy of method returns
108
117
 
@@ -142,7 +151,7 @@ p1.remarks #=> "/V/ RMK/CHARTS"
142
151
  want to loop through them separately. The `pilots` array contains all arrivals
143
152
  and departures.
144
153
 
145
- The `vatsim_callsign` method returns an **array** of all matching stations.
154
+ The `vatsim_callsign` and `vatsim_regex_callsign` methods return an **array** of all matching stations.
146
155
 
147
156
  ### Station attributes
148
157
 
@@ -320,7 +329,7 @@ end
320
329
 
321
330
  * Vatsim status and data files are cached locally to reduce the load on vatsim
322
331
  servers. Random server is chosen to retrieve the data each time. By default the
323
- status file is updated once every 4 hours and the data file once every 3 minutes
332
+ status file is updated once every 4 hours and the data file once every minute
324
333
  regardless of the number of incoming requests.
325
334
  * The data is cached in your default TEMP directory (OS specific)
326
335
  * All the data retrieval and caching logic is encapsulated in a separate class
@@ -335,134 +344,6 @@ front or after the identifiers or the commas, i.e. you can use
335
344
  `"LO,LB".vatsim_online` or `"LO, LB".vatsim_online` or `"LO , LB".vatsim_online` with
336
345
  the same result.
337
346
 
338
- ## Changelog
339
-
340
- ### v. 0.9.1 - 31 Dec 2016
341
-
342
- * Maintenance release, updated dependencies
343
-
344
- ### v. 0.9 - 20 June 2015
345
-
346
- * Fixed nil String bug, can select "ALL" (thanks to Pierre Ferran and Florian Rimoli)
347
-
348
- ### v. 0.8.3 - 10 April 2014
349
-
350
- * Fix stale data bug, change data fallback logic
351
-
352
- ### v. 0.8.2 - 5 October 2013
353
-
354
- * Handle undetermined position when drawing gcmap
355
-
356
- ### v. 0.8.1 - 5 October 2013
357
-
358
- * Ignore prefile data
359
-
360
- ### v. 0.8 - 5 October 2013
361
-
362
- * search by callsign implemented
363
-
364
- ### v. 0.7.4 - 2 August 2013
365
-
366
- * fallback scenarios for offline data servers
367
-
368
- ### v. 0.7.2 - 15 July 2013
369
-
370
- * added gemspec license declaration
371
-
372
- ### v. 0.7.1 - 15 July 2013
373
-
374
- * more comprehensive ATC ratings list
375
- * dependencies updated
376
-
377
- ### v. 0.7.0 - 26 February 2013
378
-
379
- * added Ruby 2.0 support
380
-
381
- ### v. 0.6.2 - 4 January 2013
382
-
383
- * updated gem dependencies
384
- * refactored `Station` class for simplicity
385
-
386
- ### v. 0.6.1 - 08 October 2012
387
-
388
- * Fixed pilot station duplication issue when using multiple ICAOs
389
-
390
- ### v. 0.6.0 - 08 October 2012
391
-
392
- * The `vatsim_online` method now also supports a comma-separated list of full or
393
- partial ICAO codes like this: `"LO,LB".vatsim_online`. This allows you to pull the
394
- information for multiple airports or FIRs in a single request
395
- * The comma-seprated list is not sensitive to whitespace, meaning you can use
396
- `"LO,LB".vatsim_online` or `"LO, LB".vatsim_online` or `"LO , LB".vatsim_online` with
397
- the same result
398
-
399
- ### v. 0.5.3 - 30 September 2012
400
-
401
- * fixed bug with exceptions on missing ATC remark
402
-
403
- ### v. 0.5.2 - 29 September 2012
404
-
405
- * fixed permissions bug on UNIX systems
406
-
407
- ### v. 0.5.1 - 23 September 2012
408
-
409
- * bugfixes
410
-
411
- ### v. 0.5 - 23 September 2012
412
-
413
- * New option `:exclude => "ICAO"` allowing further request customization by
414
- excluding a matching subset of ATC stations from the listing. Read the documentation
415
- for detailed explanation and examples
416
- * New customized station attribute: `online_since`. Returns the station login time
417
- parsed as a Ruby Time object in UTC (zulu time). As opposed to the `logon` attribute
418
- which returns an unformatted, unparsed string such as `20120722091954`
419
- * The `rating` station attribute is now humanized not to return just an integer,
420
- but a readable version of the VATSIM rating, i.e. S1, S2, S3, C1, C3, I1, I3, etc...
421
- * Added the possibility of customizing the great circle maps by optionally passing
422
- parameters for width and height: `icao.vatsim_online(:gcmap_width => 400, :gcmap_height => 400)`.
423
- Read the documentation for detailed explanation and examples
424
- * New customized station attribute `atis_message`. It will return a humanized web safe
425
- version of the ATC atis without the voice server info and with lines split with
426
- `<br />` tags. As opposed to the original `atis` attribute, which returns raw atis,
427
- as reported from VATSIM, including voice server as first line
428
-
429
- ### v. 0.4 - 27 August 2012
430
-
431
- * GCMapper integration: this library now plays nicely with [gcmapper](https://rubygems.org/gems/gcmapper).
432
- A new attribute is provided for the pilot stations: `.gcmap` which returns a great
433
- circle map image url, depicting the GC route of the aircraft, its origin and destination,
434
- its current position on the route, together with its current altitude and groundspeed.
435
- Look at the example in the README section above.
436
- * New station attributes: planned_altitude, transponder, heading, qnh_in, qnh_mb,
437
- flight_type, cid, latitude_humanized, longitude_humanized
438
-
439
-
440
- ### v. 0.3 - 22 July 2012
441
-
442
- * The hash returned by the `vatsim_online` method now includes 2 new arrays:
443
- `arrivals` and `departures`. These two are returned separately for convenience,
444
- in case you want to loop through them separately. The `pilots` array return is
445
- unchanged and contains all arrivals and departures.
446
- * New station attributes: latitude, longitude
447
- * Improved UTF-8 conversion process
448
-
449
- ### v. 0.2 - 21 July 2012
450
-
451
- * Station attribute `departure` is now renamed to `origin`
452
- * UTF-8 is now enforced for all local caching and file/string manipulations, the
453
- original Vatsim data is re-encoded
454
- * Station ATIS is now cleaned of invalid and obscure characters
455
- * Improved documentation
456
-
457
- ## Contributing
458
-
459
- 1. Fork it
460
- 2. Create your feature branch (`git checkout -b my-new-feature`)
461
- 3. Commit your changes (`git commit -am 'Added some feature'`)
462
- 4. Make sure all tests are passing!
463
- 5. Push to the branch (`git push origin my-new-feature`)
464
- 6. Create new Pull Request
465
-
466
347
  ## Credits
467
348
 
468
349
  Copyright © 2016 [Svilen Vassilev](http://svilen.rubystudio.net)
@@ -473,6 +354,6 @@ Copyright © 2020 [Alex Dent](https://github.com/aldent95)
473
354
  * [Florian Rimoli](https://github.com/Flox06)
474
355
  * [Pierre Ferran](https://github.com/pierr3)
475
356
 
476
- Released under the [MIT LICENSE](https://github.com/tarakanbg/vatsim_online/blob/master/LICENSE)
357
+ Released under the [MIT LICENSE](https://github.com/tarakanbg/vatsim_online_redux/blob/master/LICENSE)
477
358
 
478
359
  Maps generated by the [Great Circle Mapper](http://www.gcmap.com/), copyright Karl L. Swartz
@@ -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
@@ -0,0 +1,60 @@
1
+ module VatsimTools
2
+
3
+ class CallsignRegexParser
4
+
5
+ %w{tmpdir csv}.each { |lib| require lib }
6
+ require_relative "data_downloader"
7
+ require_relative "station"
8
+
9
+ attributes = %w{role callsign gcmap_width gcmap_height}
10
+ attributes.each {|attribute| attr_accessor attribute.to_sym }
11
+
12
+ LOCAL_DATA = "#{Dir.tmpdir}/vatsim_online/vatsim_data.json"
13
+
14
+ def initialize(callsign, args = nil)
15
+ VatsimTools::DataDownloader.new
16
+ # args.class == Hash ? @role = determine_role(args) : @role = "all"
17
+ @callsign = callsign.split(',').each {|s| s.strip!}
18
+ # @excluded = args[:exclude].upcase if args && args[:exclude]
19
+ @gcmap_width = args[:gcmap_width] if args && args[:gcmap_width]
20
+ @gcmap_height = args[:gcmap_height] if args && args[:gcmap_height]
21
+ end
22
+
23
+ # def determine_role(args)
24
+ # args[:atc] == false ? role = "pilot" : role = "all"
25
+ # args[:pilots] == false ? role = "atc" : role = role
26
+ # role = "all" if args[:pilots] == false && args[:atc] == false
27
+ # role
28
+ # end
29
+
30
+
31
+ def stations
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 Regexp.new(cs) =~ callsign[0...cs.length] # && client == "ATC") unless @role == "pilot"
43
+ # stations << row if (origin[0...icao.length] == icao || destination[0...icao.length] == icao) unless @role == "atc"
44
+ end
45
+ end
46
+ matching_stations
47
+ end
48
+
49
+ def station_objects
50
+ station_objects= []
51
+ args = {}
52
+ args[:gcmap_width] = @gcmap_width if @gcmap_width
53
+ args[:gcmap_height] = @gcmap_height if @gcmap_height
54
+ stations.each {|station| station_objects << VatsimTools::Station.new(station, args) }
55
+ station_objects
56
+ end
57
+
58
+ end
59
+
60
+ end
@@ -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.mtime, 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.mtime, Time.now)[:second] + (Time.diff(data.mtime, Time.now)[:minute] * 60)
98
+ if difference > 60
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